Skip to content

Commit

Permalink
fix(generic): also add detail views to enumarated api_endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed May 29, 2024
1 parent 7768bbd commit 302b18e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions apis_core/generic/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,24 @@


class CustomEndpointEnumerator(EndpointEnumerator):
def _generate_content_type_endpoint(self, content_type: ContentType):
def _generate_content_type_endpoint(
self, content_type: ContentType, method: str = "list"
):
"""Create a endpoint tuple, usable by the SchemaGenerator of DRF spectacular"""
path = reverse("apis_core:generic:genericmodelapi-list", args=[content_type])
cls = resolve(path).func.cls

if method == "detail":
path += "{id}/"
regex = path
# for now we only do "GET"
method = "GET"
cls = resolve(path).func.cls
httpmethod = "GET"
# we have to add a attribute, so that the
# `initkwargs` argument to the `as_view`
# method can contain a `model` argument
cls.model = None
callback = cls.as_view({"get": "list"}, model=content_type.model_class())
return (path, regex, method, callback)
callback = cls.as_view({"get": method}, model=content_type.model_class())
return (path, regex, httpmethod, callback)

def get_api_endpoints(self, patterns=None, prefix=""):
"""
Expand All @@ -62,8 +67,10 @@ def get_api_endpoints(self, patterns=None, prefix=""):
if content_type.model_class() is not None and issubclass(
content_type.model_class(), GenericModel
):
endpoint = self._generate_content_type_endpoint(content_type)
api_endpoints.append(endpoint)
api_endpoints.append(self._generate_content_type_endpoint(content_type))
api_endpoints.append(
self._generate_content_type_endpoint(content_type, "detail")
)
return api_endpoints


Expand Down

0 comments on commit 302b18e

Please sign in to comment.