From 6bea8a9094d92a55fdb81b644a06f3c525338e98 Mon Sep 17 00:00:00 2001 From: Elrond Date: Tue, 23 Apr 2013 12:29:17 +0200 Subject: [PATCH] Create add_media_to_collection() The ideas is by Alon Levy. Not only media_collect, but also other places might want to add media to a collection. So refactor this into a function for easier usage. --- mediagoblin/user_pages/lib.py | 20 ++++++++++++++++++++ mediagoblin/user_pages/views.py | 15 +++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/mediagoblin/user_pages/lib.py b/mediagoblin/user_pages/lib.py index 8a064a7c..2f47e4b1 100644 --- a/mediagoblin/user_pages/lib.py +++ b/mediagoblin/user_pages/lib.py @@ -18,6 +18,8 @@ from mediagoblin.tools.mail import send_email from mediagoblin.tools.template import render_template from mediagoblin.tools.translate import pass_to_ugettext as _ from mediagoblin import mg_globals +from mediagoblin.db.base import Session +from mediagoblin.db.models import CollectionItem def send_comment_email(user, comment, media, request): @@ -55,3 +57,21 @@ def send_comment_email(user, comment, media, request): instance_title=mg_globals.app_config['html_title']) \ + _('commented on your post'), rendered_email) + + +def add_media_to_collection(collection, media, note=None, commit=True): + collection_item = CollectionItem() + collection_item.collection = collection.id + collection_item.media_entry = media.id + if note: + collection_item.note = note + Session.add(collection_item) + + collection.items = collection.items + 1 + Session.add(collection) + + media.collected = media.collected + 1 + Session.add(media) + + if commit: + Session.commit() diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index 61c23f16..e3b46c0f 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -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, @@ -248,17 +249,7 @@ def media_collect(request, media): _('"%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.note = form.note.data - 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"') -- 2.25.1