From 62f5377d1f826401aea131073f0521a309a81cfe Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Mon, 18 Apr 2016 13:37:16 -0300 Subject: [PATCH] Adapt subnavs for master detail crud --- base/templatetags/menus.py | 15 ++++++++++----- crud/masterdetail.py | 10 ++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/base/templatetags/menus.py b/base/templatetags/menus.py index fdc6ea379..96bdc9e5a 100644 --- a/base/templatetags/menus.py +++ b/base/templatetags/menus.py @@ -19,15 +19,20 @@ def subnav(context, path=None): # TODO: 118n !!!!!!!!!!!!!! # How to internationalize yaml files???? menu = None - if 'object' in context: - obj = context['object'] - app = obj.__class__._meta.app_label + root_pk = context.get('root_pk', None) + if not root_pk: + obj = context.get('object', None) + if obj: + root_pk = obj.pk + if root_pk: + request = context['request'] + app = request.resolver_match.app_name default_path = '%s/subnav.yaml' % app path = os.path.join(TEMPLATES_DIR, path or default_path) if os.path.exists(path): menu = yaml.load(open(path, 'r')) - resolve_urls_inplace(menu, obj.pk, app) - return dict(menu=menu) + resolve_urls_inplace(menu, root_pk, app) + return {'menu': menu} def resolve_urls_inplace(menu, pk, app): diff --git a/crud/masterdetail.py b/crud/masterdetail.py index 602d4be9b..f1a7b46ba 100644 --- a/crud/masterdetail.py +++ b/crud/masterdetail.py @@ -17,6 +17,16 @@ class MasterDetailCrud(Crud): def create_url(self): return self.resolve_url(CREATE, args=(self.kwargs['pk'],)) + def get_context_data(self, **kwargs): + obj = getattr(self, 'object', None) + if obj: + root_pk = getattr(obj, self.crud.parent_field).pk + else: + root_pk = self.kwargs['pk'] # in list and create + kwargs.setdefault('root_pk', root_pk) + return super(MasterDetailCrud.BaseMixin, + self).get_context_data(**kwargs) + class ListView(CrudListView): @classmethod