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
, 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 _
26 def take_punitive_actions(request
, form
, report
, user
):
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
,
41 # If the moderator elects to ban the user, a new instance of user_ban
43 if u
'userban' in form
.action_to_resolve
.data
:
44 reason
= form
.resolution_content
.data
+ \
45 "<br>"+request
.user
.username
46 user_ban
= ban_user(form
.targeted_user
.data
,
47 expiration_date
=form
.user_banned_until
.data
,
48 reason
=form
.why_user_was_banned
.data
)
51 if form
.user_banned_until
.data
is not None:
52 form
.resolution_content
.data
+= \
53 u
"<br>%s banned user %s until %s." % (
54 request
.user
.username
,
56 form
.user_banned_until
.data
)
58 form
.resolution_content
.data
+= \
59 u
"<br>%s banned user %s indefinitely." % (
60 request
.user
.username
,
63 # If the moderator elects to send a warning message. An email will be
64 # sent to the email address given at sign up
65 if u
'sendmessage' in form
.action_to_resolve
.data
:
66 message_body
= form
.message_to_user
.data
67 form
.resolution_content
.data
+= \
68 u
"<br>%s sent a warning email to the offender." % (
69 request
.user
.username
)
71 archive
= ArchivedReport(
72 reporter_id
=report
.reporter_id
,
73 report_content
=report
.report_content
,
74 reported_user_id
=report
.reported_user_id
,
75 created
=report
.created
,
76 resolved
=datetime
.now(),
77 resolver_id
=request
.user
.id
80 if u
'delete' in form
.action_to_resolve
.data
and \
81 report
.is_comment_report():
82 deleted_comment
= report
.comment
83 Session
.delete(deleted_comment
)
84 form
.resolution_content
.data
+= \
85 u
"<br>%s deleted the comment." % (
86 request
.user
.username
)
87 elif u
'delete' in form
.action_to_resolve
.data
and \
88 report
.is_media_entry_report():
89 deleted_media
= report
.media_entry
90 Session
.delete(deleted_media
)
91 form
.resolution_content
.data
+= \
92 u
"<br>%s deleted the media entry." % (
93 request
.user
.username
)
95 # If the moderator didn't delete the content we then attach the
96 # content to the archived report. We also have to actively delete the
97 # old report, since it won't be deleted by cascading.
98 elif report
.is_comment_report():
99 archive
.comment_id
= report
.comment_id
100 Session
.delete(report
)
101 elif report
.is_media_entry_report():
102 archive
.media_entry_id
= report
.media_entry
.id
103 Session
.delete(report
)
106 archive
.result
=form
.resolution_content
.data
111 mg_globals
.app_config
['email_sender_address'],
113 _('Warning from')+ '- {moderator} '.format(
114 moderator
=request
.user
.username
),
119 'mediagoblin.moderation.users_detail',
122 #TODO make a more effective and specific try except statement. To account for
123 # incorrect value addition my moderators
124 print sys
.exc_info()[0]
125 print sys
.exc_info()[1]
126 traceback
.print_tb(sys
.exc_info()[2])
130 'mediagoblin.moderation.reports_detail',
133 def take_away_privileges(user
,*privileges
):
134 if len(privileges
) == 1:
135 privilege
= Privilege
.query
.filter(
136 Privilege
.privilege_name
==privileges
[0]).first()
137 user
= User
.query
.filter(
138 User
.username
==user
).first()
139 if privilege
in user
.all_privileges
:
140 user
.all_privileges
.remove(privilege
)
144 elif len(privileges
) > 1:
145 return (take_away_privileges(user
, privileges
[0]) and \
146 take_away_privileges(user
, *privileges
[1:]))
148 def give_privileges(user
,*privileges
):
149 if len(privileges
) == 1:
150 privilege
= Privilege
.query
.filter(
151 Privilege
.privilege_name
==privileges
[0]).first()
152 user
= User
.query
.filter(
153 User
.username
==user
).first()
154 if privilege
not in user
.all_privileges
:
155 user
.all_privileges
.append(privilege
)
159 elif len(privileges
) > 1:
160 return (give_privileges(user
, privileges
[0]) and \
161 give_privileges(user
, *privileges
[1:]))
163 def ban_user(user_id
, expiration_date
=None, reason
=None):
165 This function is used to ban a user. If the user is already banned, the
166 function returns False. If the user is not already banned, this function
167 bans the user using the arguments to build a new UserBan object.
169 :returns False if the user is already banned and the ban is not updated
170 :returns UserBan object if there is a new ban that was created.
172 user_ban
=UserBan
.query
.filter(
173 UserBan
.user_id
==user_id
)
176 new_user_ban
= UserBan(
178 expiration_date
=expiration_date
,
182 def unban_user(user_id
):
184 This function is used to unban a user. If the user is not currently banned,
187 :returns True if the operation was completed successfully and the user
189 :returns False if the user was never banned.
191 user_ban
= UserBan
.query
.filter(
192 UserBan
.user_id
==user_id
)
193 if user_ban
.count() == 0:
195 user_ban
.first().delete()