Find out if such a slug exists via a query instead of via a .save() call
authorChristopher Allan Webber <cwebber@dustycloud.org>
Fri, 3 Jun 2011 23:43:08 +0000 (18:43 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Fri, 3 Jun 2011 23:43:08 +0000 (18:43 -0500)
mediagoblin/edit/views.py

index 7df47b188b0124e04ae8f8c44a5d82275144630e..5cfb22977691ccb1a92e782349dc8a12a3f1333b 100644 (file)
@@ -34,18 +34,25 @@ def edit_media(request, media):
         description = media['description'])
 
     if request.method == 'POST' and form.validate():
-        media['title'] = request.POST['title']
-        media['description'] = request.POST['description']
-        media['slug'] = request.POST['slug']
-        try:
-            media.save()
-        except Exception as e:
-            return exc.HTTPConflict(detail = str(e))
-
-        # redirect
-        return exc.HTTPFound(
-            location=request.urlgen("mediagoblin.user_pages.media_home",
-                user=media.uploader()['username'], media=media['_id']))
+        # 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()
+        
+        if existing_user_slug_entries:
+            form.slug.errors.append(
+                u'An entry with that slug already exists for this user.')
+        else:
+            media['title'] = request.POST['title']
+            media['description'] = request.POST['description']
+            media['slug'] = request.POST['slug']
+
+            # redirect
+            return exc.HTTPFound(
+                location=request.urlgen("mediagoblin.user_pages.media_home",
+                    user=media.uploader()['username'], media=media['_id']))
 
     # render
     template = request.template_env.get_template(