1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
19 from mediagoblin
import mg_globals
20 from mediagoblin
.db
.models
import User
, Privilege
, UserBan
21 from mediagoblin
.db
.base
import Session
22 from mediagoblin
.tools
.mail
import send_email
23 from mediagoblin
.tools
.response
import redirect
24 from datetime
import datetime
25 from mediagoblin
.tools
.translate
import lazy_pass_to_ugettext
as _
28 def take_punitive_actions(request
, form
, report
, user
):
31 # The bulk of this action is running through all of the different
32 # punitive actions that a moderator could take.
33 if u
'takeaway' in form
.action_to_resolve
.data
:
34 for privilege_name
in form
.take_away_privileges
.data
:
35 take_away_privileges(user
.username
, privilege_name
)
36 form
.resolution_content
.data
+= \
37 _(u
"\n{mod} took away {user}\'s {privilege} privileges.").format(
38 mod
=request
.user
.username
,
40 privilege
=privilege_name
)
42 # If the moderator elects to ban the user, a new instance of user_ban
44 if u
'userban' in form
.action_to_resolve
.data
:
45 user_ban
= ban_user(form
.targeted_user
.data
,
46 expiration_date
=form
.user_banned_until
.data
,
47 reason
=form
.why_user_was_banned
.data
)
49 form
.resolution_content
.data
+= \
50 _(u
"\n{mod} banned user {user} {expiration_date}.").format(
51 mod
=request
.user
.username
,
54 _("until {date}").format(date
=form
.user_banned_until
.data
)
55 if form
.user_banned_until
.data
56 else _("indefinitely")
60 # If the moderator elects to send a warning message. An email will be
61 # sent to the email address given at sign up
62 if u
'sendmessage' in form
.action_to_resolve
.data
:
63 message_body
= form
.message_to_user
.data
64 form
.resolution_content
.data
+= \
65 _(u
"\n{mod} sent a warning email to the {user}.").format(
66 mod
=request
.user
.username
,
69 if u
'delete' in form
.action_to_resolve
.data
and \
70 report
.is_comment_report():
71 deleted_comment
= report
.comment
72 Session
.delete(deleted_comment
)
73 form
.resolution_content
.data
+= \
74 _(u
"\n{mod} deleted the comment.").format(
75 mod
=request
.user
.username
)
76 elif u
'delete' in form
.action_to_resolve
.data
and \
77 report
.is_media_entry_report():
78 deleted_media
= report
.media_entry
79 deleted_media
.delete()
80 form
.resolution_content
.data
+= \
81 _(u
"\n{mod} deleted the media entry.").format(
82 mod
=request
.user
.username
)
84 resolver_id
=request
.user
.id,
85 resolved
=datetime
.now(),
86 result
=form
.resolution_content
.data
)
92 mg_globals
.app_config
['email_sender_address'],
94 _('Warning from')+ '- {moderator} '.format(
95 moderator
=request
.user
.username
),
100 'mediagoblin.moderation.users_detail',
104 def take_away_privileges(user
,*privileges
):
106 Take away all of the privileges passed as arguments.
108 :param user A Unicode object representing the target user's
111 :param privileges A variable number of Unicode objects describing
112 the privileges being taken away.
115 :returns True If ALL of the privileges were taken away
118 :returns False If ANY of the privileges were not taken away
119 successfully. This means the user did not have
120 (one of) the privilege(s) to begin with.
122 if len(privileges
) == 1:
123 privilege
= Privilege
.query
.filter(
124 Privilege
.privilege_name
==privileges
[0]).first()
125 user
= User
.query
.filter(
126 User
.username
==user
).first()
127 if privilege
in user
.all_privileges
:
128 user
.all_privileges
.remove(privilege
)
132 elif len(privileges
) > 1:
133 return (take_away_privileges(user
, privileges
[0]) and \
134 take_away_privileges(user
, *privileges
[1:]))
136 def give_privileges(user
,*privileges
):
138 Take away all of the privileges passed as arguments.
140 :param user A Unicode object representing the target user's
143 :param privileges A variable number of Unicode objects describing
144 the privileges being granted.
147 :returns True If ALL of the privileges were granted successf-
150 :returns False If ANY of the privileges were not granted succ-
151 essfully. This means the user already had (one
152 of) the privilege(s) to begin with.
154 if len(privileges
) == 1:
155 privilege
= Privilege
.query
.filter(
156 Privilege
.privilege_name
==privileges
[0]).first()
157 user
= User
.query
.filter(
158 User
.username
==user
).first()
159 if privilege
not in user
.all_privileges
:
160 user
.all_privileges
.append(privilege
)
164 elif len(privileges
) > 1:
165 return (give_privileges(user
, privileges
[0]) and \
166 give_privileges(user
, *privileges
[1:]))
168 def ban_user(user_id
, expiration_date
=None, reason
=None):
170 This function is used to ban a user. If the user is already banned, the
171 function returns False. If the user is not already banned, this function
172 bans the user using the arguments to build a new UserBan object.
174 :returns False if the user is already banned and the ban is not updated
175 :returns UserBan object if there is a new ban that was created.
177 user_ban
=UserBan
.query
.filter(
178 UserBan
.user_id
==user_id
)
181 new_user_ban
= UserBan(
183 expiration_date
=expiration_date
,
187 def unban_user(user_id
):
189 This function is used to unban a user. If the user is not currently banned,
192 :returns True if the operation was completed successfully and the user
194 :returns False if the user was never banned.
196 user_ban
= UserBan
.query
.filter(
197 UserBan
.user_id
==user_id
)
198 if user_ban
.count() == 0:
200 user_ban
.first().delete()
203 def parse_report_panel_settings(form
):
205 This function parses the url arguments to which are used to filter reports
206 in the reports panel view. More filters can be added to make a usuable
209 :returns A dictionary of sqlalchemy-usable filters.
214 filters
['reported_user_id'] = form
.reported_user
.data
215 filters
['reporter_id'] = form
.reporter
.data
217 filters
= dict((k
, v
)
218 for k
, v
in six
.iteritems(filters
) if v
)