diff --git a/sigi/apps/servicos/__init__.py b/sigi/apps/servicos/__init__.py index b86b8b7..4f8b58e 100644 --- a/sigi/apps/servicos/__init__.py +++ b/sigi/apps/servicos/__init__.py @@ -17,3 +17,22 @@ def generate_instance_name(orgao): return re.sub("\W+", "", to_ascii(orgao.nome)).lower() else: return f"{orgao.tipo.sigla.lower()}-{orgao.municipio.uf.sigla.lower()}" + + +def nomeia_instancias(servicos, user=None): + from django.contrib.admin.models import LogEntry, CHANGE + from django.contrib.contenttypes.models import ContentType + from django.utils.translation import gettext as _ + + for s in servicos.filter(instancia=""): + s.instancia = generate_instance_name(s.casa_legislativa) + s.save() + if user: + LogEntry.objects.log_action( + user_id=user.id, + content_type_id=ContentType.objects.get_for_model(type(s)).pk, + object_id=s.id, + object_repr=str(s), + action_flag=CHANGE, + change_message=_("Adicionado nome automático da instância"), + ) diff --git a/sigi/apps/servicos/jobs/daily/sincroniza_rancher.py b/sigi/apps/servicos/jobs/daily/sincroniza_rancher.py index 5969807..c578908 100644 --- a/sigi/apps/servicos/jobs/daily/sincroniza_rancher.py +++ b/sigi/apps/servicos/jobs/daily/sincroniza_rancher.py @@ -1,14 +1,10 @@ -import datetime -import docutils.core import json import shutil from django.conf import settings -from django.core.mail import mail_admins -from django.template.loader import render_to_string from django.utils import timezone from django.utils.translation import gettext as _ from django_extensions.management.jobs import DailyJob -from sigi.apps.servicos import generate_instance_name +from sigi.apps.servicos import generate_instance_name, nomeia_instancias from sigi.apps.servicos.models import Servico, TipoServico from sigi.apps.casas.models import Orgao from sigi.apps.utils.mixins import JobReportMixin @@ -43,7 +39,12 @@ class Job(JobReportMixin, DailyJob): } def process(self, tipo): - self.nomeia_instancias(tipo) + nomeia_instancias( + servicos=Servico.objects.filter( + tipo_servico=tipo, data_desativacao=None, instancia="" + ), + user=self.sys_user, + ) NAO_CONSTA = "*não-consta-no-rancher*" self.errors[tipo] = [] self.infos[tipo] = [] @@ -210,13 +211,3 @@ class Job(JobReportMixin, DailyJob): self.infos[tipo].append( _(f"{desativados} {tipo.nome} desativados no SIGI") ) - - # Preenche o nome das instâncias com o nome padrão para os serviços com - # este campo em branco - def nomeia_instancias(self, tipo): - for s in Servico.objects.filter( - tipo_servico=tipo, data_desativacao=None, instancia="" - ): - s.instancia = generate_instance_name(s.casa_legislativa) - s.save() - self.admin_log_change(s, "Adicionado nome automático da instância")