| 1 |
from django.urls import re_path |
| 2 |
from rest_framework import fields, generics, versioning |
| 3 |
|
| 4 |
from snippets.models import Snippet |
| 5 |
from snippets.serializers import SnippetSerializer |
| 6 |
from testproj.urls import SchemaView, required_urlpatterns |
| 7 |
|
| 8 |
|
| 9 |
class SnippetV1Serializer(SnippetSerializer): |
| 10 |
v1field = fields.IntegerField(help_text="version 1.0 field") |
| 11 |
|
| 12 |
|
| 13 |
class SnippetSerializerV2(SnippetV1Serializer): |
| 14 |
v2field = fields.IntegerField(help_text="version 2.0 field") |
| 15 |
|
| 16 |
class Meta: |
| 17 |
# Same name for check failing |
| 18 |
ref_name = "SnippetV1" |
| 19 |
|
| 20 |
|
| 21 |
class SnippetList(generics.ListCreateAPIView): |
| 22 |
"""SnippetList classdoc""" |
| 23 |
|
| 24 |
queryset = Snippet.objects.all() |
| 25 |
serializer_class = SnippetV1Serializer |
| 26 |
versioning_class = versioning.URLPathVersioning |
| 27 |
|
| 28 |
def perform_create(self, serializer): |
| 29 |
serializer.save(owner=self.request.user) |
| 30 |
|
| 31 |
def post(self, request, *args, **kwargs): |
| 32 |
"""post method docstring""" |
| 33 |
return super(SnippetList, self).post(request, *args, **kwargs) |
| 34 |
|
| 35 |
|
| 36 |
class SnippetsV2(SnippetList): |
| 37 |
serializer_class = SnippetSerializerV2 |
| 38 |
|
| 39 |
|
| 40 |
VERSION_PREFIX_URL = r"^versioned/url/v(?P<version>1.0|2.0)/" |
| 41 |
|
| 42 |
|
| 43 |
class VersionedSchemaView(SchemaView): |
| 44 |
versioning_class = versioning.URLPathVersioning |
| 45 |
|
| 46 |
|
| 47 |
urlpatterns = required_urlpatterns + [ |
| 48 |
re_path(VERSION_PREFIX_URL + r"snippets/$", SnippetList.as_view()), |
| 49 |
re_path(VERSION_PREFIX_URL + r"other_snippets/$", SnippetsV2.as_view()), |
| 50 |
re_path( |
| 51 |
VERSION_PREFIX_URL + r"swagger(?P<format>.json|.yaml)$", |
| 52 |
VersionedSchemaView.without_ui(), |
| 53 |
name="vschema-json", |
| 54 |
), |
| 55 |
] |