diff --git a/sigi/apps/eventos/jobs/daily/encerra_inscricao.py b/sigi/apps/eventos/jobs/daily/encerra_inscricao.py index 3670cf1..87b2e65 100644 --- a/sigi/apps/eventos/jobs/daily/encerra_inscricao.py +++ b/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) diff --git a/sigi/apps/eventos/jobs/daily/sincroniza_saberes.py b/sigi/apps/eventos/jobs/daily/sincroniza_saberes.py index 2e589c1..d412ee3 100644 --- a/sigi/apps/eventos/jobs/daily/sincroniza_saberes.py +++ b/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 diff --git a/sigi/apps/home/templates/menus/menu.html b/sigi/apps/home/templates/menus/menu.html index c96c926..ce8a41b 100644 --- a/sigi/apps/home/templates/menus/menu.html +++ b/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 %}
  • @@ -9,7 +9,11 @@
    {% 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 %} {{ child.title }}
    diff --git a/sigi/apps/utils/admin_urls.py b/sigi/apps/utils/admin_urls.py new file mode 100644 index 0000000..aef93f6 --- /dev/null +++ b/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//", + views.user_run_job, + name="utils_runjob", + ), +] diff --git a/sigi/apps/utils/management/jobs.py b/sigi/apps/utils/management/jobs.py index b52c93a..8eb64ca 100644 --- a/sigi/apps/utils/management/jobs.py +++ b/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}", diff --git a/sigi/apps/utils/templates/admin/jobs/job_result.html b/sigi/apps/utils/templates/admin/jobs/job_result.html new file mode 100644 index 0000000..cfabf66 --- /dev/null +++ b/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 }} + +{% endblock %} +{% block content %}{{ content|safe }}{% endblock %} \ No newline at end of file diff --git a/sigi/apps/utils/views.py b/sigi/apps/utils/views.py new file mode 100644 index 0000000..dc279b9 --- /dev/null +++ b/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()}, + ) diff --git a/sigi/menu_conf.yaml b/sigi/menu_conf.yaml index f2435f2..19b31d1 100644 --- a/sigi/menu_conf.yaml +++ b/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 diff --git a/sigi/urls.py b/sigi/urls.py index 7182899..911c29e 100644 --- a/sigi/urls.py +++ b/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/",