moderation.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from rest_framework import serializers
  2. from django.core.exceptions import ValidationError
  3. from django.utils.translation import ugettext as _
  4. from misago.acl import add_acl
  5. from misago.threads.models import Thread
  6. from misago.threads.permissions import can_start_thread
  7. from misago.threads.validators import validate_category, validate_title
  8. __all__ = [
  9. 'NewThreadSerializer',
  10. ]
  11. class NewThreadSerializer(serializers.Serializer):
  12. title = serializers.CharField()
  13. category = serializers.IntegerField()
  14. weight = serializers.IntegerField(
  15. required=False,
  16. allow_null=True,
  17. max_value=Thread.WEIGHT_GLOBAL,
  18. min_value=Thread.WEIGHT_DEFAULT,
  19. )
  20. is_hidden = serializers.NullBooleanField(required=False)
  21. is_closed = serializers.NullBooleanField(required=False)
  22. def validate_title(self, title):
  23. return validate_title(title)
  24. def validate_category(self, category_id):
  25. self.category = validate_category(self.context, category_id)
  26. if not can_start_thread(self.context, self.category):
  27. raise ValidationError(_("You can't create new threads in selected category."))
  28. return self.category
  29. def validate_weight(self, weight):
  30. try:
  31. add_acl(self.context, self.category)
  32. except AttributeError:
  33. return weight # don't validate weight further if category failed
  34. if weight > self.category.acl.get('can_pin_threads', 0):
  35. if weight == 2:
  36. raise ValidationError(
  37. _("You don't have permission to pin threads globally in this category.")
  38. )
  39. else:
  40. raise ValidationError(
  41. _("You don't have permission to pin threads in this category.")
  42. )
  43. return weight
  44. def validate_is_hidden(self, is_hidden):
  45. try:
  46. add_acl(self.context, self.category)
  47. except AttributeError:
  48. return is_hidden # don't validate hidden further if category failed
  49. if is_hidden and not self.category.acl.get('can_hide_threads'):
  50. raise ValidationError(_("You don't have permission to hide threads in this category."))
  51. return is_hidden
  52. def validate_is_closed(self, is_closed):
  53. try:
  54. add_acl(self.context, self.category)
  55. except AttributeError:
  56. return is_closed # don't validate closed further if category failed
  57. if is_closed and not self.category.acl.get('can_close_threads'):
  58. raise ValidationError(
  59. _("You don't have permission to close threads in this category.")
  60. )
  61. return is_closed