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/>.
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 _
25 def take_punitive_actions(request
, form
, report
, user
):
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
,
37 privilege
=privilege_name
)
39 # If the moderator elects to ban the user, a new instance of user_ban
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
)
46 form
.resolution_content
.data
+= \
47 u
"\n{mod} banned user {user} {expiration_date}.".format(
48 mod
=request
.user
.username
,
51 "until {date}".format(date
=form
.user_banned_until
.data
)
52 if form
.user_banned_until
.data
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
,
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
)
81 resolver_id
=request
.user
.id,
82 resolved
=datetime
.now(),
83 result
=form
.resolution_content
.data
)
89 mg_globals
.app_config
['email_sender_address'],
91 _('Warning from')+ '- {moderator} '.format(
92 moderator
=request
.user
.username
),
97 'mediagoblin.moderation.users_detail',
101 def take_away_privileges(user
,*privileges
):
103 Take away all of the privileges passed as arguments.
105 :param user A Unicode object representing the target user's
108 :param privileges A variable number of Unicode objects describing
109 the privileges being taken away.
112 :returns True If ALL of the privileges were taken away
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.
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
)
129 elif len(privileges
) > 1:
130 return (take_away_privileges(user
, privileges
[0]) and \
131 take_away_privileges(user
, *privileges
[1:]))
133 def give_privileges(user
,*privileges
):
135 Take away all of the privileges passed as arguments.
137 :param user A Unicode object representing the target user's
140 :param privileges A variable number of Unicode objects describing
141 the privileges being granted.
144 :returns True If ALL of the privileges were granted successf-
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.
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
)
161 elif len(privileges
) > 1:
162 return (give_privileges(user
, privileges
[0]) and \
163 give_privileges(user
, *privileges
[1:]))
165 def ban_user(user_id
, expiration_date
=None, reason
=None):
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.
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.
174 user_ban
=UserBan
.query
.filter(
175 UserBan
.user_id
==user_id
)
178 new_user_ban
= UserBan(
180 expiration_date
=expiration_date
,
184 def unban_user(user_id
):
186 This function is used to unban a user. If the user is not currently banned,
189 :returns True if the operation was completed successfully and the user
191 :returns False if the user was never banned.
193 user_ban
= UserBan
.query
.filter(
194 UserBan
.user_id
==user_id
)
195 if user_ban
.count() == 0:
197 user_ban
.first().delete()
200 def parse_report_panel_settings(form
):
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
206 :returns A dictionary of sqlalchemy-usable filters.
211 filters
['reported_user_id'] = form
.reported_user
.data
212 filters
['reporter_id'] = form
.reporter
.data
214 filters
= dict((k
, v
)
215 for k
, v
in filters
.iteritems() if v
)