Commit | Line | Data |
---|---|---|
9412fffe | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
252eaf21 DM |
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 | ||
252eaf21 | 17 | from mediagoblin import mg_globals |
6bea8a90 | 18 | from mediagoblin.db.base import Session |
64a456a4 JT |
19 | from mediagoblin.db.models import CollectionItem, Report, TextComment, \ |
20 | MediaEntry | |
b694c3de OHO |
21 | from mediagoblin.tools.mail import send_email |
22 | from mediagoblin.tools.pluginapi import hook_runall | |
23 | from mediagoblin.tools.template import render_template | |
24 | from mediagoblin.tools.translate import pass_to_ugettext as _ | |
252eaf21 | 25 | |
00722c99 | 26 | |
252eaf21 DM |
27 | def send_comment_email(user, comment, media, request): |
28 | """ | |
29 | Sends comment email to user when a comment is made on their media. | |
30 | ||
31 | Args: | |
32 | - user: the user object to whom the email is sent | |
33 | - comment: the comment object referencing user's media | |
34 | - media: the media object the comment is about | |
35 | - request: the request | |
36 | """ | |
37 | ||
fa72e516 DM |
38 | comment_url = request.urlgen( |
39 | 'mediagoblin.user_pages.media_home.view_comment', | |
5c2b8486 | 40 | comment=comment.id, |
0f3bf8d4 | 41 | user=media.get_actor.username, |
00722c99 JW |
42 | media=media.slug_or_id, |
43 | qualified=True) + '#comment' | |
252eaf21 | 44 | |
0f3bf8d4 | 45 | comment_author = comment.get_actor.username |
252eaf21 DM |
46 | |
47 | rendered_email = render_template( | |
48 | request, 'mediagoblin/user_pages/comment_email.txt', | |
00722c99 JW |
49 | {'username': user.username, |
50 | 'comment_author': comment_author, | |
51 | 'comment_content': comment.content, | |
52 | 'comment_url': comment_url}) | |
252eaf21 DM |
53 | |
54 | send_email( | |
55 | mg_globals.app_config['email_sender_address'], | |
56 | [user.email], | |
00722c99 JW |
57 | '{instance_title} - {comment_author} '.format( |
58 | comment_author=comment_author, | |
59 | instance_title=mg_globals.app_config['html_title']) \ | |
60 | + _('commented on your post'), | |
252eaf21 | 61 | rendered_email) |
6bea8a90 E |
62 | |
63 | ||
64 | def add_media_to_collection(collection, media, note=None, commit=True): | |
65 | collection_item = CollectionItem() | |
66 | collection_item.collection = collection.id | |
0f3bf8d4 | 67 | collection_item.get_object = media |
6bea8a90 E |
68 | if note: |
69 | collection_item.note = note | |
70 | Session.add(collection_item) | |
71 | ||
0f3bf8d4 | 72 | collection.num_items = collection.num_items + 1 |
6bea8a90 | 73 | Session.add(collection) |
6bea8a90 E |
74 | Session.add(media) |
75 | ||
b694c3de OHO |
76 | hook_runall('collection_add_media', collection_item=collection_item) |
77 | ||
6bea8a90 E |
78 | if commit: |
79 | Session.commit() | |
30a9fe7c | 80 | |
b694c3de | 81 | |
f26c21cd | 82 | def build_report_object(report_form, media_entry=None, comment=None): |
30a9fe7c | 83 | """ |
dfd66b78 | 84 | This function is used to convert a form object (from a User filing a |
64a456a4 | 85 | report) into a Report. |
30a9fe7c | 86 | |
8e91df87 | 87 | :param report_form A MediaReportForm or a CommentReportForm object |
88 | with valid information from a POST request. | |
89 | :param media_entry A MediaEntry object. The MediaEntry being repo- | |
64a456a4 JT |
90 | -rted by a Report. |
91 | :param comment A Comment object. The Comment being | |
92 | reported by a Report. | |
93 | ||
94 | :returns A Report object if a valid MediaReportForm is | |
95 | passed as kwarg media_entry. This Report has | |
8e91df87 | 96 | not been saved. |
8e91df87 | 97 | :returns None if the form_dict is invalid. |
30a9fe7c | 98 | """ |
64a456a4 | 99 | report_object = Report() |
f26c21cd | 100 | if report_form.validate() and comment is not None: |
64a456a4 JT |
101 | report_object.obj = comment.comment() |
102 | report_object.reported_user_id = TextComment.query.get( | |
0f3bf8d4 | 103 | comment.id).get_actor.id |
f26c21cd | 104 | elif report_form.validate() and media_entry is not None: |
64a456a4 | 105 | report_object.obj = media_entry |
f26c21cd | 106 | report_object.reported_user_id = MediaEntry.query.get( |
0f3bf8d4 | 107 | media_entry.id).get_actor.id |
30a9fe7c | 108 | else: |
109 | return None | |
9b8ef022 | 110 | |
f26c21cd | 111 | report_object.report_content = report_form.report_reason.data |
112 | report_object.reporter_id = report_form.reporter_id.data | |
113 | return report_object |