threads.py 4.3 KB

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