threads.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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', {'old_title': old_title})
  29. return True
  30. else:
  31. return False
  32. @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. @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. @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. @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, thread, 'moved', {
  63. 'from_category': {
  64. 'name': from_category.name,
  65. 'url': from_category.get_absolute_url(),
  66. }
  67. }
  68. )
  69. return True
  70. else:
  71. return False
  72. @atomic
  73. def merge_thread(request, thread, other_thread):
  74. thread.merge(other_thread)
  75. other_thread.delete()
  76. record_event(request, thread, 'merged', {
  77. 'merged_thread': other_thread.title,
  78. })
  79. return True
  80. @atomic
  81. def approve_thread(request, thread):
  82. if thread.is_unapproved:
  83. thread.is_unapproved = False
  84. thread.first_post.is_unapproved = False
  85. thread.first_post.save(update_fields=['is_unapproved'])
  86. record_event(request, thread, 'approved')
  87. return True
  88. else:
  89. return False
  90. @atomic
  91. def open_thread(request, thread):
  92. if thread.is_closed:
  93. thread.is_closed = False
  94. record_event(request, thread, 'opened')
  95. return True
  96. else:
  97. return False
  98. @atomic
  99. def close_thread(request, thread):
  100. if not thread.is_closed:
  101. thread.is_closed = True
  102. record_event(request, thread, 'closed')
  103. return True
  104. else:
  105. return False
  106. @atomic
  107. def unhide_thread(request, thread):
  108. if thread.is_hidden:
  109. thread.first_post.is_hidden = False
  110. thread.first_post.save(update_fields=['is_hidden'])
  111. thread.is_hidden = False
  112. record_event(request, thread, 'unhid')
  113. return True
  114. else:
  115. return False
  116. @atomic
  117. def hide_thread(request, thread):
  118. if not thread.is_hidden:
  119. thread.first_post.is_hidden = True
  120. thread.first_post.hidden_by = request.user
  121. thread.first_post.hidden_by_name = request.user.username
  122. thread.first_post.hidden_by_slug = request.user.slug
  123. thread.first_post.hidden_on = timezone.now()
  124. thread.first_post.save(
  125. update_fields=[
  126. 'is_hidden',
  127. 'hidden_by',
  128. 'hidden_by_name',
  129. 'hidden_by_slug',
  130. 'hidden_on',
  131. ]
  132. )
  133. thread.is_hidden = True
  134. record_event(request, thread, 'hid')
  135. return True
  136. else:
  137. return False
  138. @atomic
  139. def delete_thread(request, thread):
  140. thread.delete()
  141. return True