diff options
author | 2019-11-07 19:08:23 +0100 | |
---|---|---|
committer | 2019-11-07 19:08:23 +0100 | |
commit | 8e689ac24c8965f8285d1a4fdadbc0d593fd7c0c (patch) | |
tree | 8a3adc7a67e38cfbcbe7056f0277d6c882764e8a /pydis_site/apps/api/serializers.py | |
parent | Block PATCH and PUT methods (diff) | |
parent | Merge pull request #278 from python-discord/active-infractions-validation (diff) |
Merge branch 'master' into #222-offensive-msg-autodeletion
Diffstat (limited to 'pydis_site/apps/api/serializers.py')
-rw-r--r-- | pydis_site/apps/api/serializers.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index bc94349e..52a82eac 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -1,6 +1,6 @@ """Converters from Django models to data interchange formats and back.""" - from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField, ValidationError +from rest_framework.validators import UniqueTogetherValidator from rest_framework_bulk import BulkSerializerMixin from .models import ( @@ -106,11 +106,22 @@ class InfractionSerializer(ModelSerializer): fields = ( 'id', 'inserted_at', 'expires_at', 'active', 'user', 'actor', 'type', 'reason', 'hidden' ) + validators = [ + UniqueTogetherValidator( + queryset=Infraction.objects.filter(active=True), + fields=['user', 'type'], + message='This user already has an active infraction of this type.', + ) + ] def validate(self, attrs: dict) -> dict: """Validate data constraints for the given data and abort if it is invalid.""" infr_type = attrs.get('type') + active = attrs.get('active') + if active and infr_type in ('note', 'warning', 'kick'): + raise ValidationError({'active': [f'{infr_type} infractions cannot be active.']}) + expires_at = attrs.get('expires_at') if expires_at and infr_type in ('kick', 'warning'): raise ValidationError({'expires_at': [f'{infr_type} infractions cannot expire.']}) |