Merge remote-tracking branch 'refs/remotes/npigeon/ticket-660'
[mediagoblin.git] / mediagoblin / user_pages / views.py
index bbc3259ae082a07ce441f601e88019a49583a8e3..e3b46c0f8787b41a0868d38f6d113727265d8456 100644 (file)
@@ -24,7 +24,8 @@ from mediagoblin.tools.response import render_to_response, render_404, redirect
 from mediagoblin.tools.translate import pass_to_ugettext as _
 from mediagoblin.tools.pagination import Pagination
 from mediagoblin.user_pages import forms as user_forms
-from mediagoblin.user_pages.lib import send_comment_email
+from mediagoblin.user_pages.lib import (send_comment_email,
+    add_media_to_collection)
 
 from mediagoblin.decorators import (uses_pagination, get_user_media_entry,
     get_media_entry_by_id,
@@ -204,30 +205,31 @@ def media_collect(request, media):
 
     # If we are here, method=POST and the form is valid, submit things.
     # If the user is adding a new collection, use that:
-    if request.form['collection_title']:
+    if form.collection_title.data:
         # Make sure this user isn't duplicating an existing collection
         existing_collection = Collection.query.filter_by(
                                 creator=request.user.id,
-                                title=request.form['collection_title']).first()
+                                title=form.collection_title.data).first()
         if existing_collection:
             messages.add_message(request, messages.ERROR,
-                _('You already have a collection called "%s"!'
-                  % existing_collection.title))
+                _('You already have a collection called "%s"!')
+                % existing_collection.title)
             return redirect(request, "mediagoblin.user_pages.media_home",
-                            user=request.user.username,
-                            media=media.id)
+                            user=media.get_uploader.username,
+                            media=media.slug_or_id)
 
         collection = Collection()
-        collection.title = request.form['collection_title']
-        collection.description = request.form.get('collection_description')
+        collection.title = form.collection_title.data
+        collection.description = form.collection_description.data
         collection.creator = request.user.id
         collection.generate_slug()
         collection.save()
 
     # Otherwise, use the collection selected from the drop-down
     else:
-        collection = Collection.query.filter_by(
-            id=request.form.get('collection')).first()
+        collection = form.collection.data
+        if collection and collection.creator != request.user.id:
+            collection = None
 
     # Make sure the user actually selected a collection
     if not collection:
@@ -236,7 +238,7 @@ def media_collect(request, media):
             _('You have to select or add a collection'))
         return redirect(request, "mediagoblin.user_pages.media_collect",
                     user=media.get_uploader.username,
-                    media=media.id)
+                    media_id=media.id)
 
 
     # Check whether media already exists in collection
@@ -244,29 +246,18 @@ def media_collect(request, media):
         media_entry=media.id,
         collection=collection.id).first():
         messages.add_message(request, messages.ERROR,
-            _('"%s" already in collection "%s"'
-                % (media.title, collection.title)))
+                             _('"%s" already in collection "%s"')
+                             % (media.title, collection.title))
     else: # Add item to collection
-        collection_item = request.db.CollectionItem()
-        collection_item.collection = collection.id
-        collection_item.media_entry = media.id
-        collection_item.author = request.user.id
-        collection_item.note = request.form['note']
-        collection_item.save()
-
-        collection.items = collection.items + 1
-        collection.save()
-
-        media.collected = media.collected + 1
-        media.save()
+        add_media_to_collection(collection, media, form.note.data)
 
         messages.add_message(request, messages.SUCCESS,
-                             _('"%s" added to collection "%s"'
-                               % (media.title, collection.title)))
+                             _('"%s" added to collection "%s"')
+                             % (media.title, collection.title))
 
     return redirect(request, "mediagoblin.user_pages.media_home",
                     user=media.get_uploader.username,
-                    media=media.id)
+                    media=media.slug_or_id)
 
 
 #TODO: Why does @user_may_delete_media not implicate @require_active_login?