undo that "update"
[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, 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
25 def take_punitive_actions(request, form, report, user):
26 message_body =''
27
28 # The bulk of this action is running through all of the different
29 # punitive actions that a moderator could take.
30 if u'takeaway' in form.action_to_resolve.data:
31 for privilege_name in form.take_away_privileges.data:
32 take_away_privileges(user.username, privilege_name)
33 form.resolution_content.data += \
34 u"\n{mod} took away {user}\'s {privilege} privileges.".format(
35 mod=request.user.username,
36 user=user.username,
37 privilege=privilege_name)
38
39 # If the moderator elects to ban the user, a new instance of user_ban
40 # will be created.
41 if u'userban' in form.action_to_resolve.data:
42 user_ban = ban_user(form.targeted_user.data,
43 expiration_date=form.user_banned_until.data,
44 reason=form.why_user_was_banned.data)
45 Session.add(user_ban)
46 form.resolution_content.data += \
47 u"\n{mod} banned user {user} {expiration_date}.".format(
48 mod=request.user.username,
49 user=user.username,
50 expiration_date = (
51 "until {date}".format(date=form.user_banned_until.data)
52 if form.user_banned_until.data
53 else "indefinitely"
54 )
55 )
56
57 # If the moderator elects to send a warning message. An email will be
58 # sent to the email address given at sign up
59 if u'sendmessage' in form.action_to_resolve.data:
60 message_body = form.message_to_user.data
61 form.resolution_content.data += \
62 u"\n{mod} sent a warning email to the {user}.".format(
63 mod=request.user.username,
64 user=user.username)
65
66 if u'delete' in form.action_to_resolve.data and \
67 report.is_comment_report():
68 deleted_comment = report.comment
69 Session.delete(deleted_comment)
70 form.resolution_content.data += \
71 u"\n{mod} deleted the comment.".format(
72 mod=request.user.username)
73 elif u'delete' in form.action_to_resolve.data and \
74 report.is_media_entry_report():
75 deleted_media = report.media_entry
76 deleted_media.delete()
77 form.resolution_content.data += \
78 u"\n{mod} deleted the media entry.".format(
79 mod=request.user.username)
80 report.archive(
81 resolver_id=request.user.id,
82 resolved=datetime.now(),
83 result=form.resolution_content.data)
84
85 Session.add(report)
86 Session.commit()
87 if message_body:
88 send_email(
89 mg_globals.app_config['email_sender_address'],
90 [user.email],
91 _('Warning from')+ '- {moderator} '.format(
92 moderator=request.user.username),
93 message_body)
94
95 return redirect(
96 request,
97 'mediagoblin.moderation.users_detail',
98 user=user.username)
99
100
101 def take_away_privileges(user,*privileges):
102 """
103 Take away all of the privileges passed as arguments.
104
105 :param user A Unicode object representing the target user's
106 User.username value.
107
108 :param privileges A variable number of Unicode objects describing
109 the privileges being taken away.
110
111
112 :returns True If ALL of the privileges were taken away
113 successfully.
114
115 :returns False If ANY of the privileges were not taken away
116 successfully. This means the user did not have
117 (one of) the privilege(s) to begin with.
118 """
119 if len(privileges) == 1:
120 privilege = Privilege.query.filter(
121 Privilege.privilege_name==privileges[0]).first()
122 user = User.query.filter(
123 User.username==user).first()
124 if privilege in user.all_privileges:
125 user.all_privileges.remove(privilege)
126 return True
127 return False
128
129 elif len(privileges) > 1:
130 return (take_away_privileges(user, privileges[0]) and \
131 take_away_privileges(user, *privileges[1:]))
132
133 def give_privileges(user,*privileges):
134 """
135 Take away all of the privileges passed as arguments.
136
137 :param user A Unicode object representing the target user's
138 User.username value.
139
140 :param privileges A variable number of Unicode objects describing
141 the privileges being granted.
142
143
144 :returns True If ALL of the privileges were granted successf-
145 -ully.
146
147 :returns False If ANY of the privileges were not granted succ-
148 essfully. This means the user already had (one
149 of) the privilege(s) to begin with.
150 """
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
165 def ban_user(user_id, expiration_date=None, reason=None):
166 """
167 This function is used to ban a user. If the user is already banned, the
168 function returns False. If the user is not already banned, this function
169 bans the user using the arguments to build a new UserBan object.
170
171 :returns False if the user is already banned and the ban is not updated
172 :returns UserBan object if there is a new ban that was created.
173 """
174 user_ban =UserBan.query.filter(
175 UserBan.user_id==user_id)
176 if user_ban.count():
177 return False
178 new_user_ban = UserBan(
179 user_id=user_id,
180 expiration_date=expiration_date,
181 reason=reason)
182 return new_user_ban
183
184 def unban_user(user_id):
185 """
186 This function is used to unban a user. If the user is not currently banned,
187 nothing happens.
188
189 :returns True if the operation was completed successfully and the user
190 has been unbanned
191 :returns False if the user was never banned.
192 """
193 user_ban = UserBan.query.filter(
194 UserBan.user_id==user_id)
195 if user_ban.count() == 0:
196 return False
197 user_ban.first().delete()
198 return True
199
200 def parse_report_panel_settings(form):
201 """
202 This function parses the url arguments to which are used to filter reports
203 in the reports panel view. More filters can be added to make a usuable
204 search function.
205
206 :returns A dictionary of sqlalchemy-usable filters.
207 """
208 filters = {}
209
210 if form.validate():
211 filters['reported_user_id'] = form.reported_user.data
212 filters['reporter_id'] = form.reporter.data
213
214 filters = dict((k, v)
215 for k, v in filters.iteritems() if v)
216
217 return filters