threads.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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(request, thread, 'moved', {
  64. 'from_category': {
  65. 'name': from_category.name,
  66. 'url': from_category.get_absolute_url(),
  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(update_fields=[
  125. 'is_hidden',
  126. 'hidden_by',
  127. 'hidden_by_name',
  128. 'hidden_by_slug',
  129. 'hidden_on',
  130. ])
  131. thread.is_hidden = True
  132. record_event(request, thread, 'hid')
  133. return True
  134. else:
  135. return False
  136. @atomic
  137. def delete_thread(request, thread):
  138. thread.delete()
  139. return True