From 4a05e60274500e3b0df2b292e856828bdf3628cc Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Sat, 15 Oct 2016 15:05:51 -0300 Subject: [PATCH] Ref subnav para aceitar urls sem pk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit a template tag subnav em sapl.base.templatetags.menu.py foi customizada para aceitar que urls do request com ou sem pk possa ter subnav, bem como as urls de dentro do yaml também não necessitarem de pk. Para os casos em que a url do request não tenha pk, não basta a existência do arquivo padrão subnav.yaml... mesmo que o subnav a ser renderizado seja o padrão, a tamplate tag subnav só renderiza, independente das condições, se existir a variável `subnav_template_name` no contexto apontando qual o arquivo yaml deve ser renderizado... Existem exemplos implemetandos dentro do projeto, faça uma busca por subnav_template_name. --- sapl/base/templatetags/menus.py | 34 +++++++++++++++---- sapl/materia/views.py | 2 ++ sapl/templates/materia/em_lote/acessorio.html | 1 - sapl/templates/materia/em_lote/subnav.html | 4 --- .../materia/em_lote/subnav_em_lote.yaml | 5 +++ .../templates/materia/em_lote/tramitacao.html | 1 - sapl/utils.py | 9 +++-- 7 files changed, 41 insertions(+), 15 deletions(-) delete mode 100644 sapl/templates/materia/em_lote/subnav.html create mode 100644 sapl/templates/materia/em_lote/subnav_em_lote.yaml diff --git a/sapl/base/templatetags/menus.py b/sapl/base/templatetags/menus.py index 3cbf5e46c..3d208e86b 100644 --- a/sapl/base/templatetags/menus.py +++ b/sapl/base/templatetags/menus.py @@ -1,6 +1,10 @@ -import yaml from django import template from django.core.urlresolvers import reverse +from django.utils.translation import ugettext_lazy as _ +import yaml + +from sapl.utils import sapl_logger + register = template.Library() @@ -28,7 +32,7 @@ def subnav(context, path=None): if obj: root_pk = obj.pk - if root_pk: + if root_pk or 'subnav_template_name' in context: request = context['request'] """ @@ -64,7 +68,11 @@ def subnav(context, path=None): menu = yaml.load(rendered) resolve_urls_inplace(menu, root_pk, rm, context) except Exception as e: - print(e) + sapl_logger.error(_("""Erro na conversão do yaml %s. App: %s. + Erro: + %s + """) % ( + yaml_path, rm.app_name, str(e))) return {'menu': menu} @@ -81,6 +89,7 @@ def resolve_urls_inplace(menu, pk, rm, context): return list_active else: if 'url' in menu: + url_name = menu['url'] if 'check_permission' in menu and not context[ @@ -88,16 +97,27 @@ def resolve_urls_inplace(menu, pk, rm, context): menu['url'] = '' menu['active'] = '' else: - menu['url'] = reverse( - '%s:%s' % (rm.app_name, menu['url']), kwargs={'pk': pk}) + if ':' in url_name: + try: + menu['url'] = reverse('%s' % menu['url'], + kwargs={'pk': pk}) + except: + menu['url'] = reverse('%s' % menu['url']) + else: + try: + menu['url'] = reverse('%s:%s' % ( + rm.app_name, menu['url']), kwargs={'pk': pk}) + except: + menu['url'] = reverse('%s:%s' % ( + rm.app_name, menu['url'])) + menu['active'] = 'active'\ if context['request'].path == menu['url'] else '' - if not menu['active']: """ Se não encontrada diretamente, procura a url acionada dentro do crud, caso seja um. - Serve para manter o active no suvnav correto ao acionar + Serve para manter o active no subnav correto ao acionar as funcionalidades diretas do MasterDetailCrud, como: - visualização de detalhes, adição, edição, remoção. """ diff --git a/sapl/materia/views.py b/sapl/materia/views.py index afb370712..083b3ede1 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -1336,6 +1336,8 @@ class PrimeiraTramitacaoEmLoteView(PermissionRequiredMixin, FilterView): context = super(PrimeiraTramitacaoEmLoteView, self).get_context_data(**kwargs) + context['subnav_template_name'] = 'materia/em_lote/subnav_em_lote.yaml' + # Verifica se os campos foram preenchidos if not self.filterset.form.is_valid(): return context diff --git a/sapl/templates/materia/em_lote/acessorio.html b/sapl/templates/materia/em_lote/acessorio.html index 5b4528bca..6fae58731 100644 --- a/sapl/templates/materia/em_lote/acessorio.html +++ b/sapl/templates/materia/em_lote/acessorio.html @@ -1,7 +1,6 @@ {% extends "crud/detail.html" %} {% load i18n crispy_forms_tags %} {% block actions %}{% endblock %} -{% block sections_nav %}{% endblock %} {% block detail_content %} {% if not filter_url %} diff --git a/sapl/templates/materia/em_lote/subnav.html b/sapl/templates/materia/em_lote/subnav.html deleted file mode 100644 index 1ba0fe431..000000000 --- a/sapl/templates/materia/em_lote/subnav.html +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/sapl/templates/materia/em_lote/subnav_em_lote.yaml b/sapl/templates/materia/em_lote/subnav_em_lote.yaml new file mode 100644 index 000000000..3469c9262 --- /dev/null +++ b/sapl/templates/materia/em_lote/subnav_em_lote.yaml @@ -0,0 +1,5 @@ +{% load i18n common_tags %} +- title: {% trans 'Primeira Tramitação' %} + url: primeira_tramitacao_em_lote +- title: {% trans 'Tramitação em Lote' %} + url: tramitacao_em_lote diff --git a/sapl/templates/materia/em_lote/tramitacao.html b/sapl/templates/materia/em_lote/tramitacao.html index a8fb49df5..b9845f55b 100644 --- a/sapl/templates/materia/em_lote/tramitacao.html +++ b/sapl/templates/materia/em_lote/tramitacao.html @@ -1,7 +1,6 @@ {% extends "crud/detail.html" %} {% load i18n crispy_forms_tags %} {% block actions %}{% endblock %} -{% block sections_nav %} {% include 'materia/em_lote/subnav.html'%} {% endblock sections_nav %} {% block detail_content %} {% if not filter_url %} diff --git a/sapl/utils.py b/sapl/utils.py index 05eed3457..d4e7bed3f 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -1,9 +1,9 @@ -import hashlib from datetime import date from functools import wraps from unicodedata import normalize as unicodedata_normalize +import hashlib +import logging -import magic from django import forms from django.apps import apps from django.conf import settings @@ -14,6 +14,11 @@ from django.contrib.contenttypes.models import ContentType from django.core.exceptions import PermissionDenied, ValidationError from django.utils.translation import ugettext_lazy as _ from floppyforms import ClearableFileInput +import magic +from sapl.settings import BASE_DIR + + +sapl_logger = logging.getLogger(BASE_DIR.name) def normalize(txt):