threads.py 3.7 KB

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