diff options
| author | 2021-10-29 20:11:27 +0300 | |
|---|---|---|
| committer | 2021-12-18 18:02:12 +0100 | |
| commit | b49612db59ca075f64a4c7da11e3c9ce7e7b19eb (patch) | |
| tree | f2e6aacdc47282c421fb0d1c03f8644c83114ad4 /pydis_site | |
| parent | Add validation to filters to not allow duplicates + additional_field -> JSON (diff) | |
Move filters validations to serializers
Diffstat (limited to 'pydis_site')
| -rw-r--r-- | pydis_site/apps/api/models/bot/filters.py | 34 | ||||
| -rw-r--r-- | pydis_site/apps/api/serializers.py | 33 | 
2 files changed, 33 insertions, 34 deletions
diff --git a/pydis_site/apps/api/models/bot/filters.py b/pydis_site/apps/api/models/bot/filters.py index 472354f8..3a1f3c6a 100644 --- a/pydis_site/apps/api/models/bot/filters.py +++ b/pydis_site/apps/api/models/bot/filters.py @@ -56,11 +56,6 @@ class FilterSettingsMixin(models.Model):          help_text="The duration of the infraction. Null if permanent."      ) -    def clean(self): -        """Validate infraction fields as whole.""" -        if (self.infraction_duration or self.infraction_reason) and not self.infraction_type: -            raise ValidationError("Infraction type is required if setting infraction duration or reason.") -      class Meta:          """Metaclass for settings mixin.""" @@ -113,20 +108,6 @@ class FilterList(FilterSettingsMixin):      allowed_channels = ArrayField(models.IntegerField())      allowed_categories = ArrayField(models.IntegerField()) -    def clean(self): -        """Do not allow duplicates in allowed and disallowed lists.""" -        # Still run infraction fields validation -        super().clean() - -        channels_collection = self.allowed_channels + self.disallowed_channels -        categories_collection = self.allowed_categories + self.disallowed_categories - -        if len(channels_collection) != len(set(channels_collection)): -            raise ValidationError("Allowed and disallowed channels lists contain duplicates.") - -        if len(categories_collection) != len(set(categories_collection)): -            raise ValidationError("Allowed and disallowed categories lists contain duplicates.") -      class Meta:          """Constrain name and list_type unique.""" @@ -181,20 +162,5 @@ class Filter(FilterSettingsMixin):      allowed_channels = ArrayField(models.IntegerField(), null=True)      allowed_categories = ArrayField(models.IntegerField(), null=True) -    def clean(self): -        """Do not allow duplicates in allowed and disallowed lists.""" -        # Still run infraction fields validation -        super().clean() - -        if self.allowed_channels is not None or self.disallowed_channels is not None: -            channels_collection = self.allowed_channels + self.disallowed_channels -            if len(channels_collection) != len(set(channels_collection)): -                raise ValidationError("Allowed and disallowed channels lists contain duplicates.") - -        if self.allowed_categories is not None or self.disallowed_categories is not None: -            categories_collection = self.allowed_categories + self.disallowed_categories -            if len(categories_collection) != len(set(categories_collection)): -                raise ValidationError("Allowed and disallowed categories lists contain duplicates.") -      def __str__(self) -> str:          return f"Filter {self.content!r}" diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index 4e92b3a0..b5f083b0 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -139,6 +139,23 @@ SETTINGS_FIELDS = ALWAYS_OPTIONAL_SETTINGS + REQUIRED_FOR_FILTER_LIST_SETTINGS  class FilterSerializer(ModelSerializer):      """A class providing (de-)serialization of `Filter` instances.""" +    def validate(self, data): +        """Perform infraction data + allow and disallowed lists validation.""" +        if (data.get('infraction_reason') or data.get('infraction_duration')) and not data.get('infraction_type'): +            raise ValidationError("Infraction type is required with infraction duration or reason") + +        if data.get('allowed_channels') is not None and data.get('disallowed_channels') is not None: +            channels_collection = data['allowed_channels'] + data['disallowed_channels'] +            if len(channels_collection) != len(set(channels_collection)): +                raise ValidationError("Allowed and disallowed channels lists contain duplicates.") + +        if data.get('allowed_categories') is not None and data.get('disallowed_categories') is not None: +            categories_collection = data['allowed_categories'] + data['disallowed_categories'] +            if len(categories_collection) != len(set(categories_collection)): +                raise ValidationError("Allowed and disallowed categories lists contain duplicates.") + +        return data +      class Meta:          """Metadata defined for the Django REST Framework.""" @@ -160,6 +177,22 @@ class FilterListSerializer(ModelSerializer):      filters = FilterSerializer(many=True, read_only=True) +    def validate(self, data): +        """Perform infraction data + allow and disallowed lists validation.""" +        if (data['infraction_reason'] or data['infraction_duration']) and not data['infraction_type']: +            raise ValidationError("Infraction type is required with infraction duration or reason") + +        channels_collection = data['allowed_channels'] + data['disallowed_channels'] +        categories_collection = data['allowed_categories'] + data['disallowed_categories'] + +        if len(channels_collection) != len(set(channels_collection)): +            raise ValidationError("Allowed and disallowed channels lists contain duplicates.") + +        if len(categories_collection) != len(set(categories_collection)): +            raise ValidationError("Allowed and disallowed categories lists contain duplicates.") + +        return data +      class Meta:          """Metadata defined for the Django REST Framework."""  |