X-Git-Url: https://code.kerkeslager.com/?p=bigly;a=blobdiff_plain;f=src%2Fbigly%2Fserializers.py;h=a601f1020a0ad2d0c42ffd7d30a7f81686423178;hp=8a9a5c4d88976d2546a53441b67f621328765dd8;hb=59c710f97ad48c2b57ac8711ee30cbe7438810e8;hpb=9a3403f78fb8021297a0b351d458f9004a9b2b3e diff --git a/src/bigly/serializers.py b/src/bigly/serializers.py index 8a9a5c4..a601f10 100644 --- a/src/bigly/serializers.py +++ b/src/bigly/serializers.py @@ -1,5 +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', + )