diff --git a/sigi/apps/servicos/jobs/daily/sincroniza_rancher.py b/sigi/apps/servicos/jobs/daily/sincroniza_rancher.py index 921194f..005cfe2 100644 --- a/sigi/apps/servicos/jobs/daily/sincroniza_rancher.py +++ b/sigi/apps/servicos/jobs/daily/sincroniza_rancher.py @@ -117,8 +117,18 @@ class Job(DailyJob): ) try: - portal = Servico.objects.get(instancia=iname, tipo_servico=tipo) + portal = Servico.objects.get( + instancia=iname, tipo_servico=tipo, data_desativao=None + ) encontrados += 1 + except Servico.MultipleObjectsReturned: + self._errors.append( + _( + f"Existe mais de um registro ativo da instância {iname}" + f" de {tipo}." + ) + ) + continue except Servico.DoesNotExist: if iname in self._nomes_gerados: orgao = self._nomes_gerados[iname] diff --git a/sigi/apps/servicos/migrations/0016_prepare_unique_tipo_servico_instancia_data_desativacao.py b/sigi/apps/servicos/migrations/0016_prepare_unique_tipo_servico_instancia_data_desativacao.py new file mode 100644 index 0000000..559ac50 --- /dev/null +++ b/sigi/apps/servicos/migrations/0016_prepare_unique_tipo_servico_instancia_data_desativacao.py @@ -0,0 +1,33 @@ +# Generated by Django 4.1.2 on 2022-10-24 13:28 + +from django.db import migrations + + +def instance_names_fw(apps, schema_editor): + Servico = apps.get_model("servicos", "Servico") + seq = 0 + for s in Servico.objects.filter(instancia="").exclude( + data_desativacao=None + ): + s.instancia = f"instancia-desativada-{seq}" + s.save() + seq += 1 + print(f"{seq} instâncias fakeadas") + + +def instance_names_rw(apps, schema_editor): + Servico = apps.get_model("servicos", "Servico") + Servico.objects.filter(instancia__startswith="instancia-desativada").update( + instancia="" + ) + + +class Migration(migrations.Migration): + + dependencies = [ + ("servicos", "0015_nomeia_instancias"), + ] + + operations = [ + migrations.RunPython(instance_names_fw, instance_names_rw), + ] diff --git a/sigi/apps/servicos/migrations/0017_alter_servico_options_servico_unique_instance.py b/sigi/apps/servicos/migrations/0017_alter_servico_options_servico_unique_instance.py new file mode 100644 index 0000000..31d211e --- /dev/null +++ b/sigi/apps/servicos/migrations/0017_alter_servico_options_servico_unique_instance.py @@ -0,0 +1,27 @@ +# Generated by Django 4.1.2 on 2022-10-24 13:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("servicos", "0016_prepare_unique_tipo_servico_instancia_data_desativacao"), + ] + + operations = [ + migrations.AlterModelOptions( + name="servico", + options={ + "verbose_name": "serviço SEIT", + "verbose_name_plural": "serviços SEIT", + }, + ), + migrations.AddConstraint( + model_name="servico", + constraint=models.UniqueConstraint( + fields=("tipo_servico", "instancia", "data_desativacao"), + name="unique_instance", + ), + ), + ] diff --git a/sigi/apps/servicos/models.py b/sigi/apps/servicos/models.py index 3158c09..ab53226 100644 --- a/sigi/apps/servicos/models.py +++ b/sigi/apps/servicos/models.py @@ -282,6 +282,16 @@ class Servico(models.Model): return + class Meta: + verbose_name = _("serviço SEIT") + verbose_name_plural = _("serviços SEIT") + constraints = [ + models.UniqueConstraint( + fields=["tipo_servico", "instancia", "data_desativacao"], + name="unique_instance", + ) + ] + def __str__(self): return f"{self.tipo_servico.nome} ({self.status_servico})"