From 78f90502b381622180bc069a0bf7bd8d426d0c19 Mon Sep 17 00:00:00 2001 From: Cesar Augusto de Carvalho Date: Fri, 5 Jul 2019 10:21:27 -0300 Subject: [PATCH] Fix #2872 - Adiciona User e IP em NormaJuridica (#2875) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix #2872 - Adiciona User e IP em NormaJuridica * Verifica se assuntos são diferentes --- sapl/materia/views.py | 2 +- sapl/norma/forms.py | 8 ++- .../migrations/0025_auto_20190704_1403.py | 28 ++++++++++ sapl/norma/models.py | 18 ++++++- sapl/norma/views.py | 51 ++++++++++++++++--- .../templates/norma/normajuridica_detail.html | 32 ++++++++++++ 6 files changed, 129 insertions(+), 10 deletions(-) create mode 100644 sapl/norma/migrations/0025_auto_20190704_1403.py diff --git a/sapl/materia/views.py b/sapl/materia/views.py index 7e64c6dd0..0736de351 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -1621,7 +1621,7 @@ class MateriaLegislativaCrud(Crud): form_class = MateriaLegislativaForm def get_initial(self): - initial = super(CreateView, self).get_initial() + initial = super().get_initial() initial['user'] = self.request.user initial['ip'] = get_client_ip(self.request) diff --git a/sapl/norma/forms.py b/sapl/norma/forms.py index c01688611..0298e1a8d 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -132,8 +132,12 @@ class NormaJuridicaForm(FileFieldCheckMixin, ModelForm): 'indexacao', 'observacao', 'texto_integral', - 'assuntos'] - widgets = {'assuntos': widgets.CheckboxSelectMultiple} + 'assuntos', + 'user', + 'ip'] + widgets = {'assuntos': widgets.CheckboxSelectMultiple, + 'user': forms.HiddenInput(), + 'ip': forms.HiddenInput()} def clean(self): diff --git a/sapl/norma/migrations/0025_auto_20190704_1403.py b/sapl/norma/migrations/0025_auto_20190704_1403.py new file mode 100644 index 000000000..15db2793b --- /dev/null +++ b/sapl/norma/migrations/0025_auto_20190704_1403.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-07-04 17:03 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('norma', '0024_auto_20190425_0917'), + ] + + operations = [ + migrations.AddField( + model_name='normajuridica', + name='ip', + field=models.CharField(blank=True, default='', max_length=30, verbose_name='IP'), + ), + migrations.AddField( + model_name='normajuridica', + name='user', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL, verbose_name='Usuário'), + ), + ] diff --git a/sapl/norma/models.py b/sapl/norma/models.py index 6b3a18a1f..cd90e4930 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -10,7 +10,9 @@ from sapl.base.models import Autor from sapl.compilacao.models import TextoArticulado from sapl.materia.models import MateriaLegislativa from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES, - restringe_tipos_de_arquivo_txt, texto_upload_path) + restringe_tipos_de_arquivo_txt, + texto_upload_path, + get_settings_auth_user_model) @reversion.register() @@ -138,6 +140,20 @@ class NormaJuridica(models.Model): through_fields=('norma', 'autor'), symmetrical=False) + user = models.ForeignKey( + get_settings_auth_user_model(), + verbose_name=_('Usuário'), + on_delete=models.PROTECT, + null=True, + blank=True + ) + ip = models.CharField( + verbose_name=_('IP'), + max_length=30, + blank=True, + default='' + ) + class Meta: verbose_name = _('Norma Jurídica') verbose_name_plural = _('Normas Jurídicas') diff --git a/sapl/norma/views.py b/sapl/norma/views.py index 735d595a3..8d9a62eaa 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -20,7 +20,7 @@ from sapl.base.models import AppConfig from sapl.compilacao.views import IntegracaoTaView from sapl.crud.base import (RP_DETAIL, RP_LIST, Crud, CrudAux, MasterDetailCrud, make_pagination) -from sapl.utils import show_results_filter_set +from sapl.utils import show_results_filter_set, get_client_ip from .forms import (AnexoNormaJuridicaForm, NormaFilterSet, NormaJuridicaForm, NormaPesquisaSimplesForm, NormaRelacionadaForm, AutoriaNormaForm) @@ -217,20 +217,24 @@ class NormaCrud(Crud): return self.search_url def get_initial(self): - username = self.request.user.username + initial = super().get_initial() + initial['user'] = self.request.user + initial['ip'] = get_client_ip(self.request) + + username = self.request.user.username try: self.logger.debug( 'user=' + username + '. Tentando obter objeto de modelo da esfera da federação.') esfera = sapl.base.models.AppConfig.objects.last( ).esfera_federacao - self.initial['esfera_federacao'] = esfera + initial['esfera_federacao'] = esfera except: self.logger.error( 'user=' + username + '. Erro ao obter objeto de modelo da esfera da federação.') pass - self.initial['complemento'] = False - return self.initial + initial['complemento'] = False + return initial layout_key = 'NormaJuridicaCreate' @@ -249,7 +253,7 @@ class NormaCrud(Crud): layout_key = 'NormaJuridicaCreate' def get_initial(self): - initial = super(UpdateView, self).get_initial() + initial = super().get_initial() norma = NormaJuridica.objects.get(id=self.kwargs['pk']) if norma.materia: initial['tipo_materia'] = norma.materia.tipo @@ -258,6 +262,41 @@ class NormaCrud(Crud): initial['esfera_federacao'] = norma.esfera_federacao return initial + def form_valid(self, form): + norma_antiga = NormaJuridica.objects.get( + pk=self.kwargs['pk'] + ) + + # Feito desta forma para que sejam materializados os assuntos antigos + assuntos_antigos = set(norma_antiga.assuntos.all()) + + dict_objeto_antigo = norma_antiga.__dict__ + self.object = form.save() + dict_objeto_novo = self.object.__dict__ + + atributos = ['tipo_id', 'numero', 'ano', 'data', 'esfera_federacao', + 'complemento', 'materia_id', 'numero', + 'data_publicacao', 'data_vigencia', + 'veiculo_publicacao', 'pagina_inicio_publicacao', + 'pagina_fim_publicacao', 'ementa', 'indexacao', + 'observacao', 'texto_integral'] + + for atributo in atributos: + if dict_objeto_antigo[atributo] != dict_objeto_novo[atributo]: + self.object.user = self.request.user + self.object.ip = get_client_ip(self.request) + self.object.save() + break + + # Campo Assuntos não veio no __dict__, então é comparado separadamente + assuntos_novos = set(self.object.assuntos.all()) + if assuntos_antigos != assuntos_novos: + self.object.user = self.request.user + self.object.ip = get_client_ip(self.request) + self.object.save() + + return super().form_valid(form) + def recuperar_norma(request): logger = logging.getLogger(__name__) diff --git a/sapl/templates/norma/normajuridica_detail.html b/sapl/templates/norma/normajuridica_detail.html index e45a7e486..7d8509830 100644 --- a/sapl/templates/norma/normajuridica_detail.html +++ b/sapl/templates/norma/normajuridica_detail.html @@ -87,6 +87,38 @@ +

+ + {% if user.is_superuser %} +
+ {% if object.user %} +
+
+

Usuário

+
+
+ +
+
+
+
+ {% endif %} + {% if object.ip %} +
+
+

IP

+
+
+
{{object.ip}}
+
+
+
+
+ {% endif %} +
+ {% endif %} {% if object.texto_articulado.exists and object.texto_articulado.first.has_view_permission %}