moderation.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from django.forms import ValidationError
  2. from django.utils.translation import ugettext as _
  3. from rest_framework import serializers
  4. from misago.acl import add_acl
  5. from misago.categories.models import CATEGORIES_TREE_ID, Category
  6. from misago.categories.permissions import can_see_category, can_browse_category
  7. from misago.threads.models import THREAD_WEIGHT_DEFAULT, THREAD_WEIGHT_GLOBAL
  8. from misago.threads.permissions import allow_start_thread
  9. from misago.threads.validators import validate_title
  10. def validate_category(user, category_id, allow_root=False):
  11. try:
  12. category = Category.objects.get(
  13. tree_id=CATEGORIES_TREE_ID,
  14. id=category_id,
  15. )
  16. except Category.DoesNotExist:
  17. category = None
  18. # Skip ACL validation for root category?
  19. if allow_root and category and not category.level:
  20. return category
  21. if not category or not can_see_category(user, category):
  22. raise ValidationError(_("Requested category could not be found."))
  23. if not can_browse_category(user, category):
  24. raise ValidationError(
  25. _("You don't have permission to access this category."))
  26. return category
  27. class MergeThreadsSerializer(serializers.Serializer):
  28. title = serializers.CharField()
  29. category = serializers.IntegerField()
  30. top_category = serializers.IntegerField(required=False, allow_null=True)
  31. weight = serializers.IntegerField(
  32. required=False,
  33. allow_null=True,
  34. max_value=THREAD_WEIGHT_GLOBAL,
  35. min_value=THREAD_WEIGHT_DEFAULT,
  36. )
  37. is_closed = serializers.NullBooleanField(required=False)
  38. def validate_title(self, title):
  39. return validate_title(title)
  40. def validate_top_category(self, category_id):
  41. return validate_category(self.context, category_id, allow_root=True)
  42. def validate_category(self, category_id):
  43. self.category = validate_category(self.context, category_id)
  44. return self.category
  45. def validate_weight(self, weight):
  46. try:
  47. add_acl(self.context, self.category)
  48. except AttributeError:
  49. return weight # don't validate weight further if category failed
  50. if weight > self.category.acl.get('can_pin_threads', 0):
  51. if weight == 2:
  52. raise ValidationError(_("You don't have permission to pin "
  53. "threads globally in this category."))
  54. else:
  55. raise ValidationError(_("You don't have permission to pin "
  56. "threads in this category."))
  57. return weight
  58. def validate_is_closed(self, is_closed):
  59. try:
  60. add_acl(self.context, self.category)
  61. except AttributeError:
  62. return is_closed # don't validate closed further if category failed
  63. if is_closed and not self.category.acl.get('can_close_threads'):
  64. raise ValidationError(_("You don't have permission to close "
  65. "threads in this category."))
  66. return is_closed