threads.py 4.4 KB

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