This was a big commit! I included lots of documentation below, but generally I
[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 take_away_privileges(user.username, 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_name)
40
41 # If the moderator elects to ban the user, a new instance of user_ban
42 # will be created.
43 if u'userban' in form.action_to_resolve.data:
44 reason = form.resolution_content.data + \
45 "<br>"+request.user.username
46 user_ban = UserBan(
47 user_id=form.targeted_user.data,
48 expiration_date=form.user_banned_until.data,
49 reason= form.why_user_was_banned.data
50 )
51 Session.add(user_ban)
52
53 if form.user_banned_until.data is not None:
54 form.resolution_content.data += \
55 u"<br>%s banned user %s until %s." % (
56 request.user.username,
57 user.username,
58 form.user_banned_until.data)
59 else:
60 form.resolution_content.data += \
61 u"<br>%s banned user %s indefinitely." % (
62 request.user.username,
63 user.username)
64
65 # If the moderator elects to send a warning message. An email will be
66 # sent to the email address given at sign up
67 if u'sendmessage' in form.action_to_resolve.data:
68 message_body = form.message_to_user.data
69 form.resolution_content.data += \
70 u"<br>%s sent a warning email to the offender." % (
71 request.user.username)
72
73 archive = ArchivedReport(
74 reporter_id=report.reporter_id,
75 report_content=report.report_content,
76 reported_user_id=report.reported_user_id,
77 created=report.created,
78 resolved=datetime.now(),
79 resolver_id=request.user.id
80 )
81
82 if u'delete' in form.action_to_resolve.data and \
83 report.is_comment_report():
84 deleted_comment = report.comment
85 Session.delete(deleted_comment)
86 form.resolution_content.data += \
87 u"<br>%s deleted the comment." % (
88 request.user.username)
89 elif u'delete' in form.action_to_resolve.data and \
90 report.is_media_entry_report():
91 deleted_media = report.media_entry
92 Session.delete(deleted_media)
93 form.resolution_content.data += \
94 u"<br>%s deleted the media entry." % (
95 request.user.username)
96
97 # If the moderator didn't delete the content we then attach the
98 # content to the archived report. We also have to actively delete the
99 # old report, since it won't be deleted by cascading.
100 elif report.is_comment_report():
101 archive.comment_id = report.comment_id
102 Session.delete(report)
103 elif report.is_media_entry_report():
104 archive.media_entry_id = report.media_entry.id
105 Session.delete(report)
106
107
108 archive.result=form.resolution_content.data
109 Session.add(archive)
110 Session.commit()
111 if message_body:
112 send_email(
113 mg_globals.app_config['email_sender_address'],
114 [user.email],
115 _('Warning from')+ '- {moderator} '.format(
116 moderator=request.user.username),
117 message_body)
118
119 return redirect(
120 request,
121 'mediagoblin.moderation.users_detail',
122 user=user.username)
123 except:
124 #TODO make a more effective and specific try except statement. To account for
125 # incorrect value addition my moderators
126 print sys.exc_info()[0]
127 print sys.exc_info()[1]
128 traceback.print_tb(sys.exc_info()[2])
129 Session.rollback()
130 return redirect(
131 request,
132 'mediagoblin.moderation.reports_detail',
133 report_id=report.id)
134
135 def take_away_privileges(user,*privileges):
136 if len(privileges) == 1:
137 privilege = Privilege.query.filter(
138 Privilege.privilege_name==privileges[0]).first()
139 user = User.query.filter(
140 User.username==user).first()
141 if privilege in user.all_privileges:
142 user.all_privileges.remove(privilege)
143 return True
144 return False
145
146 elif len(privileges) > 1:
147 return (take_away_privileges(user, privileges[0]) and \
148 take_away_privileges(user, *privileges[1:]))
149
150 def give_privileges(user,*privileges):
151 if len(privileges) == 1:
152 privilege = Privilege.query.filter(
153 Privilege.privilege_name==privileges[0]).first()
154 user = User.query.filter(
155 User.username==user).first()
156 if privilege not in user.all_privileges:
157 user.all_privileges.append(privilege)
158 return True
159 return False
160
161 elif len(privileges) > 1:
162 return (give_privileges(user, privileges[0]) and \
163 give_privileges(user, *privileges[1:]))
164