threads.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from django.db.transaction import atomic
  2. from django.utils.translation import ugettext_lazy, ugettext as _
  3. from misago.threads.events import record_event
  4. @atomic
  5. def label_thread(user, thread, label):
  6. if not thread.label_id or thread.label_id != label.pk:
  7. if thread.label_id:
  8. message = _("%(user)s changed thread label to %(label)s.")
  9. else:
  10. message = _("%(user)s set thread label to %(label)s.")
  11. record_event(user, thread, "tag", message, {
  12. 'user': user,
  13. 'label': label.name
  14. })
  15. thread.label = label
  16. thread.save(update_fields=['has_events', 'label'])
  17. return True
  18. else:
  19. return False
  20. @atomic
  21. def unlabel_thread(user, thread):
  22. if thread.label_id:
  23. thread.label = None
  24. message = _("%(user)s removed thread label.")
  25. record_event(user, thread, "tag", message, {'user': user})
  26. thread.save(update_fields=['has_events', 'label'])
  27. return True
  28. else:
  29. return False
  30. @atomic
  31. def pin_thread(user, thread):
  32. if not thread.is_pinned:
  33. thread.is_pinned = True
  34. message = _("%(user)s pinned thread.")
  35. record_event(user, thread, "star", message, {'user': user})
  36. thread.save(update_fields=['has_events', 'is_pinned'])
  37. return True
  38. else:
  39. return False
  40. @atomic
  41. def unpin_thread(user, thread):
  42. if thread.is_pinned:
  43. message = _("%(user)s unpinned thread.")
  44. record_event(user, thread, "circle", message, {'user': user})
  45. thread.is_pinned = False
  46. thread.save(update_fields=['has_events', 'is_pinned'])
  47. return True
  48. else:
  49. return False
  50. @atomic
  51. def move_thread(user, thread, new_forum):
  52. if thread.forum_id != new_forum.pk:
  53. message = _("%(user)s moved thread from %(forum)s.")
  54. record_event(user, thread, "arrow-right", message, {
  55. 'user': user,
  56. 'forum': thread.forum
  57. })
  58. thread.move(new_forum)
  59. thread.save(update_fields=['has_events', 'forum'])
  60. return True
  61. else:
  62. return False
  63. @atomic
  64. def merge_thread(user, thread, other_thread):
  65. message = _("%(user)s merged in %(thread)s.")
  66. record_event(user, thread, "arrow-right", message, {
  67. 'user': user,
  68. 'thread': other_thread.title
  69. })
  70. thread.merge(other_thread)
  71. thread.synchronize()
  72. thread.save()
  73. other_thread.delete()
  74. return True
  75. @atomic
  76. def approve_thread(user, thread):
  77. if thread.is_moderated:
  78. message = _("%(user)s approved thread.")
  79. record_event(user, thread, "check", message, {'user': user})
  80. thread.is_closed = False
  81. thread.first_post.is_moderated = False
  82. thread.first_post.save(update_fields=['is_moderated'])
  83. thread.synchronize()
  84. thread.save(update_fields=['has_events', 'is_moderated'])
  85. return True
  86. else:
  87. return False
  88. @atomic
  89. def open_thread(user, thread):
  90. if thread.is_closed:
  91. message = _("%(user)s opened thread.")
  92. record_event(user, thread, "unlock-alt", message, {'user': user})
  93. thread.is_closed = False
  94. thread.save(update_fields=['has_events', 'is_closed'])
  95. return True
  96. else:
  97. return False
  98. @atomic
  99. def close_thread(user, thread):
  100. if not thread.is_closed:
  101. message = _("%(user)s closed thread.")
  102. record_event(user, thread, "lock", message, {'user': user})
  103. thread.is_closed = True
  104. thread.save(update_fields=['has_events', 'is_closed'])
  105. return True
  106. else:
  107. return False
  108. @atomic
  109. def unhide_thread(user, thread):
  110. if thread.is_hidden:
  111. message = _("%(user)s made thread visible.")
  112. record_event(user, thread, "eye", message, {'user': user})
  113. thread.first_post.is_hidden = False
  114. thread.first_post.save(update_fields=['is_hidden'])
  115. thread.is_hidden = False
  116. thread.save(update_fields=['has_events', 'is_hidden'])
  117. thread.synchronize()
  118. thread.save()
  119. return True
  120. else:
  121. return False
  122. @atomic
  123. def hide_thread(user, thread):
  124. if not thread.is_hidden:
  125. message = _("%(user)s hid thread.")
  126. record_event(user, thread, "eye-slash", message, {'user': user})
  127. thread.is_hidden = True
  128. thread.save(update_fields=['has_events', 'is_hidden'])
  129. return True
  130. else:
  131. return False
  132. @atomic
  133. def delete_thread(user, thread):
  134. thread.delete()
  135. return True