From b62b3b982a2543fb881c93570c544fbef61dd97e Mon Sep 17 00:00:00 2001 From: Elrond Date: Tue, 24 Jan 2012 21:47:40 +0100 Subject: [PATCH] Generic check_media_slug_used db utility. In two cases (generating a new slug and editing the slug) it is nice to know in advance (before the db gets angry) that the slug is used/free. So created a db utility function to check for this on mongo and sql: check_media_slug_used() --- mediagoblin/db/mongo/util.py | 9 +++++++++ mediagoblin/db/sql/util.py | 11 ++++++++++- mediagoblin/db/util.py | 4 ++-- mediagoblin/edit/views.py | 9 ++++----- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/mediagoblin/db/mongo/util.py b/mediagoblin/db/mongo/util.py index 89348d98..2d47f648 100644 --- a/mediagoblin/db/mongo/util.py +++ b/mediagoblin/db/mongo/util.py @@ -301,3 +301,12 @@ def atomic_update(table, query_dict, update_values): table.collection.update( query_dict, {"$set": update_values}) + + +def check_media_slug_used(db, uploader_id, slug, ignore_m_id): + query_dict = {'uploader': uploader_id, 'slug': slug} + if ignore_m_id is not None: + query_dict['_id'] = {'$ne': ignore_m_id} + existing_user_slug_entries = db.MediaEntry.find( + query_dict).count() + return existing_user_slug_entries diff --git a/mediagoblin/db/sql/util.py b/mediagoblin/db/sql/util.py index 737e4e59..efb0fafe 100644 --- a/mediagoblin/db/sql/util.py +++ b/mediagoblin/db/sql/util.py @@ -17,7 +17,7 @@ import sys from mediagoblin.db.sql.base import Session -from mediagoblin.db.sql.models import Tag, MediaTag +from mediagoblin.db.sql.models import MediaEntry, Tag, MediaTag def _simple_printer(string): @@ -285,6 +285,15 @@ def atomic_update(table, query_dict, update_values): Session.commit() +def check_media_slug_used(dummy_db, uploader_id, slug, ignore_m_id): + filt = (MediaEntry.uploader == uploader_id) \ + & (MediaEntry.slug == slug) + if ignore_m_id is not None: + filt = filt & (MediaEntry.id != ignore_m_id) + does_exist = Session.query(MediaEntry.id).filter(filt).first() is not None + return does_exist + + def clean_orphan_tags(): q1 = Session.query(Tag).outerjoin(MediaTag).filter(MediaTag.id==None) for t in q1: diff --git a/mediagoblin/db/util.py b/mediagoblin/db/util.py index 73aee238..ff7aaf79 100644 --- a/mediagoblin/db/util.py +++ b/mediagoblin/db/util.py @@ -21,7 +21,7 @@ except ImportError: if use_sql: from mediagoblin.db.sql.fake import ObjectId, InvalidId, DESCENDING - from mediagoblin.db.sql.util import atomic_update + from mediagoblin.db.sql.util import atomic_update, check_media_slug_used else: from mediagoblin.db.mongo.util import \ - ObjectId, InvalidId, DESCENDING, atomic_update + ObjectId, InvalidId, DESCENDING, atomic_update, check_media_slug_used diff --git a/mediagoblin/edit/views.py b/mediagoblin/edit/views.py index d21ef03a..2df8f9ab 100644 --- a/mediagoblin/edit/views.py +++ b/mediagoblin/edit/views.py @@ -36,6 +36,7 @@ from mediagoblin.tools.text import ( clean_html, convert_to_tag_list_of_dicts, media_tags_as_string) from mediagoblin.tools.licenses import SUPPORTED_LICENSES +from mediagoblin.db.util import check_media_slug_used @get_user_media_entry @@ -58,12 +59,10 @@ def edit_media(request, media): if request.method == 'POST' and form.validate(): # Make sure there isn't already a MediaEntry with such a slug # and userid. - existing_user_slug_entries = request.db.MediaEntry.find( - {'slug': request.POST['slug'], - 'uploader': media.uploader, - '_id': {'$ne': media._id}}).count() + slug_used = check_media_slug_used(request.db, media.uploader, + request.POST['slug'], media.id) - if existing_user_slug_entries: + if slug_used: form.slug.errors.append( _(u'An entry with that slug already exists for this user.')) else: -- 2.25.1