Browse Source

Importação do Gescon a cada hora

pull/163/head 3.0.35
Sesostris Vieira 2 years ago
parent
commit
2b45e837ca
  1. 1
      docker/Dockerfile
  2. 5
      sigi/apps/convenios/jobs/hourly/importa_gescon.py
  3. 17
      sigi/apps/convenios/migrations/0034_gescon_checksums.py
  4. 18
      sigi/apps/convenios/models.py

1
docker/Dockerfile

@ -93,7 +93,6 @@ RUN mkdir -p /var/log/sigi
# schedule cron jobs
RUN crontab -l | { cat; echo "* * * * * /usr/local/bin/python ${HOME}/manage.py runjobs minutely >> /var/log/sigi/cron.log 2>&1"; } | crontab -
RUN crontab -l | { cat; echo "0 * * * * /usr/local/bin/python ${HOME}/manage.py runjobs hourly >> /var/log/sigi/cron.log 2>&1"; } | crontab -
RUN crontab -l | { cat; echo "0 */4 * * * /usr/local/bin/python ${HOME}/manage.py runjob importa_gescon >> /var/log/sigi/cron.log 2>&1"; } | crontab -
RUN crontab -l | { cat; echo "0 0 * * * /usr/local/bin/python ${HOME}/manage.py runjobs daily >> /var/log/sigi/cron.log 2>&1"; } | crontab -
RUN crontab -l | { cat; echo "0 0 * * 0 /usr/local/bin/python ${HOME}/manage.py runjobs weekly >> /var/log/sigi/cron.log 2>&1"; } | crontab -
RUN crontab -l | { cat; echo "0 0 1 * * /usr/local/bin/python ${HOME}/manage.py runjobs monthly >> /var/log/sigi/cron.log 2>&1"; } | crontab -

5
sigi/apps/convenios/jobs/importa_gescon.py → sigi/apps/convenios/jobs/hourly/importa_gescon.py

@ -2,11 +2,12 @@ import datetime
import docutils.core
from django.core.mail import mail_admins
from django.utils.translation import gettext as _
from django_extensions.management.jobs import HourlyJob
from sigi.apps.convenios.models import Gescon
from sigi.apps.utils.management.jobs import JobReportMixin, QuarterDailyJob
from sigi.apps.utils.management.jobs import JobReportMixin
class Job(JobReportMixin, QuarterDailyJob):
class Job(JobReportMixin, HourlyJob):
help = "Carga de dados do Gescon."
def do_job(self):

17
sigi/apps/convenios/migrations/0034_gescon_checksums.py

@ -0,0 +1,17 @@
# Generated by Django 4.1.7 on 2023-03-29 18:58
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("convenios", "0033_alter_projeto_modelo_minuta_and_more"),
]
operations = [
migrations.AddField(
model_name="gescon",
name="checksums",
field=models.JSONField(null=True),
),
]

18
sigi/apps/convenios/models.py

@ -1,5 +1,6 @@
import re
import requests
from hashlib import md5
from pathlib import Path
from django.db import models
from django.db.models import Q, fields
@ -561,6 +562,7 @@ class Gescon(models.Model):
ultima_importacao = models.TextField(
_("Resultado da última importação"), blank=True
)
checksums = models.JSONField(null=True)
class Meta:
verbose_name = _("Configuração do Gescon")
@ -599,6 +601,8 @@ class Gescon(models.Model):
def importa_contratos(self):
self.ultima_importacao = ""
if self.checksums is None:
self.checksums = {}
self.add_message(
_(
f"Importação iniciada em {timezone.localtime():%d/%m/%Y %H:%M:%S}\n"
@ -642,6 +646,8 @@ class Gescon(models.Model):
if re.sub("[^\d]", "", o.cnpj) != ""
}
requests.packages.urllib3.disable_warnings()
for sigla_gescon, sigla_sigi in subespecies:
self.add_message(_(f"\n**Importando subespécie {sigla_gescon}**"))
url = self.url_gescon.format(s=sigla_gescon)
@ -669,6 +675,17 @@ class Gescon(models.Model):
)
continue
md5sum = md5(response.text.encode(response.encoding)).hexdigest()
if (
sigla_gescon in self.checksums
and self.checksums[sigla_gescon] == md5sum
):
self.add_message(
f"Dados da subespécie {sigla_gescon} inalterados no Gescon."
" Processamento desnecessário."
)
continue
contratos = response.json()
# Pegar só os contratos que possuem alguma das palavras-chave
@ -914,6 +931,7 @@ class Gescon(models.Model):
f"{erros} reportados com erro."
)
)
self.checksums[sigla_gescon] = md5sum
self.save()

Loading…
Cancel
Save