threads.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 announce_thread(user, thread):
  32. if thread.weight < 2:
  33. thread.weight = 2
  34. message = _("%(user)s changed thread to announcement.")
  35. record_event(user, thread, "star", message, {'user': user})
  36. thread.save(update_fields=['has_events', 'weight'])
  37. return True
  38. else:
  39. return False
  40. @atomic
  41. def pin_thread(user, thread):
  42. if thread.weight != 1:
  43. thread.weight = 1
  44. message = _("%(user)s pinned thread.")
  45. record_event(user, thread, "bookmark", message, {'user': user})
  46. thread.save(update_fields=['has_events', 'weight'])
  47. return True
  48. else:
  49. return False
  50. @atomic
  51. def reset_thread(user, thread):
  52. if thread.weight > 0:
  53. if thread.is_announcement:
  54. message = _("%(user)s withhold announcement.")
  55. if thread.is_pinned:
  56. message = _("%(user)s unpinned thread.")
  57. record_event(user, thread, "circle", message, {'user': user})
  58. thread.weight = 0
  59. thread.save(update_fields=['has_events', 'weight'])
  60. return True
  61. else:
  62. return False
  63. @atomic
  64. def move_thread(user, thread, new_forum):
  65. if thread.forum_id != new_forum.pk:
  66. message = _("%(user)s moved thread from %(forum)s.")
  67. record_event(user, thread, "arrow-right", message, {
  68. 'user': user,
  69. 'forum': thread.forum
  70. })
  71. thread.move(new_forum)
  72. thread.save(update_fields=['has_events', 'forum'])
  73. return True
  74. else:
  75. return False
  76. @atomic
  77. def merge_thread(user, thread, other_thread):
  78. message = _("%(user)s merged in %(thread)s.")
  79. record_event(user, thread, "arrow-right", message, {
  80. 'user': user,
  81. 'thread': other_thread.title
  82. })
  83. thread.merge(other_thread)
  84. thread.synchronize()
  85. thread.save()
  86. other_thread.delete()
  87. return True
  88. @atomic
  89. def approve_thread(user, thread):
  90. if thread.is_moderated:
  91. message = _("%(user)s approved thread.")
  92. record_event(user, thread, "check", message, {'user': user})
  93. thread.is_closed = False
  94. thread.first_post.is_moderated = False
  95. thread.first_post.save(update_fields=['is_moderated'])
  96. thread.synchronize()
  97. thread.save(update_fields=['has_events', 'is_moderated'])
  98. return True
  99. else:
  100. return False
  101. @atomic
  102. def open_thread(user, thread):
  103. if thread.is_closed:
  104. message = _("%(user)s opened thread.")
  105. record_event(user, thread, "unlock-alt", message, {'user': user})
  106. thread.is_closed = False
  107. thread.save(update_fields=['has_events', 'is_closed'])
  108. return True
  109. else:
  110. return False
  111. @atomic
  112. def close_thread(user, thread):
  113. if not thread.is_closed:
  114. message = _("%(user)s closed thread.")
  115. record_event(user, thread, "lock", message, {'user': user})
  116. thread.is_closed = True
  117. thread.save(update_fields=['has_events', 'is_closed'])
  118. return True
  119. else:
  120. return False
  121. @atomic
  122. def unhide_thread(user, thread):
  123. if thread.is_hidden:
  124. message = _("%(user)s made thread visible.")
  125. record_event(user, thread, "eye", message, {'user': user})
  126. thread.first_post.is_hidden = False
  127. thread.first_post.save(update_fields=['is_hidden'])
  128. thread.is_hidden = False
  129. thread.save(update_fields=['has_events', 'is_hidden'])
  130. thread.synchronize()
  131. thread.save()
  132. return True
  133. else:
  134. return False
  135. @atomic
  136. def hide_thread(user, thread):
  137. if not thread.is_hidden:
  138. message = _("%(user)s hid thread.")
  139. record_event(user, thread, "eye-slash", message, {'user': user})
  140. thread.is_hidden = True
  141. thread.save(update_fields=['has_events', 'is_hidden'])
  142. return True
  143. else:
  144. return False
  145. @atomic
  146. def delete_thread(user, thread):
  147. thread.delete()
  148. return True