Browse Source

mudanças no sistema de cronjobs para disparos pelo admin. Gertiq #168327

pull/169/head 3.0.72
Sesóstris Vieira 11 months ago
parent
commit
943d594ca9
  1. 1
      sigi/apps/eventos/jobs/daily/encerra_inscricao.py
  2. 1
      sigi/apps/eventos/jobs/daily/sincroniza_saberes.py
  3. 8
      sigi/apps/home/templates/menus/menu.html
  4. 10
      sigi/apps/utils/admin_urls.py
  5. 25
      sigi/apps/utils/management/jobs.py
  6. 18
      sigi/apps/utils/templates/admin/jobs/job_result.html
  7. 23
      sigi/apps/utils/views.py
  8. 6
      sigi/menu_conf.yaml
  9. 2
      sigi/urls.py

1
sigi/apps/eventos/jobs/daily/encerra_inscricao.py

@ -16,6 +16,7 @@ class Job(JobReportMixin, DailyJob):
report_data = []
def do_job(self):
self.report_data = []
hoje = timezone.localtime().replace(hour=23, minute=59, second=59)
retroagir = hoje - timezone.timedelta(days=30)

1
sigi/apps/eventos/jobs/daily/sincroniza_saberes.py

@ -13,6 +13,7 @@ class Job(JobReportMixin, DailyJob):
report_data = []
def do_job(self):
self.report_data = []
infos = []
errors = []
total_sinc = 0

8
sigi/apps/home/templates/menus/menu.html

@ -1,4 +1,4 @@
{% load menus %}
{% load menus sigi_tags %}
{% for menu in menu_items %}
{% if menu.children %}
<li class="scrollspy">
@ -9,7 +9,11 @@
<div class="collapsible-body">
{% for child in menu.children %}
{% if child.view_name %}
{% url child.view_name as item_url %}
{% if child.view_param %}
{% url child.view_name child.view_param as item_url%}
{% else %}
{% url child.view_name as item_url %}
{% endif %}
<div{% if item_url in request.path and child.querystr == None or child.querystr in request.get_full_path %} class="active"{% endif %}>
<a href="{{ item_url }}{% if child.querystr %}?{{ child.querystr }}{% endif %}">{{ child.title }}</a>
</div>

10
sigi/apps/utils/admin_urls.py

@ -0,0 +1,10 @@
from django.urls import path, include
from sigi.apps.utils import views
urlpatterns = [
path(
"runjob/<str:job_name>/",
views.user_run_job,
name="utils_runjob",
),
]

25
sigi/apps/utils/management/jobs.py

@ -86,12 +86,17 @@ class JobReportMixin:
)
print(rst)
def report(self, start_time, end_time):
if self.report_data is None:
raise MisconfiguredError(
"Job needs to define 'report_data' property"
)
def prepare_report(self, start_time, end_time):
"""Prepara RST e HTML do relatório do JOB
Args:
start_time (datetime): Timestamp do início da execução
end_time (datetime): Timestamp do término da execução
Returns:
tupla(rst: str, html:str): Retorna o relatório do job formatado em
RST e HTML.
"""
rst = render_to_string(
self.report_template,
{
@ -109,6 +114,16 @@ class JobReportMixin:
"output_encoding": "unicode",
},
)
return (rst, html)
def report(self, start_time, end_time):
if self.report_data is None:
raise MisconfiguredError(
"Job needs to define 'report_data' property"
)
rst, html = self.prepare_report(start_time, end_time)
if self.send_report_mail:
mail_admins(
subject=f"JOB: {self.help}",

18
sigi/apps/utils/templates/admin/jobs/job_result.html

@ -0,0 +1,18 @@
{% extends "admin/base_site.html" %}
{% load static i18n %}
{% block extrastyle %}
{{ block.super }}
<style type="text/css">
h1 {
font-size: 2.5rem;
}
h2 {
font-size: 2rem;
}
h3 {
font-size: 1.5rem;
}
</style>
{% endblock %}
{% block content %}{{ content|safe }}{% endblock %}

23
sigi/apps/utils/views.py

@ -0,0 +1,23 @@
from parsel import Selector
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.utils import timezone
from django_extensions.management.jobs import get_job
@login_required
@user_passes_test(lambda user: user.is_superuser)
def user_run_job(request, job_name):
job = get_job(None, job_name)()
start_time = timezone.localtime()
job.do_job()
end_time = timezone.localtime()
rst, html = job.prepare_report(start_time, end_time)
dp = Selector(text=html)
return render(
request,
"admin/jobs/job_result.html",
{"content": dp.xpath("//body/*").get()},
)

6
sigi/menu_conf.yaml

@ -12,6 +12,12 @@ admin_menu:
view_name: admin:parlamentares_senador_changelist
- title: Avisos do sistema
view_name: admin:utils_sigialert_changelist
- title: Sincroniza eventos com Saberes
view_name: utils_runjob
view_param: sincroniza_saberes
- title: Encerra inscrições de eventos
view_name: utils_runjob
view_param: encerra_inscricao
main_menu:
- title: Municípios

2
sigi/urls.py

@ -13,6 +13,7 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from rest_framework.schemas import get_schema_view
from django.conf import settings
from django.conf.urls.static import static
@ -30,6 +31,7 @@ urlpatterns = [
path("admin/eventos/", include("sigi.apps.eventos.admin_urls")),
path("admin/convenios/", include("sigi.apps.convenios.urls")),
path("admin/ocorrencias/", include("sigi.apps.ocorrencias.admin_urls")),
path("admin/utils/", include("sigi.apps.utils.admin_urls")),
path("admin/", admin.site.urls),
path(
"api/",

Loading…
Cancel
Save