tools.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. import six
  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 _
  24. def take_punitive_actions(request, form, report, user):
  25. message_body = ''
  26. # The bulk of this action is running through all of the different
  27. # punitive actions that a moderator could take.
  28. if u'takeaway' in form.action_to_resolve.data:
  29. for privilege_name in form.take_away_privileges.data:
  30. take_away_privileges(user.username, privilege_name)
  31. form.resolution_content.data += \
  32. _(u"\n{mod} took away {user}\'s {privilege} privileges.").format(
  33. mod=request.user.username,
  34. user=user.username,
  35. privilege=privilege_name)
  36. # If the moderator elects to ban the user, a new instance of user_ban
  37. # will be created.
  38. if u'userban' in form.action_to_resolve.data:
  39. user_ban = ban_user(form.targeted_user.data,
  40. expiration_date=form.user_banned_until.data,
  41. reason=form.why_user_was_banned.data)
  42. Session.add(user_ban)
  43. form.resolution_content.data += \
  44. _(u"\n{mod} banned user {user} {expiration_date}.").format(
  45. mod=request.user.username,
  46. user=user.username,
  47. expiration_date = (
  48. _("until {date}").format(date=form.user_banned_until.data)
  49. if form.user_banned_until.data
  50. else _("indefinitely")
  51. )
  52. )
  53. # If the moderator elects to send a warning message. An email will be
  54. # sent to the email address given at sign up
  55. if u'sendmessage' in form.action_to_resolve.data:
  56. message_body = form.message_to_user.data
  57. form.resolution_content.data += \
  58. _(u"\n{mod} sent a warning email to the {user}.").format(
  59. mod=request.user.username,
  60. user=user.username)
  61. if u'delete' in form.action_to_resolve.data and \
  62. report.is_comment_report():
  63. deleted_comment = report.comment
  64. Session.delete(deleted_comment)
  65. form.resolution_content.data += \
  66. _(u"\n{mod} deleted the comment.").format(
  67. mod=request.user.username)
  68. elif u'delete' in form.action_to_resolve.data and \
  69. report.is_media_entry_report():
  70. deleted_media = report.media_entry
  71. deleted_media.delete()
  72. form.resolution_content.data += \
  73. _(u"\n{mod} deleted the media entry.").format(
  74. mod=request.user.username)
  75. report.archive(
  76. resolver_id=request.user.id,
  77. resolved=datetime.now(),
  78. result=form.resolution_content.data)
  79. Session.add(report)
  80. Session.commit()
  81. if message_body:
  82. send_email(
  83. mg_globals.app_config['email_sender_address'],
  84. [user.email],
  85. _('Warning from')+ '- {moderator} '.format(
  86. moderator=request.user.username),
  87. message_body)
  88. return redirect(
  89. request,
  90. 'mediagoblin.moderation.users_detail',
  91. user=user.username)
  92. def take_away_privileges(user,*privileges):
  93. """
  94. Take away all of the privileges passed as arguments.
  95. :param user A Unicode object representing the target user's
  96. User.username value.
  97. :param privileges A variable number of Unicode objects describing
  98. the privileges being taken away.
  99. :returns True If ALL of the privileges were taken away
  100. successfully.
  101. :returns False If ANY of the privileges were not taken away
  102. successfully. This means the user did not have
  103. (one of) the privilege(s) to begin with.
  104. """
  105. if len(privileges) == 1:
  106. privilege = Privilege.query.filter(
  107. Privilege.privilege_name==privileges[0]).first()
  108. user = User.query.filter(
  109. User.username==user).first()
  110. if privilege in user.all_privileges:
  111. user.all_privileges.remove(privilege)
  112. return True
  113. return False
  114. elif len(privileges) > 1:
  115. return (take_away_privileges(user, privileges[0]) and \
  116. take_away_privileges(user, *privileges[1:]))
  117. def give_privileges(user,*privileges):
  118. """
  119. Take away all of the privileges passed as arguments.
  120. :param user A Unicode object representing the target user's
  121. User.username value.
  122. :param privileges A variable number of Unicode objects describing
  123. the privileges being granted.
  124. :returns True If ALL of the privileges were granted successf-
  125. -ully.
  126. :returns False If ANY of the privileges were not granted succ-
  127. essfully. This means the user already had (one
  128. of) the privilege(s) to begin with.
  129. """
  130. if len(privileges) == 1:
  131. privilege = Privilege.query.filter(
  132. Privilege.privilege_name==privileges[0]).first()
  133. user = User.query.filter(
  134. User.username==user).first()
  135. if privilege not in user.all_privileges:
  136. user.all_privileges.append(privilege)
  137. return True
  138. return False
  139. elif len(privileges) > 1:
  140. return (give_privileges(user, privileges[0]) and \
  141. give_privileges(user, *privileges[1:]))
  142. def ban_user(user_id, expiration_date=None, reason=None):
  143. """
  144. This function is used to ban a user. If the user is already banned, the
  145. function returns False. If the user is not already banned, this function
  146. bans the user using the arguments to build a new UserBan object.
  147. :returns False if the user is already banned and the ban is not updated
  148. :returns UserBan object if there is a new ban that was created.
  149. """
  150. user_ban =UserBan.query.filter(
  151. UserBan.user_id==user_id)
  152. if user_ban.count():
  153. return False
  154. new_user_ban = UserBan(
  155. user_id=user_id,
  156. expiration_date=expiration_date,
  157. reason=reason)
  158. return new_user_ban
  159. def unban_user(user_id):
  160. """
  161. This function is used to unban a user. If the user is not currently banned,
  162. nothing happens.
  163. :returns True if the operation was completed successfully and the user
  164. has been unbanned
  165. :returns False if the user was never banned.
  166. """
  167. user_ban = UserBan.query.filter(
  168. UserBan.user_id==user_id)
  169. if user_ban.count() == 0:
  170. return False
  171. user_ban.first().delete()
  172. return True
  173. def parse_report_panel_settings(form):
  174. """
  175. This function parses the url arguments to which are used to filter reports
  176. in the reports panel view. More filters can be added to make a usuable
  177. search function.
  178. :returns A dictionary of sqlalchemy-usable filters.
  179. """
  180. filters = {}
  181. if form.validate():
  182. filters['reported_user_id'] = form.reported_user.data
  183. filters['reporter_id'] = form.reporter.data
  184. filters = dict((k, v)
  185. for k, v in six.iteritems(filters) if v)
  186. return filters