Simplify check_media_slug_used
authorSebastian Spaeth <Sebastian@SSpaeth.de>
Wed, 9 Jan 2013 10:31:05 +0000 (11:31 +0100)
committerSebastian Spaeth <Sebastian@SSpaeth.de>
Tue, 15 Jan 2013 13:49:50 +0000 (14:49 +0100)
Remove the unused dummy_db argument and generally make the function
readable.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
mediagoblin/db/mixin.py
mediagoblin/db/util.py
mediagoblin/edit/views.py

index 9829bb6e92a1101a31ab2748f25998589b4ffd26..001b7826425bee1a6ee67a0079166bf89bb95ec0 100644 (file)
@@ -58,8 +58,7 @@ class MediaEntryMixin(object):
 
         self.slug = slugify(self.title)
 
-        duplicate = check_media_slug_used(mg_globals.database,
-            self.uploader, self.slug, self.id)
+        duplicate = check_media_slug_used(self.uploader, self.slug, self.id)
 
         if duplicate:
             if self.id is not None:
index 90383f811855bd02230f2084a36e433ec651efc6..529ef8b9a636ccfb8d54d75e36e399352945714b 100644 (file)
@@ -29,12 +29,11 @@ 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)
+def check_media_slug_used(uploader_id, slug, ignore_m_id):
+    query = MediaEntry.query.filter_by(uploader=uploader_id, 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
+        query = query.filter(MediaEntry.id != ignore_m_id)
+    does_exist = query.first() is not None
     return does_exist
 
 
index 2f669c6629ce19f6d2122f2e186d0f212a266882..ece11df587f893ab3c8ffe84dd948af683c08f9d 100644 (file)
@@ -57,8 +57,8 @@ 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.
-        slug_used = check_media_slug_used(request.db, media.uploader,
-                request.form['slug'], media.id)
+        slug_used = check_media_slug_used(media.uploader, request.form['slug'],
+                                          media.id)
 
         if slug_used:
             form.slug.errors.append(