X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=src%2Fbigly%2Fserializers.py;h=a601f1020a0ad2d0c42ffd7d30a7f81686423178;hb=59c710f97ad48c2b57ac8711ee30cbe7438810e8;hp=bc948fd388753916a570de0d6f3cba3a0e775daf;hpb=c2f61845f8372b4796bf4d10da7f57f99ee933c7;p=bigly diff --git a/src/bigly/serializers.py b/src/bigly/serializers.py index bc948fd..a601f10 100644 --- a/src/bigly/serializers.py +++ b/src/bigly/serializers.py @@ -1,4 +1,27 @@ from rest_framework import serializers +class ChoiceField(serializers.CharField): + def __init__(self, *args, **kwargs): + sentinel = object() + self._choices = kwargs.pop('choices', sentinel) + if self._choices is sentinel: + raise Exception('Choices is required') + else: + self._choices = set(self._choices) + super().__init__(self, *args, **kwargs) + + def to_internal_value(self, data): + if data not in choices: + raise serializers.ValidationError('{} not in choices {}'.format( + data, + self._choices, + )) + return data + class FollowRedirectsSerializer(serializers.Serializer): link = serializers.URLField(required=True) + remove_utm = serializers.BooleanField(required=False) + handler = serializers.ChoiceField( + choices=['display', 'redirect'], + default='display', + )