Browse Source

Inicia a refatoracao de autor

pull/508/head
Eduardo Calil 9 years ago
parent
commit
5fe59d8407
  1. 94
      sapl/materia/forms.py
  2. 9
      sapl/materia/layouts.yaml
  3. 37
      sapl/materia/migrations/0039_auto_20160628_1251.py
  4. 24
      sapl/materia/models.py
  5. 11
      sapl/materia/views.py
  6. 21
      sapl/parlamentares/migrations/0023_auto_20160628_1247.py

94
sapl/materia/forms.py

@ -4,8 +4,11 @@ import django_filters
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, Button, Column, Fieldset, Layout from crispy_forms.layout import HTML, Button, Column, Fieldset, Layout
from django import forms from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.password_validation import validate_password
from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import models from django.db import models
from django.db import transaction
from django.db.models import Max from django.db.models import Max
from django.forms import ModelForm from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -542,3 +545,94 @@ class AutoriaForm(ModelForm):
raise ValidationError(msg) raise ValidationError(msg)
return self.cleaned_data return self.cleaned_data
class AutorForm(ModelForm):
senha = forms.CharField(
max_length=20,
label=_('Senha'),
required=True,
widget=forms.PasswordInput())
senha_confirma = forms.CharField(
max_length=20,
label=_('Confirmar Senha'),
required=True,
widget=forms.PasswordInput())
confirma_email = forms.EmailField(
required=True,
label=_('Confirmar Email'))
class Meta:
model = Autor
fields = ['username',
'senha',
'email',
'nome',
'tipo',
'cargo',
'parlamentar',
'comissao']
def valida_igualdade(self, texto1, texto2, msg):
if texto1 != texto2:
raise ValidationError(msg)
return True
def valida_email_existente(self):
return User.objects.filter(
email=self.cleaned_data['email']).exists()
def clean(self):
if ('senha' not in self.cleaned_data or
'senha_confirma' not in self.cleaned_data):
raise ValidationError(_('Favor informar as senhas'))
msg = _('As senhas não conferem.')
self.valida_igualdade(
self.cleaned_data['senha'],
self.cleaned_data['senha_confirma'],
msg)
if ('email' not in self.cleaned_data or
'confirma_email' not in self.cleaned_data):
raise ValidationError(_('Favor informar endereços de email'))
msg = _('Os emails não conferem.')
self.valida_igualdade(
self.cleaned_data['email'],
self.cleaned_data['confirma_email'],
msg)
email_existente = self.valida_email_existente()
if email_existente:
msg = _('Esse email já foi cadastrado.')
raise ValidationError(msg)
try:
validate_password(self.cleaned_data['senha'])
except ValidationError as error:
raise ValidationError(error)
return self.cleaned_data
@transaction.atomic
def save(self, commit=False):
autor = super(AutorForm, self).save(commit)
u = User.objects.get_or_create(
username=autor.username,
email=autor.email)
u = u[0]
u.set_password(self.cleaned_data['senha'])
u.is_active = False
u.save()
autor.user = u
autor.save()
return autor

9
sapl/materia/layouts.yaml

@ -53,7 +53,14 @@ TipoAutor:
Autor: Autor:
Autor: Autor:
- tipo:3 nome - tipo:3 nome
- username - username:6 cargo
AutorCreate:
Autor:
- tipo:3 nome
- username:4 senha:4 senha_confirma:4
- email:6 confirma_email:6
- cargo:4 parlamentar:4 comissao:4
Autoria: Autoria:
Autoria: Autoria:

