Whew. This is a big update. I did some significant keeping work. I moved all of
[mediagoblin:mediagoblin.git] / mediagoblin / moderation / views.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 werkzeug.exceptions import Forbidden
18
19 from mediagoblin.db.models import (MediaEntry, User, MediaComment, \
20                                    CommentReport, ReportBase, Privilege, \
21                                    UserBan)
22 from mediagoblin.decorators import (require_admin_or_moderator_login, \
23                                     active_user_from_url)
24 from mediagoblin.tools.response import render_to_response, redirect
25 from mediagoblin.moderation import forms as moderation_forms
26 from datetime import datetime
27
28 @require_admin_or_moderator_login
29 def moderation_media_processing_panel(request):
30     '''
31     Show the global media processing panel for this instance
32     '''
33     processing_entries = MediaEntry.query.filter_by(state = u'processing').\
34         order_by(MediaEntry.created.desc())
35
36     # Get media entries which have failed to process
37     failed_entries = MediaEntry.query.filter_by(state = u'failed').\
38         order_by(MediaEntry.created.desc())
39
40     processed_entries = MediaEntry.query.filter_by(state = u'processed').\
41         order_by(MediaEntry.created.desc()).limit(10)
42
43     # Render to response
44     return render_to_response(
45         request,
46         'mediagoblin/moderation/media_panel.html',
47         {'processing_entries': processing_entries,
48          'failed_entries': failed_entries,
49          'processed_entries': processed_entries})
50
51 @require_admin_or_moderator_login
52 def moderation_users_panel(request):
53     '''
54     Show the global panel for monitoring users in this instance
55     '''
56     user_list = User.query
57
58     return render_to_response(
59         request,
60         'mediagoblin/moderation/user_panel.html',
61         {'user_list': user_list})
62
63 @require_admin_or_moderator_login
64 def moderation_users_detail(request):
65     '''
66     Shows details about a particular user.
67     '''
68     user = User.query.filter_by(username=request.matchdict['user']).first()
69     active_reports = user.reports_filed_on.filter(
70         ReportBase.resolved==None).limit(5)
71     closed_reports = user.reports_filed_on.filter(
72         ReportBase.resolved!=None).all()
73     privileges = Privilege.query
74
75     return render_to_response(
76         request,
77         'mediagoblin/moderation/user.html',
78         {'user':user,
79          'privileges':privileges,
80          'reports':active_reports})
81
82 @require_admin_or_moderator_login
83 def moderation_reports_panel(request):
84     '''
85     Show the global panel for monitoring reports filed against comments or 
86         media entries for this instance.
87     '''
88     report_list = ReportBase.query.filter(
89         ReportBase.resolved==None).order_by(
90         ReportBase.created.desc()).limit(10)
91     closed_report_list = ReportBase.query.filter(
92         ReportBase.resolved!=None).order_by(
93         ReportBase.created.desc()).limit(10)
94
95     # Render to response
96     return render_to_response(
97         request,
98         'mediagoblin/moderation/report_panel.html',
99         {'report_list':report_list,
100          'closed_report_list':closed_report_list})
101
102 @require_admin_or_moderator_login
103 def moderation_reports_detail(request):
104     """
105     This is the page an admin or moderator goes to see the details of a report.
106     The report can be resolved or unresolved. This is also the page that a mod-
107     erator would go to to take an action to resolve a report.
108     """
109     form = moderation_forms.ReportResolutionForm(request.form)
110     report = ReportBase.query.get(request.matchdict['report_id'])
111
112     if request.method == "POST" and form.validate():
113         user = User.query.get(form.targeted_user.data)
114         if form.action_to_resolve.data == u'takeaway':
115             if report.discriminator == u'comment_report':
116                 privilege = Privilege.one({'privilege_name':u'commenter'})
117                 form.resolution_content.data += \
118                     u"<br>%s took away %s\'s commenting privileges" % (
119                         request.user.username,
120                         user.username)
121             else:
122                 privilege = Privilege.one({'privilege_name':u'uploader'})
123                 form.resolution_content.data += \
124                     u"<br>%s took away %s\'s media uploading privileges" % (
125                         request.user.username,
126                         user.username)
127             user.all_privileges.remove(privilege)
128             user.save()
129             report.result = form.resolution_content.data
130             report.resolved = datetime.now()
131             report.save()
132             
133         elif form.action_to_resolve.data == u'userban':
134             reason = form.resolution_content.data + \
135                 "<br>"+request.user.username
136             user_ban = UserBan(
137                 user_id=form.targeted_user.data,
138                 expiration_date=form.user_banned_until.data,
139                 reason= form.resolution_content.data)
140             user_ban.save()
141             if not form.user_banned_until == "":
142                 form.resolution_content.data += \
143                     u"<br>%s banned user %s until %s." % (
144                     request.user.username,
145                     user.username,
146                     form.user_banned_until.data)
147             else:
148                 form.resolution_content.data += \
149                     u"<br>%s banned user %s indefinitely." % (
150                     request.user.username,
151                     user.username,
152                     form.user_banned_until.data)
153
154             report.result = form.resolution_content.data
155             report.resolved = datetime.now()
156             report.save()
157
158         else:
159             pass
160
161         return redirect(
162             request,
163             'mediagoblin.moderation.users_detail',
164             user=user.username)
165
166     if report.discriminator == 'comment_report':
167         comment = MediaComment.query.get(report.comment_id)
168         media_entry = None
169     elif report.discriminator == 'media_report':
170         media_entry = MediaEntry.query.get(report.media_entry_id)
171         comment = None
172
173     form.targeted_user.data = report.reported_user_id
174
175     return render_to_response(
176         request,
177         'mediagoblin/moderation/report.html',
178         {'report':report,
179          'media_entry':media_entry,
180          'comment':comment,
181          'form':form})
182
183 @require_admin_or_moderator_login
184 @active_user_from_url
185 def give_or_take_away_privilege(request, url_user):
186     '''
187     A form action to give or take away a particular privilege from a user
188     '''
189     form = moderation_forms.PrivilegeAddRemoveForm(request.form)
190     if request.method == "POST" and form.validate():
191         privilege = Privilege.one({'privilege_name':form.privilege_name.data})
192         if privilege in url_user.all_privileges is True:
193             url_user.all_privileges.remove(privilege)
194         else:      
195             url_user.all_privileges.append(privilege)
196         url_user.save()
197         return redirect(
198             request,
199             'mediagoblin.moderation.users_detail',
200             user=url_user.username)