threads.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. from django.db.transaction import atomic
  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. @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. @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. @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. @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. @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. @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. @atomic
  83. def approve_thread(request, thread):
  84. if thread.is_unapproved:
  85. thread.is_unapproved = False
  86. thread.first_post.is_unapproved = False
  87. thread.first_post.save(update_fields=['is_unapproved'])
  88. record_event(request, thread, 'approved')
  89. return True
  90. else:
  91. return False
  92. @atomic
  93. def open_thread(request, thread):
  94. if thread.is_closed:
  95. thread.is_closed = False
  96. record_event(request, thread, 'opened')
  97. return True
  98. else:
  99. return False
  100. @atomic
  101. def close_thread(request, thread):
  102. if not thread.is_closed:
  103. thread.is_closed = True
  104. record_event(request, thread, 'closed')
  105. return True
  106. else:
  107. return False
  108. @atomic
  109. def unhide_thread(request, thread):
  110. if thread.is_hidden:
  111. thread.first_post.is_hidden = False
  112. thread.first_post.save(update_fields=['is_hidden'])
  113. thread.is_hidden = False
  114. record_event(request, thread, 'unhid')
  115. return True
  116. else:
  117. return False
  118. @atomic
  119. def hide_thread(request, thread):
  120. if not thread.is_hidden:
  121. thread.first_post.is_hidden = True
  122. thread.first_post.hidden_by = request.user
  123. thread.first_post.hidden_by_name = request.user.username
  124. thread.first_post.hidden_by_slug = request.user.slug
  125. thread.first_post.hidden_on = timezone.now()
  126. thread.first_post.save(
  127. update_fields=[
  128. 'is_hidden',
  129. 'hidden_by',
  130. 'hidden_by_name',
  131. 'hidden_by_slug',
  132. 'hidden_on',
  133. ]
  134. )
  135. thread.is_hidden = True
  136. record_event(request, thread, 'hid')
  137. return True
  138. else:
  139. return False
  140. @atomic
  141. def delete_thread(request, thread):
  142. thread.delete()
  143. return True