Add the ability to redirect to the resulting link, some styling
[bigly] / src / bigly / serializers.py
index 8a9a5c4..a601f10 100644 (file)
@@ -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',
+    )