views.py 8.1 KB

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