Merge branch 'master' of git://gitorious.org/mediagoblin/mediagoblin
[mediagoblin.git] / mediagoblin / user_pages / lib.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 from mediagoblin.tools.mail import send_email
18 from mediagoblin.tools.template import render_template
19 from mediagoblin.tools.translate import pass_to_ugettext as _
20 from mediagoblin import mg_globals
21 from mediagoblin.db.base import Session
22 from mediagoblin.db.models import CollectionItem
23
24
25 def send_comment_email(user, comment, media, request):
26 """
27 Sends comment email to user when a comment is made on their media.
28
29 Args:
30 - user: the user object to whom the email is sent
31 - comment: the comment object referencing user's media
32 - media: the media object the comment is about
33 - request: the request
34 """
35
36 comment_url = request.urlgen(
37 'mediagoblin.user_pages.media_home.view_comment',
38 comment=comment.id,
39 user=media.get_uploader.username,
40 media=media.slug_or_id,
41 qualified=True) + '#comment'
42
43 comment_author = comment.get_author.username
44
45 rendered_email = render_template(
46 request, 'mediagoblin/user_pages/comment_email.txt',
47 {'username': user.username,
48 'comment_author': comment_author,
49 'comment_content': comment.content,
50 'comment_url': comment_url})
51
52 send_email(
53 mg_globals.app_config['email_sender_address'],
54 [user.email],
55 '{instance_title} - {comment_author} '.format(
56 comment_author=comment_author,
57 instance_title=mg_globals.app_config['html_title']) \
58 + _('commented on your post'),
59 rendered_email)
60
61
62 def add_media_to_collection(collection, media, note=None, commit=True):
63 collection_item = CollectionItem()
64 collection_item.collection = collection.id
65 collection_item.media_entry = media.id
66 if note:
67 collection_item.note = note
68 Session.add(collection_item)
69
70 collection.items = collection.items + 1
71 Session.add(collection)
72
73 media.collected = media.collected + 1
74 Session.add(media)
75
76 if commit:
77 Session.commit()