jumps.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from django.utils.translation import ugettext as _
  2. from misago.acl.exceptions import ACLError403, ACLError404
  3. from misago.models import User
  4. from misago.apps.threadtype.jumps import *
  5. from misago.apps.privatethreads.mixins import TypeMixin
  6. class LastReplyView(LastReplyBaseView, TypeMixin):
  7. pass
  8. class FindReplyView(FindReplyBaseView, TypeMixin):
  9. pass
  10. class NewReplyView(NewReplyBaseView, TypeMixin):
  11. pass
  12. class ShowHiddenRepliesView(ShowHiddenRepliesBaseView, TypeMixin):
  13. pass
  14. class WatchThreadView(WatchThreadBaseView, TypeMixin):
  15. pass
  16. class WatchEmailThreadView(WatchEmailThreadBaseView, TypeMixin):
  17. pass
  18. class UnwatchThreadView(UnwatchThreadBaseView, TypeMixin):
  19. pass
  20. class UnwatchEmailThreadView(UnwatchEmailThreadBaseView, TypeMixin):
  21. pass
  22. class UpvotePostView(UpvotePostBaseView, TypeMixin):
  23. pass
  24. class DownvotePostView(DownvotePostBaseView, TypeMixin):
  25. pass
  26. class InviteUserView(JumpView, TypeMixin):
  27. def make_jump(self):
  28. username = self.request.POST.get('username', '').strip()
  29. if not username:
  30. self.request.messages.set_flash(Message(_('You have to enter name of user you want to invite to thread.')), 'error', 'threads')
  31. return self.retreat_redirect()
  32. try:
  33. user = User.objects.get(username=username)
  34. acl = user.acl(self.request)
  35. if user in self.thread.participants.all():
  36. if user.pk == self.request.user.pk:
  37. self.request.messages.set_flash(Message(_('You cannot add yourself to this thread.')), 'error', 'threads')
  38. else:
  39. self.request.messages.set_flash(Message(_('%(user)s is already participating in this thread.') % {'user': user.username}), 'info', 'threads')
  40. if not acl.private_threads.can_participate():
  41. self.request.messages.set_flash(Message(_('%(user)s cannot participate in private threads.') % {'user': user.username}), 'info', 'threads')
  42. else:
  43. self.thread.participants.add(user)
  44. self.thread.last_post.set_checkpoint(self.request, 'invited', user)
  45. self.thread.last_post.save(force_update=True)
  46. self.request.messages.set_flash(Message(_('%(user)s has been added to this thread.') % {'user': user.username}), 'success', 'threads')
  47. return self.retreat_redirect()
  48. except User.DoesNotExist:
  49. self.request.messages.set_flash(Message(_('User with requested username could not be found.')), 'error', 'threads')
  50. return self.retreat_redirect()
  51. class RemoveUserView(JumpView, TypeMixin):
  52. def make_jump(self):
  53. target_user = int(self.request.POST.get('user', 0))
  54. if (not (self.request.user.pk == self.thread.start_poster_id or
  55. self.request.acl.private_threads.is_mod()) and
  56. target_user != self.request.user.pk):
  57. raise ACLError403(_("You don't have permission to remove discussion participants."))
  58. try:
  59. user = self.thread.participants.get(id=target_user)
  60. self.thread.participants.remove(user)
  61. self.thread.threadread_set.filter(id=user.pk).delete()
  62. self.thread.watchedthread_set.filter(id=user.pk).delete()
  63. # If there are no more participants in thread, remove it
  64. if self.thread.participants.count() == 0:
  65. self.thread.delete()
  66. self.request.messages.set_flash(Message(_('Thread has been deleted because last participant left it.')), 'info', 'threads')
  67. return self.threads_list_redirect()
  68. # Nope, see if we removed ourselves
  69. if user.pk == self.request.user.pk:
  70. self.thread.last_post.set_checkpoint(self.request, 'left')
  71. self.thread.last_post.save(force_update=True)
  72. self.request.messages.set_flash(Message(_('You have left the "%(thread)s" thread.') % {'thread': self.thread.name}), 'info', 'threads')
  73. return self.threads_list_redirect()
  74. # Nope, somebody else removed user
  75. self.thread.last_post.set_checkpoint(self.request, 'removed', user)
  76. self.thread.last_post.save(force_update=True)
  77. self.request.messages.set_flash(Message(_('Selected participant was removed from thread.')), 'info', 'threads')
  78. return self.retreat_redirect()
  79. except User.DoesNotExist:
  80. self.request.messages.set_flash(Message(_('Requested thread participant does not exist.')), 'error', 'threads')
  81. return self.retreat_redirect()