Browse Source

Garante unicidade dos nomes das instâncias para cada tipo de produto

pull/159/head
Sesostris Vieira 2 years ago
parent
commit
b9ec7fd72c
  1. 12
      sigi/apps/servicos/jobs/daily/sincroniza_rancher.py
  2. 33
      sigi/apps/servicos/migrations/0016_prepare_unique_tipo_servico_instancia_data_desativacao.py
  3. 27
      sigi/apps/servicos/migrations/0017_alter_servico_options_servico_unique_instance.py
  4. 10
      sigi/apps/servicos/models.py

12
sigi/apps/servicos/jobs/daily/sincroniza_rancher.py

@ -117,8 +117,18 @@ class Job(DailyJob):
) )
try: try:
portal = Servico.objects.get(instancia=iname, tipo_servico=tipo) portal = Servico.objects.get(
instancia=iname, tipo_servico=tipo, data_desativao=None
)
encontrados += 1 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: except Servico.DoesNotExist:
if iname in self._nomes_gerados: if iname in self._nomes_gerados:
orgao = self._nomes_gerados[iname] orgao = self._nomes_gerados[iname]

33
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),
]

27
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",
),
),
]

10
sigi/apps/servicos/models.py

@ -282,6 +282,16 @@ class Servico(models.Model):
return 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): def __str__(self):
return f"{self.tipo_servico.nome} ({self.status_servico})" return f"{self.tipo_servico.nome} ({self.status_servico})"

Loading…
Cancel
Save