threads.py 4.0 KB

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