threads.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. thread.merge(other_thread)
  79. thread.synchornize()
  80. thread.save()
  81. other_thread.delete()
  82. return True
  83. @atomic
  84. def approve_thread(user, thread):
  85. if thread.is_moderated:
  86. message = _("%(user)s approved thread.")
  87. record_event(user, thread, "check", message, {'user': user})
  88. thread.is_closed = False
  89. thread.first_post.is_moderated = False
  90. thread.first_post.save(update_fields=['has_events', 'is_moderated'])
  91. thread.synchornize()
  92. thread.save()
  93. return True
  94. else:
  95. return False
  96. @atomic
  97. def open_thread(user, thread):
  98. if thread.is_closed:
  99. message = _("%(user)s opened thread.")
  100. record_event(user, thread, "unlock-alt", message, {'user': user})
  101. thread.is_closed = False
  102. thread.save(update_fields=['has_events', 'is_closed'])
  103. return True
  104. else:
  105. return False
  106. @atomic
  107. def close_thread(user, thread):
  108. if not thread.is_closed:
  109. message = _("%(user)s closed thread.")
  110. record_event(user, thread, "lock", message, {'user': user})
  111. thread.is_closed = True
  112. thread.save(update_fields=['has_events', 'is_closed'])
  113. return True
  114. else:
  115. return False
  116. @atomic
  117. def unhide_thread(user, thread):
  118. if thread.is_hidden:
  119. message = _("%(user)s made thread visible.")
  120. record_event(user, thread, "eye", message, {'user': user})
  121. thread.first_post.is_hidden = False
  122. thread.first_post.save(update_fields=['is_hidden'])
  123. thread.is_hidden = False
  124. thread.save(update_fields=['has_events', 'is_hidden'])
  125. thread.synchronize()
  126. thread.save()
  127. return True
  128. else:
  129. return False
  130. @atomic
  131. def hide_thread(user, thread):
  132. if not thread.is_hidden:
  133. message = _("%(user)s hid thread.")
  134. record_event(user, thread, "eye-slash", message, {'user': user})
  135. thread.is_hidden = True
  136. thread.save(update_fields=['has_events', 'is_hidden'])
  137. return True
  138. else:
  139. return False
  140. @atomic
  141. def delete_thread(user, thread):
  142. thread.delete()
  143. return True