threads.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. from django.db import transaction
  2. from django.utils import timezone
  3. from misago.threads.events import record_event
  4. __all__ = [
  5. "change_thread_title",
  6. "pin_thread_globally",
  7. "pin_thread_locally",
  8. "unpin_thread",
  9. "move_thread",
  10. "merge_thread",
  11. "approve_thread",
  12. "open_thread",
  13. "close_thread",
  14. "unhide_thread",
  15. "hide_thread",
  16. "delete_thread",
  17. ]
  18. @transaction.atomic
  19. def change_thread_title(request, thread, new_title):
  20. if thread.title != new_title:
  21. old_title = thread.title
  22. thread.set_title(new_title)
  23. thread.save(update_fields=["title", "slug"])
  24. thread.first_post.set_search_document(thread.title)
  25. thread.first_post.save(update_fields=["search_document"])
  26. thread.first_post.update_search_vector()
  27. thread.first_post.save(update_fields=["search_vector"])
  28. record_event(request, thread, "changed_title", {"old_title": old_title})
  29. return True
  30. else:
  31. return False
  32. @transaction.atomic
  33. def pin_thread_globally(request, thread):
  34. if thread.weight != 2:
  35. thread.weight = 2
  36. record_event(request, thread, "pinned_globally")
  37. return True
  38. else:
  39. return False
  40. @transaction.atomic
  41. def pin_thread_locally(request, thread):
  42. if thread.weight != 1:
  43. thread.weight = 1
  44. record_event(request, thread, "pinned_locally")
  45. return True
  46. else:
  47. return False
  48. @transaction.atomic
  49. def unpin_thread(request, thread):
  50. if thread.weight:
  51. thread.weight = 0
  52. record_event(request, thread, "unpinned")
  53. return True
  54. else:
  55. return False
  56. @transaction.atomic
  57. def move_thread(request, thread, new_category):
  58. if thread.category_id != new_category.pk:
  59. from_category = thread.category
  60. thread.move(new_category)
  61. record_event(
  62. request,
  63. thread,
  64. "moved",
  65. {
  66. "from_category": {
  67. "name": from_category.name,
  68. "url": from_category.get_absolute_url(),
  69. }
  70. },
  71. )
  72. return True
  73. else:
  74. return False
  75. @transaction.atomic
  76. def merge_thread(request, thread, other_thread):
  77. thread.merge(other_thread)
  78. other_thread.delete()
  79. record_event(request, thread, "merged", {"merged_thread": other_thread.title})
  80. return True
  81. @transaction.atomic
  82. def approve_thread(request, thread):
  83. if thread.is_unapproved:
  84. thread.first_post.is_unapproved = False
  85. thread.first_post.save(update_fields=["is_unapproved"])
  86. thread.is_unapproved = False
  87. unapproved_post_qs = thread.post_set.filter(is_unapproved=True)
  88. thread.has_unapproved_posts = unapproved_post_qs.exists()
  89. record_event(request, thread, "approved")
  90. return True
  91. else:
  92. return False
  93. @transaction.atomic
  94. def open_thread(request, thread):
  95. if thread.is_closed:
  96. thread.is_closed = False
  97. record_event(request, thread, "opened")
  98. return True
  99. else:
  100. return False
  101. @transaction.atomic
  102. def close_thread(request, thread):
  103. if not thread.is_closed:
  104. thread.is_closed = True
  105. record_event(request, thread, "closed")
  106. return True
  107. else:
  108. return False
  109. @transaction.atomic
  110. def unhide_thread(request, thread):
  111. if thread.is_hidden:
  112. thread.first_post.is_hidden = False
  113. thread.first_post.save(update_fields=["is_hidden"])
  114. thread.is_hidden = False
  115. record_event(request, thread, "unhid")
  116. if thread.pk == thread.category.last_thread_id:
  117. thread.category.synchronize()
  118. thread.category.save()
  119. return True
  120. else:
  121. return False
  122. @transaction.atomic
  123. def hide_thread(request, thread):
  124. if not thread.is_hidden:
  125. thread.first_post.is_hidden = True
  126. thread.first_post.hidden_by = request.user
  127. thread.first_post.hidden_by_name = request.user.username
  128. thread.first_post.hidden_by_slug = request.user.slug
  129. thread.first_post.hidden_on = timezone.now()
  130. thread.first_post.save(
  131. update_fields=[
  132. "is_hidden",
  133. "hidden_by",
  134. "hidden_by_name",
  135. "hidden_by_slug",
  136. "hidden_on",
  137. ]
  138. )
  139. thread.is_hidden = True
  140. record_event(request, thread, "hid")
  141. if thread.pk == thread.category.last_thread_id:
  142. thread.category.synchronize()
  143. thread.category.save()
  144. return True
  145. else:
  146. return False
  147. @transaction.atomic
  148. def delete_thread(request, thread):
  149. thread.delete()
  150. thread.category.synchronize()
  151. thread.category.save()
  152. return True