threads.py 3.2 KB

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