From b230416a5a3626b87c2823f49b1ee2de58445b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Rodrigues?= Date: Wed, 25 Sep 2019 16:00:00 -0300 Subject: [PATCH] Fix #615960 (OSTicket) (#2893) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adicionar migração - deduplica protocolos * Fixes #615960 (OSTicket) * Corrigir erros * Refatoração * Adicionar correção --- sapl/base/tests/test_view_base.py | 26 ------------------- sapl/base/views.py | 12 +++------ sapl/protocoloadm/forms.py | 4 +-- .../migrations/0022_deduplica_protocolos.py | 25 ++++++++++++++++++ .../migrations/0023_auto_20190711_1755.py | 19 ++++++++++++++ .../migrations/0024_merge_20190821_1418.py | 16 ++++++++++++ sapl/protocoloadm/models.py | 1 + .../templates/base/protocolos_duplicados.html | 8 +++--- 8 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 sapl/protocoloadm/migrations/0022_deduplica_protocolos.py create mode 100644 sapl/protocoloadm/migrations/0023_auto_20190711_1755.py create mode 100644 sapl/protocoloadm/migrations/0024_merge_20190821_1418.py diff --git a/sapl/base/tests/test_view_base.py b/sapl/base/tests/test_view_base.py index 44d7a3632..75748edd0 100644 --- a/sapl/base/tests/test_view_base.py +++ b/sapl/base/tests/test_view_base.py @@ -24,32 +24,6 @@ from sapl.base.views import (protocolos_duplicados, protocolos_com_materias, bancada_comissao_autor_externo, anexados_ciclicos) -@pytest.mark.django_db(transaction=False) -def test_lista_protocolos_duplicados(): - mommy.make( - Protocolo, - numero=15, - ano=2031 - ) - mommy.make( - Protocolo, - numero=15, - ano=2031 - ) - mommy.make( - Protocolo, - numero=33, - ano=2033 - ) - - lista_protocolos_duplicados = protocolos_duplicados() - - assert len(lista_protocolos_duplicados) == 1 - assert lista_protocolos_duplicados[0][1] == 2 - assert lista_protocolos_duplicados[0][0].numero == 15 - assert lista_protocolos_duplicados[0][0].ano == 2031 - - @pytest.mark.django_db(transaction=False) def test_lista_protocolos_com_materias(): mommy.make( diff --git a/sapl/base/views.py b/sapl/base/views.py index c54dc34cd..bbb926726 100644 --- a/sapl/base/views.py +++ b/sapl/base/views.py @@ -1653,14 +1653,10 @@ class ListarProtocolosComMateriasView(PermissionRequiredMixin, ListView): def protocolos_duplicados(): - protocolos = {} - for p in Protocolo.objects.order_by('-ano', 'numero'): - key = "{}/{}".format(p.numero, p.ano) - val = protocolos.get(key, list()) - val.append(p) - protocolos[key] = val - - return [(v[0], len(v)) for (k, v) in protocolos.items() if len(v) > 1] + return [ + protocolo for protocolo in Protocolo.objects.values( + 'numero', 'ano').order_by('-ano', 'numero').annotate(total=Count('numero')).filter(total__gt=1) + ] class ListarProtocolosDuplicadosView(PermissionRequiredMixin, ListView): diff --git a/sapl/protocoloadm/forms.py b/sapl/protocoloadm/forms.py index 859de2850..cae67d2ab 100644 --- a/sapl/protocoloadm/forms.py +++ b/sapl/protocoloadm/forms.py @@ -302,9 +302,7 @@ class AnularProtocoloAdmForm(ModelForm): class Meta: model = Protocolo - fields = ['numero', - 'ano', - 'justificativa_anulacao', + fields = ['justificativa_anulacao', 'anulado', 'user_anulacao', 'ip_anulacao', diff --git a/sapl/protocoloadm/migrations/0022_deduplica_protocolos.py b/sapl/protocoloadm/migrations/0022_deduplica_protocolos.py new file mode 100644 index 000000000..790030a5f --- /dev/null +++ b/sapl/protocoloadm/migrations/0022_deduplica_protocolos.py @@ -0,0 +1,25 @@ +from __future__ import unicode_literals + +from django.db import migrations + + +def deduplica_protocolos(apps, schema_editor): + from sapl.base.views import protocolos_duplicados + + Protocolo = apps.get_model('protocoloadm', 'Protocolo') + + protocolos = protocolos_duplicados() + for protocolo in protocolos: + protocolo_principal = Protocolo.objects.filter(numero=protocolo['numero'], ano=protocolo['ano']).order_by('-id')[0] + Protocolo.objects.filter(numero=protocolo['numero'], ano=protocolo['ano']).exclude(id=protocolo_principal.id).delete() + + +class Migration(migrations.Migration): + + dependencies = [ + ('protocoloadm', '0021_merge_20190429_1531'), + ] + + operations = [ + migrations.RunPython(deduplica_protocolos) + ] diff --git a/sapl/protocoloadm/migrations/0023_auto_20190711_1755.py b/sapl/protocoloadm/migrations/0023_auto_20190711_1755.py new file mode 100644 index 000000000..70620c8f7 --- /dev/null +++ b/sapl/protocoloadm/migrations/0023_auto_20190711_1755.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-07-11 20:55 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('protocoloadm', '0022_deduplica_protocolos'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='protocolo', + unique_together=set([('numero', 'ano')]), + ), + ] diff --git a/sapl/protocoloadm/migrations/0024_merge_20190821_1418.py b/sapl/protocoloadm/migrations/0024_merge_20190821_1418.py new file mode 100644 index 000000000..6bd5eea65 --- /dev/null +++ b/sapl/protocoloadm/migrations/0024_merge_20190821_1418.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-08-21 17:18 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('protocoloadm', '0023_auto_20190711_1755'), + ('protocoloadm', '0023_merge_20190802_1112'), + ] + + operations = [ + ] diff --git a/sapl/protocoloadm/models.py b/sapl/protocoloadm/models.py index 5f67054a4..4993b84e7 100644 --- a/sapl/protocoloadm/models.py +++ b/sapl/protocoloadm/models.py @@ -120,6 +120,7 @@ class Protocolo(models.Model): permissions = ( ('action_anular_protocolo', _('Permissão para Anular Protocolo')), ) + unique_together = ('numero', 'ano',) def __str__(self): return _('%(numero)s/%(ano)s') % { diff --git a/sapl/templates/base/protocolos_duplicados.html b/sapl/templates/base/protocolos_duplicados.html index 6a45a0045..be71fc499 100644 --- a/sapl/templates/base/protocolos_duplicados.html +++ b/sapl/templates/base/protocolos_duplicados.html @@ -15,12 +15,14 @@ - {% for protocolo, quantidade in protocolos_duplicados %} + {% for protocolo in protocolos_duplicados %} - {{ protocolo }} + + {{ protocolo.numero }}/{{ protocolo.ano }} + - {{ quantidade }} + {{ protocolo.total }} {% endfor %}