diff options
| author | 2018-12-08 11:55:40 +0000 | |
|---|---|---|
| committer | 2018-12-08 11:55:40 +0000 | |
| commit | 65b3381cfd35949ef457d5f794f83dbd3a4bd45c (patch) | |
| tree | 1867912cf613498a4ca56fe3a0ef8660876157e3 /api/serializers.py | |
| parent | Renamed class in test_users (diff) | |
| parent | Bump minimum DRF version to `3.9.0`. (diff) | |
Fixed merge conflicts
Diffstat (limited to '')
| -rw-r--r-- | api/serializers.py | 40 | 
1 files changed, 38 insertions, 2 deletions
diff --git a/api/serializers.py b/api/serializers.py index ba6dfaaf..612ce5b4 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -1,8 +1,8 @@ -from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField +from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField, ValidationError  from rest_framework_bulk import BulkSerializerMixin  from .models import ( -    DocumentationLink, +    DocumentationLink, Infraction,      OffTopicChannelName,      Role, SnakeFact,      SnakeIdiom, SnakeName, @@ -16,6 +16,42 @@ class DocumentationLinkSerializer(ModelSerializer):          fields = ('package', 'base_url', 'inventory_url') +class InfractionSerializer(ModelSerializer): +    class Meta: +        model = Infraction +        fields = ( +            'id', 'inserted_at', 'expires_at', 'active', 'user', 'actor', 'type', 'reason', 'hidden' +        ) + +    def validate(self, attrs): +        infr_type = attrs.get('type') + +        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.']}) + +        hidden = attrs.get('hidden') +        if hidden and infr_type in ('superstar',): +            raise ValidationError({'hidden': [f'{infr_type} infractions cannot be hidden.']}) + +        return attrs + + +class ExpandedInfractionSerializer(InfractionSerializer): +    def to_representation(self, instance): +        ret = super().to_representation(instance) + +        user = User.objects.get(id=ret['user']) +        user_data = UserSerializer(user).data +        ret['user'] = user_data + +        actor = User.objects.get(id=ret['actor']) +        actor_data = UserSerializer(actor).data +        ret['actor'] = actor_data + +        return ret + +  class OffTopicChannelNameSerializer(ModelSerializer):      class Meta:          model = OffTopicChannelName  |