Skip to content

Commit

Permalink
Merge 3910dd3 into ba90a5a
Browse files Browse the repository at this point in the history
  • Loading branch information
skrhlm committed Nov 18, 2019
2 parents ba90a5a + 3910dd3 commit 4044b8a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
20 changes: 20 additions & 0 deletions bananas/admin/api/schemas/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from functools import wraps


def navigation(view):
@wraps(view)
def wrapper(*args, **kwargs):
return view(*args, **kwargs)
wrapper.navigation = True
return wrapper


def tags(include=None, exclude=None):
def wrapped(view):
@wraps(view)
def wrapper(*args, **kwargs):
return view(*args, **kwargs)
wrapper.include_tags = include
wrapper.exclude_tags = exclude
return wrapper
return wrapped
21 changes: 16 additions & 5 deletions bananas/admin/api/schemas/yasg.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,35 @@ def get_summary(self):
def get_tags(self, operation_keys):
view = self.view
meta = self.view.get_admin_meta()
tags = ["app:{label}".format(label=meta.app_label)]
tags = {"app:{label}".format(label=meta.app_label)}

if self.is_navigation():
tags.append("navigation")
tags.add("navigation")

if issubclass(view.__class__, viewsets.ModelViewSet):
tags.append("crud")
tags.add("crud")

view_method = getattr(view, view.action, None)
if view_method:
include_tags = set(getattr(view_method, "include_tags", None) or [])
exclude_tags = set(getattr(view_method, "exclude_tags", None) or [])
tags |= include_tags
tags -= exclude_tags

return [tag for tag in tags if tag not in meta.exclude_tags]

def is_navigation(self):
if not hasattr(self, "_is_navigation"):
self._is_navigation = False
try:
view_method = getattr(self.view, self.view.action, None)
is_custom_list_route = is_custom_action(self.view.action) and view_method and not view_method.detail
allows_navigation = getattr(view_method, "navigation", False)
url_name = view_method.url_name if is_custom_list_route else self.view.action
if self.method == "GET" and (
self.view.action == "list" or not hasattr(self.view, "list")
self.view.action == "list" or not hasattr(self.view, "list") or (is_custom_list_route and allows_navigation)
):
self.view.reverse_action("list")
self.view.reverse_action(url_name)
self._is_navigation = True
except NoReverseMatch:
pass
Expand Down
13 changes: 13 additions & 0 deletions example/example/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from bananas.admin.api.views import BananasAPI
from bananas.compat import reverse
from bananas.lazy import lazy_title
from bananas.admin.api.schemas.decorators import tags


class UserDetailsSerializer(serializers.HyperlinkedModelSerializer):
Expand Down Expand Up @@ -136,8 +137,20 @@ class AppleViewSet(BananasAPI, viewsets.ModelViewSet):

name = lazy_title(_("apple"))

#@tags(exclude=["navigation"])
def list(self, request):
return Response()

@tags(["navigation"])
@action(detail=False)
def red_delicious(self, request):
return Response()

@tags(["navigation"])
@action(detail=False)
def granny_smith(self, request):
return Response()


class Admin:
app_label = "fruit"
Expand Down

0 comments on commit 4044b8a

Please sign in to comment.