posts.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.moderation.exceptions import ModerationError
  5. def protect_post(user, post):
  6. if not post.is_protected:
  7. post.is_protected = True
  8. post.save(update_fields=['is_protected'])
  9. return True
  10. else:
  11. return False
  12. def unprotect_post(user, post):
  13. if post.is_protected:
  14. post.is_protected = False
  15. post.save(update_fields=['is_protected'])
  16. return True
  17. else:
  18. return False
  19. def unhide_post(user, post):
  20. if post.pk == post.thread.first_post_id:
  21. raise ModerationError(_("You can't make original post "
  22. " visible without revealing thread."))
  23. if post.is_hidden:
  24. post.is_hidden = False
  25. post.save(update_fields=['is_hidden'])
  26. return True
  27. else:
  28. return False
  29. def hide_post(user, post):
  30. if post.pk == post.thread.first_post_id:
  31. raise ModerationError(_("You can't hide original "
  32. "post without hiding thread."))
  33. if not post.is_hidden:
  34. post.is_hidden = True
  35. post.hidden_by = user
  36. post.hidden_by_name = user.username
  37. post.hidden_by_slug = user.slug
  38. post.hidden_on = timezone.now()
  39. post.save(update_fields=[
  40. 'is_hidden',
  41. 'hidden_by',
  42. 'hidden_by_name',
  43. 'hidden_by_slug',
  44. 'hidden_on',
  45. ])
  46. return True
  47. else:
  48. return False
  49. @atomic
  50. def delete_post(user, post):
  51. if post.pk == post.thread.first_post_id:
  52. raise ModerationError(_("You can't delete original "
  53. "post without deleting thread."))
  54. post.delete()
  55. return True