From 530f98290ecf14cbdea3a838d8dca39fee3ccf79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Rodrigues?= Date: Thu, 30 May 2019 11:20:34 -0300 Subject: [PATCH] =?UTF-8?q?Adiciona=20USER=20e=20IP=20em=20Mat=C3=A9riaLeg?= =?UTF-8?q?islativa=20(#2797)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adicionar user e ip em MatériaLegislativa * Refatorar Matéria Detail * Refatorar cód. que verifica alterações --- sapl/materia/forms.py | 4 ++ .../migrations/0050_auto_20190521_1148.py | 28 +++++++++++++ sapl/materia/models.py | 14 +++++++ sapl/materia/views.py | 41 ++++++++++++++++++- sapl/templates/materia/materia_detail.html | 36 ++++++++++++++++ 5 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 sapl/materia/migrations/0050_auto_20190521_1148.py create mode 100644 sapl/templates/materia/materia_detail.html diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py index 1be97fab3..b65bef4fd 100644 --- a/sapl/materia/forms.py +++ b/sapl/materia/forms.py @@ -194,6 +194,10 @@ class MateriaLegislativaForm(FileFieldCheckMixin, ModelForm): model = MateriaLegislativa exclude = ['texto_articulado', 'autores', 'proposicao', 'anexadas', 'data_ultima_atualizacao'] + widgets = { + 'user': forms.HiddenInput(), + 'ip': forms.HiddenInput() + } def __init__(self, *args, **kwargs): super(MateriaLegislativaForm, self).__init__(*args, **kwargs) diff --git a/sapl/materia/migrations/0050_auto_20190521_1148.py b/sapl/materia/migrations/0050_auto_20190521_1148.py new file mode 100644 index 000000000..1a2945e60 --- /dev/null +++ b/sapl/materia/migrations/0050_auto_20190521_1148.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-05-21 14:48 +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), + ('materia', '0049_pautareuniao'), + ] + + operations = [ + migrations.AddField( + model_name='materialegislativa', + name='ip', + field=models.CharField(blank=True, default='', max_length=30, verbose_name='IP'), + ), + migrations.AddField( + model_name='materialegislativa', + 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/materia/models.py b/sapl/materia/models.py index 182c705b9..096aabc97 100644 --- a/sapl/materia/models.py +++ b/sapl/materia/models.py @@ -279,6 +279,20 @@ class MateriaLegislativa(models.Model): auto_now=True, verbose_name=_('Data')) + 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 = _('Matéria Legislativa') verbose_name_plural = _('Matérias Legislativas') diff --git a/sapl/materia/views.py b/sapl/materia/views.py index 7eda72a34..43d6658a3 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -1605,6 +1605,16 @@ class MateriaLegislativaCrud(Crud): form_class = MateriaLegislativaForm + def form_valid(self, form): + self.object = form.save() + username = self.request.user.username + + self.object.user = self.request.user + self.object.ip = get_client_ip(self.request) + self.object.save() + + return super().form_valid(form) + @property def cancel_url(self): return self.search_url @@ -1614,8 +1624,28 @@ class MateriaLegislativaCrud(Crud): form_class = MateriaLegislativaForm def form_valid(self, form): + dict_objeto_antigo = MateriaLegislativa.objects.get( + pk=self.kwargs['pk'] + ).__dict__ + self.object = form.save() - username = self.request.user.username + dict_objeto_novo = self.object.__dict__ + + atributos = [ + 'tipo_id', 'ano', 'numero', 'data_apresentacao', 'numero_protocolo', + 'tipo_apresentacao', 'texto_original', 'apelido', 'dias_prazo', 'polemica', + 'objeto', 'regime_tramitacao_id', 'em_tramitacao', 'data_fim_prazo', + 'data_publicacao', 'complementar', 'tipo_origem_externa_id', + 'numero_origem_externa', 'ano_origem_externa', 'local_origem_externa_id', + 'data_origem_externa', 'ementa', 'indexacao', 'observacao' + ] + + 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 if Anexada.objects.filter(materia_principal=self.kwargs['pk']).exists(): materia = MateriaLegislativa.objects.get(pk=self.kwargs['pk']) @@ -1624,7 +1654,7 @@ class MateriaLegislativaCrud(Crud): for anexada in anexadas: anexada.em_tramitacao = True if form.instance.em_tramitacao else False anexada.save() - + return super().form_valid(form) @property @@ -1639,6 +1669,13 @@ class MateriaLegislativaCrud(Crud): class DetailView(Crud.DetailView): layout_key = 'MateriaLegislativaDetail' + template_name = "materia/materia_detail.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['user'] = self.request.user + context['materia'] = MateriaLegislativa.objects.get(pk=self.kwargs['pk']) + return context class ListView(Crud.ListView, RedirectView): diff --git a/sapl/templates/materia/materia_detail.html b/sapl/templates/materia/materia_detail.html new file mode 100644 index 000000000..ee1e97c9e --- /dev/null +++ b/sapl/templates/materia/materia_detail.html @@ -0,0 +1,36 @@ +{% extends "crud/detail.html" %} +{% load i18n %} + +{% block detail_content %} + {{ block.super }} + {% if user.is_superuser %} +
+ {% if materia.user %} +
+
+

Usuário

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

IP

+
+
+
{{materia.ip}}
+
+
+
+
+ {% endif %} +
+ {% endif %} +{% endblock detail_content %}