diff --git a/sapl/norma/urls.py b/sapl/norma/urls.py index abf13c276..93081c4fc 100644 --- a/sapl/norma/urls.py +++ b/sapl/norma/urls.py @@ -2,7 +2,8 @@ from django.conf.urls import include, url from sapl.norma.views import (AssuntoNormaCrud, NormaCrud, NormaPesquisaView, NormaRelacionadaCrud, NormaTaView, TipoNormaCrud, - TipoVinculoNormaJuridicaCrud, recuperar_norma) + TipoVinculoNormaJuridicaCrud, recuperar_norma, + recuperar_numero_norma) from .apps import AppConfig @@ -25,5 +26,7 @@ urlpatterns = [ NormaPesquisaView.as_view(), name='norma_pesquisa'), url(r'^norma/recuperar-norma$', recuperar_norma, name="recuperar_norma"), + url(r'^norma/recuperar-numero-norma$', recuperar_numero_norma, + name="recuperar_numero_norma"), ] diff --git a/sapl/norma/views.py b/sapl/norma/views.py index 9120da4e9..28835a2a0 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -1,3 +1,4 @@ +from datetime import datetime from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse from django.http import JsonResponse @@ -145,6 +146,7 @@ class NormaCrud(Crud): def layout_key(self): return 'NormaJuridicaCreate' + class ListView(Crud.ListView, RedirectView): def get_redirect_url(self, *args, **kwargs): @@ -185,3 +187,22 @@ def recuperar_norma(request): response = JsonResponse({'ementa': '', 'id': 0}) return response + + +def recuperar_numero_norma(request): + tipo = TipoNormaJuridica.objects.get(pk=request.GET['tipo']) + ano = request.GET.get('ano', '') + + param = {'tipo': tipo} + param['ano'] = ano if ano else datetime.now().year + + norma = NormaJuridica.objects.filter(**param).order_by( + 'tipo', 'ano', 'numero').values_list('numero', 'ano').last() + if norma: + response = JsonResponse({'numero': int(norma[0]) + 1, + 'ano': norma[1]}) + else: + response = JsonResponse( + {'numero': 1, 'ano': ano}) + + return response diff --git a/sapl/templates/norma/normajuridica_form.html b/sapl/templates/norma/normajuridica_form.html index 7a35b05a7..9c4772a80 100644 --- a/sapl/templates/norma/normajuridica_form.html +++ b/sapl/templates/norma/normajuridica_form.html @@ -24,6 +24,23 @@ for (i = 0; i < fields.length; i++) { $(fields[i]).change(recuperar_materia); } + function recuperar_norma() { + var tipo = $("#id_tipo").val() + var ano = $("#id_ano").val() + + if (tipo) { + $.get("/norma/recuperar-numero-norma",{tipo: tipo, + ano: ano}, + function(data, status) { + $("#id_numero").val(data.numero); + $("#id_ano").val(data.ano); + }); + } + } + var fields = ["#id_tipo", "#id_ano"] + for (i = 0; i < fields.length; i++) { + $(fields[i]).change(recuperar_norma); + } {% endblock %}