From 6d07780dfc8f5784c5d86da2285d9100a22e3060 Mon Sep 17 00:00:00 2001 From: Edward Date: Mon, 3 Dec 2018 16:36:19 -0200 Subject: [PATCH] Fixes #2410 (#2411) --- sapl/norma/views.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/sapl/norma/views.py b/sapl/norma/views.py index 779cfcd11..f2dfb6f2e 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -274,16 +274,19 @@ def recuperar_norma(request): 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 timezone.now().year + param = {'tipo': tipo, + 'ano': ano if ano else timezone.now().year + } norma = NormaJuridica.objects.filter(**param).order_by( - 'tipo', 'ano', 'numero').values_list('numero', 'ano').last() + 'tipo', 'ano', 'numero').values_list('numero', flat=True) if norma: - response = JsonResponse({'numero': int(re.sub("[^0-9].*", '', norma[0])) + 1, - 'ano': norma[1]}) + numeros = sorted([int(re.sub("[^0-9].*", '', n)) for n in norma]) + next_num = numeros.pop() + 1 + response = JsonResponse({'numero': next_num, + 'ano': param['ano']}) else: response = JsonResponse( - {'numero': 1, 'ano': ano}) + {'numero': 1, 'ano': param['ano']}) return response