threads.py 3.5 KB

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