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.
This commit is contained in:
parent
e6bd03d429
commit
6bea8a9094
@ -18,6 +18,8 @@ from mediagoblin.tools.mail import send_email
|
|||||||
from mediagoblin.tools.template import render_template
|
from mediagoblin.tools.template import render_template
|
||||||
from mediagoblin.tools.translate import pass_to_ugettext as _
|
from mediagoblin.tools.translate import pass_to_ugettext as _
|
||||||
from mediagoblin import mg_globals
|
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):
|
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']) \
|
instance_title=mg_globals.app_config['html_title']) \
|
||||||
+ _('commented on your post'),
|
+ _('commented on your post'),
|
||||||
rendered_email)
|
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()
|
||||||
|
@ -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.translate import pass_to_ugettext as _
|
||||||
from mediagoblin.tools.pagination import Pagination
|
from mediagoblin.tools.pagination import Pagination
|
||||||
from mediagoblin.user_pages import forms as user_forms
|
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,
|
from mediagoblin.decorators import (uses_pagination, get_user_media_entry,
|
||||||
get_media_entry_by_id,
|
get_media_entry_by_id,
|
||||||
@ -248,17 +249,7 @@ def media_collect(request, media):
|
|||||||
_('"%s" already in collection "%s"')
|
_('"%s" already in collection "%s"')
|
||||||
% (media.title, collection.title))
|
% (media.title, collection.title))
|
||||||
else: # Add item to collection
|
else: # Add item to collection
|
||||||
collection_item = request.db.CollectionItem()
|
add_media_to_collection(collection, media, form.note.data)
|
||||||
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()
|
|
||||||
|
|
||||||
messages.add_message(request, messages.SUCCESS,
|
messages.add_message(request, messages.SUCCESS,
|
||||||
_('"%s" added to collection "%s"')
|
_('"%s" added to collection "%s"')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user