Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

birger/openapi tags #1503

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apis_core/generic/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.forms import model_to_dict
from django.urls import reverse

from apis_core.generic.helpers import permission_fullname
from apis_core.generic.helpers import mro_paths, permission_fullname
from apis_core.generic.signals import (
post_duplicate,
post_merge_with,
Expand Down Expand Up @@ -39,6 +39,10 @@ def get_importview_url(cls):
ct = ContentType.objects.get_for_model(cls)
return reverse("apis_core:generic:import", args=[ct])

@classmethod
def get_openapi_tags(cls):
return [item[-1] for item in mro_paths(cls)]

def get_edit_url(self):
ct = ContentType.objects.get_for_model(self)
return reverse("apis_core:generic:update", args=[ct, self.id])
Expand Down
3 changes: 3 additions & 0 deletions apis_core/generic/api_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from rest_framework import viewsets

from apis_core.generic.schema import GenericAutoSchema

from .filterbackends import GenericFilterBackend
from .helpers import first_member_match, makeclassprefix, module_paths
from .serializers import GenericHyperlinkedModelSerializer, serializer_factory
Expand All @@ -15,6 +17,7 @@ class ModelViewSet(viewsets.ModelViewSet):
"""

filter_backends = [GenericFilterBackend]
schema = GenericAutoSchema()

def dispatch(self, *args, **kwargs):
self.model = kwargs.get("contenttype").model_class()
Expand Down
14 changes: 14 additions & 0 deletions apis_core/generic/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from drf_spectacular.openapi import AutoSchema


class GenericAutoSchema(AutoSchema):
"""
Add an option to the default drf_spectacular schema that
allows to set the tags of a route via the model.
"""

def get_tags(self) -> list[str]:
model = getattr(self.view, "model", None)
if hasattr(model, "get_openapi_tags"):
return model.get_openapi_tags()
return super().get_tags()
Loading