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 0ba92b737..0dfcbd417 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -137,8 +137,13 @@ class NormaJuridicaForm(FileFieldCheckMixin, ModelForm): 'texto_integral', 'assuntos', 'norma_de_destaque', - 'apelido'] - widgets = {'assuntos': widgets.CheckboxSelectMultiple} + 'apelido', + '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 52e797165..308ac48b0 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() @@ -145,6 +147,20 @@ class NormaJuridica(models.Model): apelido = models.TextField( blank=True, verbose_name=_('Apelido')) + 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 9dde99eb3..6f35d3db5 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) @@ -224,20 +224,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' @@ -256,7 +260,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 @@ -265,6 +269,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 %}