diff options
author | 2018-11-29 12:27:07 -0800 | |
---|---|---|
committer | 2018-11-29 21:27:07 +0100 | |
commit | 9b0eeff865bb39454f201eb82b460fdc27899a90 (patch) | |
tree | 02581ab033d8a6485861b97384a520953a56aa71 /api/serializers.py | |
parent | Added regex validator to special snake name. (#153) (diff) |
Django - Add Infractions API (#149)
* add Infraction model and serialiser
The model in not finalised.
* fix mix up of serialiser fields
* remove explicit id field and add foreign keys
* remove unused import
* disallow null for user
* add view set and route
* fix model and create migration
* fix typo choice => choices
* specify names for reverse accessors for User FKs
* add django-filter
* add filters to view set
* add string dunder method to model
* add list/retrieve tests
* make reason nullable
* add creation tests
* remove support for PUT and DELETE
* add support for PATCH
* assert timestamps using strings rather than datetimes
This is done to keep 3.6 support; datetime.fromisoformat() is 3.7+
* assert inserted_at
* add unauthenticated tests
* add bad value tests for list filters and retrieve
* remove prefetch cache invalidation
* make __str__ more descriptive
* add field validation & remove note type
* add tests for field validation
* fix coverage for Infraction string dunder test
* fix coverage (for sure this time)
* return 400 for partial updates with frozen fields
* add expanded serialiser and endpoints
* test expanded endpoints
* remove extra retrieve call
* remove unnecessary try-finally blocks
* remove extra blank line
* document endpoints (except expanded)
* document expanded routes
* fix wrong routes in docstring (/infraction -> /infractions)
* make merge migration
Diffstat (limited to 'api/serializers.py')
-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 |