posts.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from django.db.transaction import atomic
  2. from django.utils import timezone
  3. from django.utils.translation import ugettext as _
  4. from .exceptions import ModerationError
  5. def approve_post(user, post):
  6. if post.is_unapproved:
  7. post.is_unapproved = False
  8. post.save(update_fields=['is_unapproved'])
  9. return True
  10. else:
  11. return False
  12. def protect_post(user, post):
  13. if not post.is_protected:
  14. post.is_protected = True
  15. post.save(update_fields=['is_protected'])
  16. return True
  17. else:
  18. return False
  19. def unprotect_post(user, post):
  20. if post.is_protected:
  21. post.is_protected = False
  22. post.save(update_fields=['is_protected'])
  23. return True
  24. else:
  25. return False
  26. def unhide_post(user, post):
  27. if post.is_first_post:
  28. raise ModerationError(_("You can't make original post visible without revealing thread."))
  29. if post.is_hidden:
  30. post.is_hidden = False
  31. post.save(update_fields=['is_hidden'])
  32. return True
  33. else:
  34. return False
  35. def hide_post(user, post):
  36. if post.is_first_post:
  37. raise ModerationError(_("You can't hide original post without hiding thread."))
  38. if not post.is_hidden:
  39. post.is_hidden = True
  40. post.hidden_by = user
  41. post.hidden_by_name = user.username
  42. post.hidden_by_slug = user.slug
  43. post.hidden_on = timezone.now()
  44. post.save(update_fields=[
  45. 'is_hidden',
  46. 'hidden_by',
  47. 'hidden_by_name',
  48. 'hidden_by_slug',
  49. 'hidden_on',
  50. ])
  51. return True
  52. else:
  53. return False
  54. @atomic
  55. def delete_post(user, post):
  56. if post.is_first_post:
  57. raise ModerationError(_("You can't delete original post without deleting thread."))
  58. post.delete()
  59. return True