tools.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, LocalUser
  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.obj()
  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.obj()
  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 = LocalUser.query.filter(
  109. LocalUser.username==user
  110. ).first()
  111. if privilege in user.all_privileges:
  112. user.all_privileges.remove(privilege)
  113. return True
  114. return False
  115. elif len(privileges) > 1:
  116. return (take_away_privileges(user, privileges[0]) and \
  117. take_away_privileges(user, *privileges[1:]))
  118. def give_privileges(user,*privileges):
  119. """
  120. Take away all of the privileges passed as arguments.
  121. :param user A Unicode object representing the target user's
  122. User.username value.
  123. :param privileges A variable number of Unicode objects describing
  124. the privileges being granted.
  125. :returns True If ALL of the privileges were granted successf-
  126. -ully.
  127. :returns False If ANY of the privileges were not granted succ-
  128. essfully. This means the user already had (one
  129. of) the privilege(s) to begin with.
  130. """
  131. if len(privileges) == 1:
  132. privilege = Privilege.query.filter(
  133. Privilege.privilege_name==privileges[0]).first()
  134. user = LocalUser.query.filter(
  135. LocalUser.username==user
  136. ).first()
  137. if privilege not in user.all_privileges:
  138. user.all_privileges.append(privilege)
  139. return True
  140. return False
  141. elif len(privileges) > 1:
  142. return (give_privileges(user, privileges[0]) and \
  143. give_privileges(user, *privileges[1:]))
  144. def ban_user(user_id, expiration_date=None, reason=None):
  145. """
  146. This function is used to ban a user. If the user is already banned, the
  147. function returns False. If the user is not already banned, this function
  148. bans the user using the arguments to build a new UserBan object.
  149. :returns False if the user is already banned and the ban is not updated
  150. :returns UserBan object if there is a new ban that was created.
  151. """
  152. user_ban =UserBan.query.filter(
  153. UserBan.user_id==user_id)
  154. if user_ban.count():
  155. return False
  156. new_user_ban = UserBan(
  157. user_id=user_id,
  158. expiration_date=expiration_date,
  159. reason=reason)
  160. return new_user_ban
  161. def unban_user(user_id):
  162. """
  163. This function is used to unban a user. If the user is not currently banned,
  164. nothing happens.
  165. :returns True if the operation was completed successfully and the user
  166. has been unbanned
  167. :returns False if the user was never banned.
  168. """
  169. user_ban = UserBan.query.filter(
  170. UserBan.user_id==user_id)
  171. if user_ban.count() == 0:
  172. return False
  173. user_ban.first().delete()
  174. return True
  175. def parse_report_panel_settings(form):
  176. """
  177. This function parses the url arguments to which are used to filter reports
  178. in the reports panel view. More filters can be added to make a usuable
  179. search function.
  180. :returns A dictionary of sqlalchemy-usable filters.
  181. """
  182. filters = {}
  183. if form.validate():
  184. filters['reported_user_id'] = form.reported_user.data
  185. filters['reporter_id'] = form.reporter.data
  186. filters = dict((k, v)
  187. for k, v in six.iteritems(filters) if v)
  188. return filters