Browse Source

Adiciona USER e IP em MatériaLegislativa (#2797)

* Adicionar user e ip em MatériaLegislativa

* Refatorar Matéria Detail

* Refatorar cód. que verifica alterações
pull/2766/head
João Rodrigues 6 years ago
committed by Edward
parent
commit
eb232c5a81
  1. 4
      sapl/materia/forms.py
  2. 28
      sapl/materia/migrations/0050_auto_20190521_1148.py
  3. 14
      sapl/materia/models.py
  4. 41
      sapl/materia/views.py
  5. 36
      sapl/templates/materia/materia_detail.html

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

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

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

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

36
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 %}
<div class="row">
{% if materia.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' materia.user.pk %}">{{materia.user}}</a>
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% if materia.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">{{materia.ip}}</div>
</div>
</div>
</div>
</div>
{% endif %}
</div>
{% endif %}
{% endblock detail_content %}
Loading…
Cancel
Save