diff --git a/sigi/apps/home/templates/home/openmap.html b/sigi/apps/home/templates/home/openmap.html index f0ead55..0dd3f10 100644 --- a/sigi/apps/home/templates/home/openmap.html +++ b/sigi/apps/home/templates/home/openmap.html @@ -50,20 +50,6 @@ {% endblock %} {% block content %} - -
@@ -80,10 +66,6 @@ var options = { color: 'blue', fillColor: 'red', fillOpacity: 0.4, radius: 500 }; var unfiltred_options = { color: 'red', fillColor: 'red', fillOpacity: 0, radius: 1000 }; $(document).ready(function () { - var aviso = M.Modal.init($('#aviso')); - aviso[0].open(); - - $("input[type=checkbox]").change(filtra); mymap = L.map('map', { zoomSnap: 0.01 }).setView(map_center, 4.5); diff --git a/sigi/apps/utils/admin.py b/sigi/apps/utils/admin.py new file mode 100644 index 0000000..ac8a685 --- /dev/null +++ b/sigi/apps/utils/admin.py @@ -0,0 +1,12 @@ +from django.contrib import admin +from sigi.apps.utils.models import SigiAlert +from tinymce.models import HTMLField +from tinymce.widgets import AdminTinyMCE + + +@admin.register(SigiAlert) +class SigiAlertAdmin(admin.ModelAdmin): + list_display = ("titulo", "caminho", "destinatarios") + search_fields = ("titulo", "caminho") + formfield_overrides = {HTMLField: {"widget": AdminTinyMCE}} + list_filter = ("destinatarios",) diff --git a/sigi/apps/utils/apps.py b/sigi/apps/utils/apps.py index 58013e8..890b295 100644 --- a/sigi/apps/utils/apps.py +++ b/sigi/apps/utils/apps.py @@ -4,4 +4,4 @@ from django.utils.translation import gettext_lazy as _ class UtilsConfig(AppConfig): name = "sigi.apps.utils" - verbose_name = _("utiliátios SIGI") + verbose_name = _("utilitários SIGI") diff --git a/sigi/apps/utils/middleware.py b/sigi/apps/utils/middleware.py new file mode 100644 index 0000000..13e2f69 --- /dev/null +++ b/sigi/apps/utils/middleware.py @@ -0,0 +1,53 @@ +from django.contrib.auth.models import AnonymousUser +from django.db.models import Q +from sigi.apps.utils.models import SigiAlert +from django.template.loader import render_to_string + + +class SigiAlertsMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + response = self.get_response(request) + if response.streaming: + return response + if ( + request.method == "GET" + and response.status_code == 200 + and "Content-Type" in response.headers + and "html" in response.headers["Content-Type"] + and b"" in response.content + ): + if hasattr(request, "user"): + user = request.user + else: + user = AnonymousUser() + + destinos = ["A"] + if user.is_anonymous or not user.is_authenticated: + destinos.append("N") + if user.is_staff: + destinos.append("S") + if user.is_superuser: + destinos.append("D") + + alertas = SigiAlert.objects.filter( + Q(caminho=request.path_info) + & Q(destinatarios__in=destinos) + # & Q(Q(grupos__icontains=user.groups.all()) | Q(grupo__isnull=True)) + ) + + if len(alertas) > 0: + avisos = {} + context = {"alertas": alertas} + snippet = render_to_string( + "sigialerts/alert_snippet.html", + request=request, + context=context, + ) + snippet += "" + response.content = response.content.replace( + b"", snippet.encode("utf-8") + ) + return response diff --git a/sigi/apps/utils/migrations/0001_initial.py b/sigi/apps/utils/migrations/0001_initial.py new file mode 100644 index 0000000..871a95c --- /dev/null +++ b/sigi/apps/utils/migrations/0001_initial.py @@ -0,0 +1,30 @@ +# Generated by Django 4.0.6 on 2022-07-12 20:41 + +from django.db import migrations, models +import tinymce.models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='SigiAlert', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('caminho', models.CharField(max_length=200, verbose_name='caminho da tela')), + ('destinatarios', models.CharField(choices=[('A', 'Todo e qualquer usuário'), ('N', 'Usuários anônimos / não autenticados'), ('S', 'Membros da equipe Interlegis'), ('D', 'Administradores do sistema')], max_length=1, verbose_name='destinatários')), + ('titulo', models.CharField(max_length=60, verbose_name='título')), + ('mensagem', tinymce.models.HTMLField(verbose_name='mensagem')), + ], + options={ + 'verbose_name': 'alerta SIGI', + 'verbose_name_plural': 'alertas SIGI', + 'ordering': ('caminho', 'destinatarios'), + }, + ), + ] diff --git a/sigi/apps/utils/migrations/__init__.py b/sigi/apps/utils/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/apps/utils/models.py b/sigi/apps/utils/models.py new file mode 100644 index 0000000..2858e3f --- /dev/null +++ b/sigi/apps/utils/models.py @@ -0,0 +1,28 @@ +from pyexpat import model +from django.db import models +from django.contrib.auth.models import Group +from django.utils.translation import gettext as _ +from tinymce.models import HTMLField + + +class SigiAlert(models.Model): + DESTINATARIOS_CHOICES = ( + ("A", _("Todo e qualquer usuário")), + ("N", _("Usuários anônimos / não autenticados")), + ("S", _("Membros da equipe Interlegis")), + ("D", _("Administradores do sistema")), + ) + caminho = models.CharField(_("caminho da tela"), max_length=200) + destinatarios = models.CharField( + _("destinatários"), max_length=1, choices=DESTINATARIOS_CHOICES + ) + titulo = models.CharField(_("título"), max_length=60) + mensagem = HTMLField(_("mensagem")) + + class Meta: + ordering = ("caminho", "destinatarios") + verbose_name = _("alerta SIGI") + verbose_name_plural = _("alertas SIGI") + + def __str__(self): + return self.titulo diff --git a/sigi/apps/utils/templates/sigialerts/alert_snippet.html b/sigi/apps/utils/templates/sigialerts/alert_snippet.html new file mode 100644 index 0000000..300af06 --- /dev/null +++ b/sigi/apps/utils/templates/sigialerts/alert_snippet.html @@ -0,0 +1,90 @@ +{% load i18n %} + +
+ + info + +
+
+
+
{% trans "Novidades!" %}
+

+ {% blocktranslate count counter=alertas.count|default:0 %} + Existe um aviso para você. Clique no botão para visualizar + {% plural %} + Existem {{ counter }} avisos para você. Clique no botão para visualizar + {% endblocktranslate %} +

+
+
+ + \ No newline at end of file diff --git a/sigi/menu_conf.yaml b/sigi/menu_conf.yaml index 62c3484..a8e1036 100644 --- a/sigi/menu_conf.yaml +++ b/sigi/menu_conf.yaml @@ -8,6 +8,8 @@ admin_menu: view_name: admin:auth_group_changelist - title: Importar Gescon view_name: importar-gescon + - title: Avisos do sistema + view_name: admin:utils_sigialert_changelist main_menu: - title: Municípios diff --git a/sigi/settings.py b/sigi/settings.py index 6830066..236a96e 100644 --- a/sigi/settings.py +++ b/sigi/settings.py @@ -70,6 +70,7 @@ MIDDLEWARE = [ "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", + "sigi.apps.utils.middleware.SigiAlertsMiddleware", ] if DEBUG: