| 1 |
import coreapi |
| 2 |
import coreschema |
| 3 |
from django.urls import re_path |
| 4 |
from rest_framework import pagination |
| 5 |
|
| 6 |
from testproj.urls import required_urlpatterns |
| 7 |
|
| 8 |
from .url_versioning import VERSION_PREFIX_URL, SnippetList, VersionedSchemaView |
| 9 |
|
| 10 |
|
| 11 |
class FilterBackendWithoutParams: |
| 12 |
def filter_queryset(self, request, queryset, view): |
| 13 |
return queryset |
| 14 |
|
| 15 |
|
| 16 |
class OldFilterBackend(FilterBackendWithoutParams): |
| 17 |
def get_schema_fields(self, view): |
| 18 |
return [ |
| 19 |
coreapi.Field( |
| 20 |
name="test_param", |
| 21 |
required=False, |
| 22 |
location="query", |
| 23 |
schema=coreschema.String(title="Test", description="Test description"), |
| 24 |
) |
| 25 |
] |
| 26 |
|
| 27 |
|
| 28 |
class PaginatorV1(pagination.LimitOffsetPagination): |
| 29 |
def get_paginated_response_schema(self, schema): |
| 30 |
response_schema = super().get_paginated_response_schema(schema) |
| 31 |
del response_schema["properties"]["count"] |
| 32 |
return response_schema |
| 33 |
|
| 34 |
|
| 35 |
class PaginatorV2(pagination.LimitOffsetPagination): |
| 36 |
def __getattribute__(self, item): |
| 37 |
if item in {"get_paginated_response_schema", "get_schema_operation_parameters"}: |
| 38 |
raise AttributeError |
| 39 |
return super().__getattribute__(item) |
| 40 |
|
| 41 |
|
| 42 |
class PaginatorV3(PaginatorV1): |
| 43 |
def get_paginated_response_schema(self, schema): |
| 44 |
response_schema = super().get_paginated_response_schema(schema) |
| 45 |
response_schema["required"] = ["results"] |
| 46 |
return response_schema |
| 47 |
|
| 48 |
def __getattribute__(self, item): |
| 49 |
if item == "get_schema_fields": |
| 50 |
raise AttributeError() |
| 51 |
return super().__getattribute__(item) |
| 52 |
|
| 53 |
|
| 54 |
class SnippetsV1(SnippetList): |
| 55 |
filter_backends = list(SnippetList.filter_backends) + [ |
| 56 |
FilterBackendWithoutParams, |
| 57 |
OldFilterBackend, |
| 58 |
] |
| 59 |
pagination_class = PaginatorV1 |
| 60 |
|
| 61 |
|
| 62 |
class SnippetsV2(SnippetList): |
| 63 |
pagination_class = PaginatorV2 |
| 64 |
|
| 65 |
|
| 66 |
class SnippetsV3(SnippetList): |
| 67 |
pagination_class = PaginatorV3 |
| 68 |
|
| 69 |
|
| 70 |
urlpatterns = required_urlpatterns + [ |
| 71 |
re_path(VERSION_PREFIX_URL + r"snippets/$", SnippetsV1.as_view()), |
| 72 |
re_path(VERSION_PREFIX_URL + r"other_snippets/$", SnippetsV2.as_view()), |
| 73 |
re_path(VERSION_PREFIX_URL + r"ya_snippets/$", SnippetsV3.as_view()), |
| 74 |
re_path( |
| 75 |
VERSION_PREFIX_URL + r"swagger(?P<format>.json|.yaml)$", |
| 76 |
VersionedSchemaView.without_ui(), |
| 77 |
name="vschema-json", |
| 78 |
), |
| 79 |
] |