threads.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. @atomic
  6. def pin_thread_globally(user, thread):
  7. if thread.weight != 2:
  8. message = _("%(user)s pinned thread globally.")
  9. record_event(user, thread, "bookmark", message, {'user': user})
  10. thread.weight = 2
  11. thread.save(update_fields=['has_events', 'weight'])
  12. return True
  13. else:
  14. return False
  15. @atomic
  16. def pin_thread_locally(user, thread):
  17. if thread.weight != 1:
  18. message = _("%(user)s pinned thread locally.")
  19. record_event(user, thread, "bookmark", message, {'user': user})
  20. thread.weight = 1
  21. thread.save(update_fields=['has_events', 'weight'])
  22. return True
  23. else:
  24. return False
  25. @atomic
  26. def unpin_thread(user, thread):
  27. if thread.weight:
  28. message = _("%(user)s unpinned thread.")
  29. record_event(user, thread, "circle", message, {'user': user})
  30. thread.weight = 0
  31. thread.save(update_fields=['has_events', 'weight'])
  32. return True
  33. else:
  34. return False
  35. @atomic
  36. def move_thread(user, thread, new_category):
  37. if thread.category_id != new_category.pk:
  38. message = _("%(user)s moved thread from %(category)s.")
  39. record_event(user, thread, "arrow-right", message, {
  40. 'user': user,
  41. 'category': thread.category
  42. })
  43. thread.move(new_category)
  44. thread.save(update_fields=['has_events', 'category'])
  45. return True
  46. else:
  47. return False
  48. @atomic
  49. def merge_thread(user, thread, other_thread):
  50. message = _("%(user)s merged in %(thread)s.")
  51. record_event(user, thread, "arrow-right", message, {
  52. 'user': user,
  53. 'thread': other_thread.title
  54. })
  55. thread.merge(other_thread)
  56. other_thread.delete()
  57. return True
  58. @atomic
  59. def approve_thread(user, thread):
  60. if thread.is_moderated:
  61. message = _("%(user)s approved thread.")
  62. record_event(user, thread, "check", message, {'user': user})
  63. thread.is_closed = False
  64. thread.first_post.is_moderated = False
  65. thread.first_post.save(update_fields=['is_moderated'])
  66. thread.synchronize()
  67. thread.save(update_fields=['has_events', 'is_moderated'])
  68. return True
  69. else:
  70. return False
  71. @atomic
  72. def open_thread(user, thread):
  73. if thread.is_closed:
  74. message = _("%(user)s opened thread.")
  75. record_event(user, thread, "unlock-alt", message, {'user': user})
  76. thread.is_closed = False
  77. thread.save(update_fields=['has_events', 'is_closed'])
  78. return True
  79. else:
  80. return False
  81. @atomic
  82. def close_thread(user, thread):
  83. if not thread.is_closed:
  84. message = _("%(user)s closed thread.")
  85. record_event(user, thread, "lock", message, {'user': user})
  86. thread.is_closed = True
  87. thread.save(update_fields=['has_events', 'is_closed'])
  88. return True
  89. else:
  90. return False
  91. @atomic
  92. def unhide_thread(user, thread):
  93. if thread.is_hidden:
  94. message = _("%(user)s made thread visible.")
  95. record_event(user, thread, "eye", message, {'user': user})
  96. thread.first_post.is_hidden = False
  97. thread.first_post.save(update_fields=['is_hidden'])
  98. thread.is_hidden = False
  99. thread.save(update_fields=['has_events', 'is_hidden'])
  100. thread.synchronize()
  101. thread.save()
  102. return True
  103. else:
  104. return False
  105. @atomic
  106. def hide_thread(user, thread):
  107. if not thread.is_hidden:
  108. message = _("%(user)s hidden thread.")
  109. record_event(user, thread, "eye-slash", message, {'user': user})
  110. thread.first_post.is_hidden = True
  111. thread.first_post.hidden_by = user
  112. thread.first_post.hidden_by_name = user.username
  113. thread.first_post.hidden_by_slug = user.slug
  114. thread.first_post.hidden_on = timezone.now()
  115. thread.first_post.save(update_fields=[
  116. 'is_hidden',
  117. 'hidden_by',
  118. 'hidden_by_name',
  119. 'hidden_by_slug',
  120. 'hidden_on',
  121. ])
  122. thread.is_hidden = True
  123. thread.save(update_fields=['has_events', 'is_hidden'])
  124. return True
  125. else:
  126. return False
  127. @atomic
  128. def delete_thread(user, thread):
  129. thread.delete()
  130. return True