threads.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from django.db.transaction import atomic
  2. from django.utils.translation import ugettext_lazy, ugettext as _
  3. from misago.threads.events import record_event
  4. @atomic
  5. def announce_thread(user, thread):
  6. if thread.weight < 2:
  7. thread.weight = 2
  8. message = _("%(user)s changed thread to announcement.")
  9. record_event(user, thread, "star", message, {'user': user})
  10. thread.save(update_fields=['has_events', 'weight'])
  11. return True
  12. else:
  13. return False
  14. @atomic
  15. def pin_thread(user, thread):
  16. if thread.weight < 1:
  17. thread.weight = 1
  18. message = _("%(user)s pinned thread.")
  19. record_event(user, thread, "bookmark", message, {'user': user})
  20. thread.save(update_fields=['has_events', 'weight'])
  21. return True
  22. else:
  23. return False
  24. @atomic
  25. def default_thread(user, thread):
  26. if thread.weight > 0:
  27. if thread.is_announcement:
  28. message = _("%(user)s withhold announcement.")
  29. if thread.is_pinned:
  30. message = _("%(user)s unpinned thread.")
  31. record_event(user, thread, "circle", message, {'user': user})
  32. thread.weight = 0
  33. thread.save(update_fields=['has_events', 'weight'])
  34. return True
  35. else:
  36. return False
  37. @atomic
  38. def move_thread(user, thread, new_forum):
  39. if thread.forum_id != new_forum.pk:
  40. message = _("%(user)s moved thread from %(forum)s.")
  41. record_event(user, thread, "arrow-right", message, {
  42. 'user': user,
  43. 'forum': thread.forum
  44. })
  45. thread.move(new_forum)
  46. thread.save(update_fields=['has_events', 'forum'])
  47. return True
  48. else:
  49. return False
  50. @atomic
  51. def merge_thread(user, thread, other_thread):
  52. thread.merge(other_thread)
  53. thread.synchornize()
  54. thread.save()
  55. other_thread.delete()
  56. return True
  57. @atomic
  58. def approve_thread(user, thread):
  59. if thread.is_moderated:
  60. message = _("%(user)s approved thread.")
  61. record_event(user, thread, "check", message, {'user': user})
  62. thread.is_closed = False
  63. thread.first_post.is_moderated = False
  64. thread.first_post.save(update_fields=['has_events', 'is_moderated'])
  65. thread.synchornize()
  66. thread.save()
  67. return True
  68. else:
  69. return False
  70. @atomic
  71. def open_thread(user, thread):
  72. if thread.is_closed:
  73. message = _("%(user)s opened thread.")
  74. record_event(user, thread, "unlock-alt", message, {'user': user})
  75. thread.is_closed = False
  76. thread.save(update_fields=['has_events', 'is_closed'])
  77. return True
  78. else:
  79. return False
  80. @atomic
  81. def close_thread(user, thread):
  82. if not thread.is_closed:
  83. message = _("%(user)s closed thread.")
  84. record_event(user, thread, "lock", message, {'user': user})
  85. thread.is_closed = True
  86. thread.save(update_fields=['has_events', 'is_closed'])
  87. return True
  88. else:
  89. return False
  90. @atomic
  91. def show_thread(user, thread):
  92. if thread.is_hidden:
  93. message = _("%(user)s made thread visible.")
  94. record_event(user, thread, "eye", message, {'user': user})
  95. thread.first_post.is_hidden = False
  96. thread.first_post.save(update_fields=['is_hidden'])
  97. thread.is_hidden = False
  98. thread.save(update_fields=['has_events', 'is_hidden'])
  99. thread.synchornize()
  100. thread.save()
  101. return True
  102. else:
  103. return False
  104. @atomic
  105. def hide_thread(user, thread):
  106. if not thread.is_hidden:
  107. message = _("%(user)s hid thread.")
  108. record_event(user, thread, "eye-slash", message, {'user': user})
  109. thread.is_hidden = True
  110. thread.save(update_fields=['has_events', 'is_hidden'])
  111. return True
  112. else:
  113. return False
  114. @atomic
  115. def delete_thread(user, thread):
  116. thread.delete()
  117. return True