37
sapl/materia/migrations/0039_auto_20160628_1251.py

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.5 on 2016-06-28 15:51
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import sapl.materia.models
class Migration(migrations.Migration):
dependencies = [
('auth', '0007_alter_validators_add_error_messages'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('materia', '0038_auto_20160612_1506'),
]
operations = [
migrations.AddField(
model_name='autor',
name='email',
field=models.EmailField(default=' ', max_length=254, verbose_name='Email'),
preserve_default=False,
),
migrations.AddField(
model_name='autor',
name='grupo_usuario',
field=models.ForeignKey(default=sapl.materia.models.grupo_autor, on_delete=django.db.models.deletion.CASCADE, to='auth.Group'),
),
migrations.AddField(
model_name='autor',
name='user',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
]

24
sapl/materia/models.py

@ -1,4 +1,5 @@
from django.db import models from django.db import models
from django.contrib.auth.models import Group, User
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from model_utils import Choices from model_utils import Choices
@ -8,6 +9,14 @@ from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES,
restringe_tipos_de_arquivo_txt, xstr) restringe_tipos_de_arquivo_txt, xstr)
def grupo_autor():
try:
grupo = Group.objects.get(name='Autor')
except Group.DoesNotExist:
return None
return grupo.id
class TipoMateriaLegislativa(models.Model): class TipoMateriaLegislativa(models.Model):
sigla = models.CharField(max_length=5, verbose_name=_('Sigla')) sigla = models.CharField(max_length=5, verbose_name=_('Sigla'))
descricao = models.CharField(max_length=50, verbose_name=_('Descrição ')) descricao = models.CharField(max_length=50, verbose_name=_('Descrição '))
@ -199,6 +208,7 @@ class TipoAutor(models.Model):
class Autor(models.Model): class Autor(models.Model):
user = models.ForeignKey(User)
partido = models.ForeignKey(Partido, blank=True, null=True) partido = models.ForeignKey(Partido, blank=True, null=True)
comissao = models.ForeignKey(Comissao, blank=True, null=True) comissao = models.ForeignKey(Comissao, blank=True, null=True)
parlamentar = models.ForeignKey(Parlamentar, blank=True, null=True) parlamentar = models.ForeignKey(Parlamentar, blank=True, null=True)
@ -206,18 +216,24 @@ class Autor(models.Model):
nome = models.CharField( nome = models.CharField(
max_length=50, blank=True, verbose_name=_('Autor')) max_length=50, blank=True, verbose_name=_('Autor'))
cargo = models.CharField(max_length=50, blank=True) cargo = models.CharField(max_length=50, blank=True)
username = models.CharField(max_length=50, blank=True) username = models.CharField(
max_length=50,
blank=True,
verbose_name=_('Nome de Usuário'))
email = models.EmailField(
verbose_name=_('Email'))
grupo_usuario = models.ForeignKey(Group, default=grupo_autor)
class Meta: class Meta:
verbose_name = _('Autor') verbose_name = _('Autor')
verbose_name_plural = _('Autores') verbose_name_plural = _('Autores')
def __str__(self): def __str__(self):
if str(self.tipo) == 'Parlamentar': if str(self.tipo) == 'Parlamentar' and self.parlamentar:
return self.parlamentar.nome_parlamentar return self.parlamentar.nome_parlamentar
elif str(self.tipo) == 'Comissao': elif str(self.tipo) == 'Comissao' and self.comissao:
return str(self.comissao) return str(self.comissao)
elif str(self.tipo) == 'Partido': elif str(self.tipo) == 'Partido' and self.partido:
return str(self.partido) return str(self.partido)
else: else:
if str(self.cargo): if str(self.cargo):

11
sapl/materia/views.py

@ -25,8 +25,8 @@ from sapl.norma.models import LegislacaoCitada
from sapl.utils import (autor_label, autor_modal, get_base_url, from sapl.utils import (autor_label, autor_modal, get_base_url,
permissoes_materia, permissao_tb_aux) permissoes_materia, permissao_tb_aux)
from .forms import (AcompanhamentoMateriaForm, AnexadaForm, AutoriaForm, from .forms import (AcompanhamentoMateriaForm, AnexadaForm, AutorForm,
DespachoInicialForm, DocumentoAcessorioForm, AutoriaForm, DespachoInicialForm, DocumentoAcessorioForm,
LegislacaoCitadaForm, MateriaLegislativaFilterSet, LegislacaoCitadaForm, MateriaLegislativaFilterSet,
NumeracaoForm, ProposicaoForm, RelatoriaForm, NumeracaoForm, ProposicaoForm, RelatoriaForm,
TramitacaoForm, UnidadeTramitacaoForm, TramitacaoForm, UnidadeTramitacaoForm,
@ -102,9 +102,16 @@ class AutorCrud(Crud):
help_path = 'autor' help_path = 'autor'
class BaseMixin(PermissionRequiredMixin, CrudBaseMixin): class BaseMixin(PermissionRequiredMixin, CrudBaseMixin):
list_field_names = ['tipo', 'nome',
'username', 'cargo']
def has_permission(self): def has_permission(self):
return permissao_tb_aux(self) return permissao_tb_aux(self)
class CreateView(CrudCreateView):
form_class = AutorForm
layout_key = 'AutorCreate'
class OrgaoCrud(Crud): class OrgaoCrud(Crud):
model = Orgao model = Orgao

21
sapl/parlamentares/migrations/0023_auto_20160628_1247.py

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.5 on 2016-06-28 15:47
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('parlamentares', '0022_auto_20160624_1124'),
]
operations = [
migrations.AlterField(
model_name='sessaolegislativa',
name='legislatura',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='parlamentares.Legislatura', verbose_name='Legislatura'),
),
]
Loading…
Cancel
Save