Add the ability to redirect to the resulting link, some styling
[bigly] / src / bigly / serializers.py
1 from rest_framework import serializers
2
3 class ChoiceField(serializers.CharField):
4     def __init__(self, *args, **kwargs):
5         sentinel = object()
6         self._choices = kwargs.pop('choices', sentinel)
7         if self._choices is sentinel:
8             raise Exception('Choices is required')
9         else:
10             self._choices = set(self._choices)
11         super().__init__(self, *args, **kwargs)
12
13     def to_internal_value(self, data):
14         if data not in choices:
15             raise serializers.ValidationError('{} not in choices {}'.format(
16                 data,
17                 self._choices,
18             ))
19         return data
20
21 class FollowRedirectsSerializer(serializers.Serializer):
22     link = serializers.URLField(required=True)
23     remove_utm = serializers.BooleanField(required=False)
24     handler = serializers.ChoiceField(
25         choices=['display', 'redirect'],
26         default='display',
27     )