threads.py 4.0 KB

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