threads.py 4.7 KB

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