moderation.py 2.7 KB

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