threads.py 3.8 KB

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