From 02677ea2ab2592639102bd2f819ff528dbbdcb5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Rodrigues?= Date: Tue, 10 Dec 2019 19:07:52 -0300 Subject: [PATCH] HOT-FIX - deduplica_protocolos --- .../migrations/0022_deduplica_protocolos.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/sapl/protocoloadm/migrations/0022_deduplica_protocolos.py b/sapl/protocoloadm/migrations/0022_deduplica_protocolos.py index 790030a5f..0ff516db7 100644 --- a/sapl/protocoloadm/migrations/0022_deduplica_protocolos.py +++ b/sapl/protocoloadm/migrations/0022_deduplica_protocolos.py @@ -7,12 +7,19 @@ def deduplica_protocolos(apps, schema_editor): from sapl.base.views import protocolos_duplicados Protocolo = apps.get_model('protocoloadm', 'Protocolo') + DocumentoAdministrativo = apps.get_model('protocoloadm', 'DocumentoAdministrativo') 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() - + for p in protocolos: + principal = Protocolo.objects.filter(numero=p['numero'], ano=p['ano']).order_by('-id').first() + replicas = Protocolo.objects.filter(numero=p['numero'], ano=p['ano']).exclude(id=principal.id) + + for r in replicas: + documentos = DocumentoAdministrativo.objects.filter(protocolo_id=r.id) + for d in documentos: + d.protocolo = principal + d.save() + replicas.delete() class Migration(migrations.Migration):