trac#687: Add unit tests for `redirect` and `redirect_obj`.
[larjonas-mediagoblin.git] / mediagoblin / moderation / views.py
blobfdcbf051428fd17bfdf929959beea07c3e066175
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/>.
18 from mediagoblin.db.models import (MediaEntry, User, Report, Privilege,
19 UserBan, LocalUser)
20 from mediagoblin.decorators import (require_admin_or_moderator_login,
21 active_user_from_url, user_has_privilege,
22 allow_reporting)
23 from mediagoblin.tools.response import render_to_response, redirect
24 from mediagoblin.moderation import forms as moderation_forms
25 from mediagoblin.moderation.tools import (take_punitive_actions, \
26 take_away_privileges, give_privileges, ban_user, unban_user, \
27 parse_report_panel_settings)
28 from math import ceil
30 @require_admin_or_moderator_login
31 def moderation_media_processing_panel(request):
32 '''
33 Show the global media processing panel for this instance
34 '''
35 processing_entries = MediaEntry.query.filter_by(state = u'processing').\
36 order_by(MediaEntry.created.desc())
38 # Get media entries which have failed to process
39 failed_entries = MediaEntry.query.filter_by(state = u'failed').\
40 order_by(MediaEntry.created.desc())
42 processed_entries = MediaEntry.query.filter_by(state = u'processed').\
43 order_by(MediaEntry.created.desc()).limit(10)
45 # Render to response
46 return render_to_response(
47 request,
48 'mediagoblin/moderation/media_panel.html',
49 {'processing_entries': processing_entries,
50 'failed_entries': failed_entries,
51 'processed_entries': processed_entries})
53 @require_admin_or_moderator_login
54 def moderation_users_panel(request):
55 '''
56 Show the global panel for monitoring users in this instance
57 '''
58 current_page = 1
59 if len(request.args) > 0:
60 form = moderation_forms.UserPanelSortingForm(request.args)
61 if form.validate():
62 current_page = form.p.data or 1
64 all_user_list = User.query
65 user_list = all_user_list.order_by(
66 User.created.desc()).offset(
67 (current_page-1)*10).limit(10)
68 last_page = int(ceil(all_user_list.count()/10.))
70 return render_to_response(
71 request,
72 'mediagoblin/moderation/user_panel.html',
73 {'user_list': user_list,
74 'current_page':current_page,
75 'last_page':last_page})
77 @require_admin_or_moderator_login
78 def moderation_users_detail(request):
79 '''
80 Shows details about a particular user.
81 '''
82 user = LocalUser.query.filter(
83 LocalUser.username==request.matchdict['user']
84 ).first()
85 active_reports = user.reports_filed_on.filter(
86 Report.resolved==None).limit(5)
87 closed_reports = user.reports_filed_on.filter(
88 Report.resolved!=None).all()
89 privileges = Privilege.query
90 user_banned = UserBan.query.get(user.id)
91 ban_form = moderation_forms.BanForm()
93 return render_to_response(
94 request,
95 'mediagoblin/moderation/user.html',
96 {'user':user,
97 'privileges': privileges,
98 'reports':active_reports,
99 'user_banned':user_banned,
100 'ban_form':ban_form})
102 @require_admin_or_moderator_login
103 @allow_reporting
104 def moderation_reports_panel(request):
106 Show the global panel for monitoring reports filed against comments or
107 media entries for this instance.
109 filters = []
110 active_settings, closed_settings = {'current_page':1}, {'current_page':1}
112 if len(request.args) > 0:
113 form = moderation_forms.ReportPanelSortingForm(request.args)
114 if form.validate():
115 filters = parse_report_panel_settings(form)
116 active_settings['current_page'] = form.active_p.data or 1
117 closed_settings['current_page'] = form.closed_p.data or 1
118 filters = [
119 getattr(Report,key)==val
120 for key,val in filters.viewitems()]
122 all_active = Report.query.filter(
123 Report.resolved==None).filter(
124 *filters)
125 all_closed = Report.query.filter(
126 Report.resolved!=None).filter(
127 *filters)
129 # report_list and closed_report_list are the two lists of up to 10
130 # items which are actually passed to the user in this request
131 report_list = all_active.order_by(
132 Report.created.desc()).offset(
133 (active_settings['current_page']-1)*10).limit(10)
134 closed_report_list = all_closed.order_by(
135 Report.created.desc()).offset(
136 (closed_settings['current_page']-1)*10).limit(10)
138 active_settings['last_page'] = int(ceil(all_active.count()/10.))
139 closed_settings['last_page'] = int(ceil(all_closed.count()/10.))
140 # Render to response
141 return render_to_response(
142 request,
143 'mediagoblin/moderation/report_panel.html',
144 {'report_list':report_list,
145 'closed_report_list':closed_report_list,
146 'active_settings':active_settings,
147 'closed_settings':closed_settings})
149 @require_admin_or_moderator_login
150 @allow_reporting
151 def moderation_reports_detail(request):
153 This is the page an admin or moderator goes to see the details of a report.
154 The report can be resolved or unresolved. This is also the page that a mod-
155 erator would go to to take an action to resolve a report.
157 form = moderation_forms.ReportResolutionForm(request.form)
158 report = Report.query.get(request.matchdict['report_id'])
160 form.take_away_privileges.choices = [
161 (s.privilege_name,s.privilege_name.title()) \
162 for s in report.reported_user.all_privileges
165 if request.method == "POST" and form.validate() and not (
166 not request.user.has_privilege(u'admin') and
167 report.reported_user.has_privilege(u'admin')):
169 user = User.query.get(form.targeted_user.data)
170 return take_punitive_actions(request, form, report, user)
173 form.targeted_user.data = report.reported_user_id
175 return render_to_response(
176 request,
177 'mediagoblin/moderation/report.html',
178 {'report':report,
179 'form':form})
181 @user_has_privilege(u'admin')
182 @active_user_from_url
183 def give_or_take_away_privilege(request, url_user):
185 A form action to give or take away a particular privilege from a user.
186 Can only be used by an admin.
188 form = moderation_forms.PrivilegeAddRemoveForm(request.form)
189 if request.method == "POST" and form.validate():
190 privilege = Privilege.query.filter(
191 Privilege.privilege_name==form.privilege_name.data).one()
192 if not take_away_privileges(
193 url_user.username, form.privilege_name.data):
195 give_privileges(url_user.username, form.privilege_name.data)
196 url_user.save()
198 return redirect(
199 request,
200 'mediagoblin.moderation.users_detail',
201 user=url_user.username)
203 @user_has_privilege(u'admin')
204 @active_user_from_url
205 def ban_or_unban(request, url_user):
207 A page to ban or unban a user. Only can be used by an admin.
209 form = moderation_forms.BanForm(request.form)
210 if request.method == "POST" and form.validate():
211 already_banned = unban_user(url_user.id)
212 same_as_requesting_user = (request.user.id == url_user.id)
213 if not already_banned and not same_as_requesting_user:
214 user_ban = ban_user(url_user.id,
215 expiration_date = form.user_banned_until.data,
216 reason = form.why_user_was_banned.data)
217 user_ban.save()
218 return redirect(
219 request,
220 'mediagoblin.moderation.users_detail',
221 user=url_user.username)