threads.py 4.4 KB

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