posts.py 1.9 KB

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