This is a very small commit. All that I have done here is to clean up my code
[mediagoblin.git] / mediagoblin / moderation / tools.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 import mg_globals
18 from mediagoblin.db.models import User, Privilege, ArchivedReport, UserBan
19 from mediagoblin.db.base import Session
20 from mediagoblin.tools.mail import send_email
21 from mediagoblin.tools.response import redirect
22 from datetime import datetime
23 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
24 import sys, traceback
25
26 def take_punitive_actions(request, form, report, user):
27 message_body =''
28 try:
29
30 # The bulk of this action is running through all of the different
31 # punitive actions that a moderator could take.
32 if u'takeaway' in form.action_to_resolve.data:
33 for privilege_name in form.take_away_privileges.data:
34 privilege = Privilege.one({u'privilege_name':privilege_name})
35 form.resolution_content.data += \
36 u"<br>%s took away %s\'s %s privileges" % (
37 request.user.username,
38 user.username,
39 privilege.privilege_name)
40 user.all_privileges.remove(privilege)
41
42 # If the moderator elects to ban the user, a new instance of user_ban
43 # will be created.
44 if u'userban' in form.action_to_resolve.data:
45 reason = form.resolution_content.data + \
46 "<br>"+request.user.username
47 user_ban = UserBan(
48 user_id=form.targeted_user.data,
49 expiration_date=form.user_banned_until.data,
50 reason= form.why_user_was_banned.data
51 )
52 Session.add(user_ban)
53
54 if form.user_banned_until.data is not None:
55 form.resolution_content.data += \
56 u"<br>%s banned user %s until %s." % (
57 request.user.username,
58 user.username,
59 form.user_banned_until.data)
60 else:
61 form.resolution_content.data += \
62 u"<br>%s banned user %s indefinitely." % (
63 request.user.username,
64 user.username)
65
66 # If the moderator elects to send a warning message. An email will be
67 # sent to the email address given at sign up
68 if u'sendmessage' in form.action_to_resolve.data:
69 message_body = form.message_to_user.data
70 form.resolution_content.data += \
71 u"<br>%s sent a warning email to the offender." % (
72 request.user.username)
73
74 archive = ArchivedReport(
75 reporter_id=report.reporter_id,
76 report_content=report.report_content,
77 reported_user_id=report.reported_user_id,
78 created=report.created,
79 resolved=datetime.now(),
80 resolver_id=request.user.id
81 )
82
83 if u'delete' in form.action_to_resolve.data and \
84 report.is_comment_report():
85 deleted_comment = report.comment
86 Session.delete(deleted_comment)
87 form.resolution_content.data += \
88 u"<br>%s deleted the comment" % (
89 request.user.username)
90 elif u'delete' in form.action_to_resolve.data and \
91 report.is_media_entry_report():
92 deleted_media = report.media_entry
93 Session.delete(deleted_media)
94 form.resolution_content.data += \
95 u"<br>%s deleted the media entry" % (
96 request.user.username)
97
98 # If the moderator didn't delete the content we then attach the
99 # content to the archived report. We also have to actively delete the
100 # old report, since it won't be deleted by cascading.
101 elif report.is_comment_report():
102 archive.comment_id = report.comment_id
103 Session.delete(report)
104 elif report.is_media_entry_report():
105 archive.media_entry_id = report.media_entry.id
106 Session.delete(report)
107
108
109 archive.result=form.resolution_content.data
110 Session.add(archive)
111 Session.commit()
112 if message_body:
113 send_email(
114 mg_globals.app_config['email_sender_address'],
115 [user.email],
116 _('Warning from')+ '- {moderator} '.format(
117 moderator=request.user.username),
118 message_body)
119
120 return redirect(
121 request,
122 'mediagoblin.moderation.users_detail',
123 user=user.username)
124 except:
125 #TODO make a more effective and specific try except statement. To account for
126 # incorrect value addition my moderators
127 print sys.exc_info()[0]
128 print sys.exc_info()[1]
129 traceback.print_tb(sys.exc_info()[2])
130 Session.rollback()
131 return redirect(
132 request,
133 'mediagoblin.moderation.reports_detail',
134 report_id=report.id)