Browse Source

Fix #2872 - Adiciona User e IP em NormaJuridica (#2875)

* Fix #2872 - Adiciona User e IP em NormaJuridica

* Verifica se assuntos são diferentes
pull/2882/head
Cesar Augusto de Carvalho 6 years ago
committed by Cesar Carvalho
parent
commit
8f9bc4c52a
  1. 2
      sapl/materia/views.py
  2. 9
      sapl/norma/forms.py
  3. 28
      sapl/norma/migrations/0025_auto_20190704_1403.py
  4. 18
      sapl/norma/models.py
  5. 51
      sapl/norma/views.py
  6. 32
      sapl/templates/norma/normajuridica_detail.html

2
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)

9
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):

28
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'),
),
]

18
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')

51
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__)

32
sapl/templates/norma/normajuridica_detail.html

@ -87,6 +87,38 @@
</div>
</div>
<br><br>
{% if user.is_superuser %}
<div class="row">
{% if object.user %}
<div class="col-sm-6">
<div id="div_id_user" class="form-group">
<p class="control-label">Usuário</p>
<div class="controls">
<div class="form-control-static">
<div class="dont-break-out">
<a href="{% url 'sapl.base:user_edit' object.user.pk %}">{{object.user}}</a>
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% if object.ip %}
<div class="col-sm-6">
<div id="div_ip_user" class="form-group">
<p class="control-label">IP</p>
<div class="controls">
<div class="form-control-static">
<div class="dont-break-out">{{object.ip}}</div>
</div>
</div>
</div>
</div>
{% endif %}
</div>
{% endif %}
{% if object.texto_articulado.exists and object.texto_articulado.first.has_view_permission %}
<hr />
<div class="row">

Loading…
Cancel
Save