thread.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from django.utils.translation import ugettext as _
  2. from misago import messages
  3. from misago.apps.threadtype.thread import ThreadBaseView, ThreadModeration, PostsModeration
  4. from misago.models import Forum, Thread, ThreadPrefix
  5. from misago.apps.threads.forms import PollVoteForm
  6. from misago.apps.threads.mixins import TypeMixin
  7. class ThreadView(ThreadBaseView, ThreadModeration, PostsModeration, TypeMixin):
  8. def template_vars(self, context):
  9. prefixes = ThreadPrefix.objects.forum_prefixes(self.forum)
  10. if self.thread.prefix_id in prefixes:
  11. context['prefix'] = prefixes[self.thread.prefix_id]
  12. self.add_poll(context)
  13. return super(ThreadView, self).template_vars(context)
  14. def add_poll(self, context):
  15. context['poll'] = None
  16. context['poll_form'] = None
  17. if self.thread.has_poll:
  18. context['poll'] = self.thread.poll
  19. self.thread.poll.message = self.request.messages.get_message('poll_%s' % self.thread.poll.pk)
  20. if self.request.user.is_authenticated():
  21. self.thread.poll.user_votes = [x.option_id for x in self.request.user.pollvote_set.filter(poll=self.thread.poll)]
  22. if self.request.acl.threads.can_vote_in_polls(self.forum, self.thread, self.thread.poll):
  23. context['poll_form'] = PollVoteForm(request=self.request, poll=self.thread.poll)
  24. def posts_actions(self):
  25. acl = self.request.acl.threads.get_role(self.thread.forum_id)
  26. actions = []
  27. try:
  28. if acl['can_approve'] and self.thread.replies_moderated > 0:
  29. actions.append(('accept', _('Accept posts')))
  30. if acl['can_move_threads_posts']:
  31. actions.append(('merge', _('Merge posts into one')))
  32. actions.append(('split', _('Split posts to new thread')))
  33. actions.append(('move', _('Move posts to other thread')))
  34. if acl['can_protect_posts']:
  35. actions.append(('protect', _('Protect posts')))
  36. actions.append(('unprotect', _('Remove posts protection')))
  37. if acl['can_delete_posts']:
  38. if self.thread.replies_deleted > 0:
  39. actions.append(('undelete', _('Restore posts')))
  40. actions.append(('soft', _('Hide posts')))
  41. if acl['can_delete_posts'] == 2:
  42. actions.append(('hard', _('Delete posts')))
  43. except KeyError:
  44. pass
  45. return actions
  46. def thread_actions(self):
  47. acl = self.request.acl.threads.get_role(self.thread.forum_id)
  48. actions = []
  49. try:
  50. if acl['can_approve'] and self.thread.moderated:
  51. actions.append(('accept', _('Accept this thread')))
  52. if acl['can_change_prefixes']:
  53. self.prefixes = ThreadPrefix.objects.forum_prefixes(self.forum)
  54. if self.thread.prefix_id:
  55. actions.append(('prefix:0', _('Remove prefix')))
  56. for prefix in self.prefixes.values():
  57. if prefix.pk != self.thread.prefix_id:
  58. actions.append(('prefix:%s' % prefix.pk, _('Change prefix to: %(prefix)s') % {'prefix': _(prefix.name)}))
  59. if acl['can_pin_threads'] == 2 and self.thread.weight < 2:
  60. actions.append(('annouce', _('Change this thread to announcement')))
  61. if acl['can_pin_threads'] > 0 and self.thread.weight != 1:
  62. actions.append(('sticky', _('Change this thread to sticky')))
  63. if acl['can_pin_threads'] > 0:
  64. if self.thread.weight == 2:
  65. actions.append(('normal', _('Change this thread to normal')))
  66. if self.thread.weight == 1:
  67. actions.append(('normal', _('Unpin this thread')))
  68. if acl['can_move_threads_posts']:
  69. actions.append(('move', _('Move this thread')))
  70. if acl['can_close_threads']:
  71. if self.thread.closed:
  72. actions.append(('open', _('Open this thread')))
  73. else:
  74. actions.append(('close', _('Close this thread')))
  75. if acl['can_delete_threads']:
  76. if self.thread.deleted:
  77. actions.append(('undelete', _('Restore this thread')))
  78. else:
  79. actions.append(('soft', _('Hide this thread')))
  80. if acl['can_delete_threads'] == 2:
  81. actions.append(('hard', _('Delete this thread')))
  82. except KeyError:
  83. pass
  84. return actions
  85. def thread_action_prefix(self, prefix):
  86. try:
  87. prefix = int(prefix)
  88. except TypeError:
  89. prefix = 0
  90. prefix = prefix or None
  91. if prefix:
  92. self._thread_action_set_prefix(self.prefixes[prefix])
  93. messages.success(self.request, _('Threads prefix has been changed to "%(name)s".') % {'name': _(self.prefixes[prefix].name)}, 'threads')
  94. else:
  95. self._thread_action_remove_prefix()
  96. messages.success(self.request, _('Thread prefix has been removed.'), 'threads')
  97. def _thread_action_set_prefix(self, prefix):
  98. self.thread.prefix_id = prefix.pk
  99. thread.set_checkpoint(self.request, 'changed_prefix', self.request.user, self.forum, extra=prefix.name)
  100. self.thread.save(force_update=True)
  101. def _thread_action_remove_prefix(self):
  102. self.thread.prefix_id = None
  103. thread.set_checkpoint(self.request, 'removed_prefix', self.request.user, self.forum)
  104. self.thread.save(force_update=True)