| 1 |
from django.urls import re_path |
| 2 |
from rest_framework import serializers |
| 3 |
|
| 4 |
from testproj.urls import required_urlpatterns |
| 5 |
|
| 6 |
from .url_versioning import ( |
| 7 |
VERSION_PREFIX_URL, |
| 8 |
SnippetList, |
| 9 |
SnippetSerializer, |
| 10 |
VersionedSchemaView, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
class SnippetsSerializer(serializers.HyperlinkedModelSerializer, SnippetSerializer): |
| 15 |
ipv4 = serializers.IPAddressField(required=False) |
| 16 |
uri = serializers.URLField(required=False) |
| 17 |
tracks = serializers.RelatedField( |
| 18 |
read_only=True, |
| 19 |
allow_null=True, |
| 20 |
allow_empty=True, |
| 21 |
many=True, |
| 22 |
) |
| 23 |
|
| 24 |
class Meta: |
| 25 |
fields = tuple(SnippetSerializer().fields.keys()) + ( |
| 26 |
"ipv4", |
| 27 |
"uri", |
| 28 |
"tracks", |
| 29 |
"url", |
| 30 |
) |
| 31 |
model = SnippetList.queryset.model |
| 32 |
|
| 33 |
|
| 34 |
class SnippetsV2Serializer(SnippetSerializer): |
| 35 |
url = serializers.HyperlinkedRelatedField( |
| 36 |
view_name="snippets-detail", source="*", read_only=True |
| 37 |
) |
| 38 |
other_owner_snippets = serializers.PrimaryKeyRelatedField( |
| 39 |
read_only=True, source="owner.snippets", many=True |
| 40 |
) |
| 41 |
owner_snippets = serializers.PrimaryKeyRelatedField(read_only=True, many=True) |
| 42 |
|
| 43 |
|
| 44 |
class SnippetsV1(SnippetList): |
| 45 |
serializer_class = SnippetsSerializer |
| 46 |
|
| 47 |
def get_serializer_class(self): |
| 48 |
return self.serializer_class |
| 49 |
|
| 50 |
|
| 51 |
class SnippetsV2(SnippetsV1): |
| 52 |
serializer_class = SnippetsV2Serializer |
| 53 |
|
| 54 |
|
| 55 |
urlpatterns = required_urlpatterns + [ |
| 56 |
re_path(VERSION_PREFIX_URL + r"snippets/$", SnippetsV1.as_view()), |
| 57 |
re_path(VERSION_PREFIX_URL + r"other_snippets/$", SnippetsV2.as_view()), |
| 58 |
re_path( |
| 59 |
VERSION_PREFIX_URL + r"swagger(?P<format>.json|.yaml)$", |
| 60 |
VersionedSchemaView.without_ui(), |
| 61 |
name="vschema-json", |
| 62 |
), |
| 63 |
] |