Browse Source

Subst uso do model User pelo indicado no README

pull/626/head
LeandroRoberto 8 years ago
parent
commit
242f7712ed
  1. 12
      sapl/materia/forms.py
  2. 8
      sapl/materia/models.py
  3. 10
      sapl/materia/tests/test_materia.py
  4. 29
      sapl/materia/views.py
  5. 8
      scripts/inicializa_grupos_autorizacoes.py

12
sapl/materia/forms.py

@ -1,16 +1,17 @@
from datetime import datetime from datetime import datetime
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 Group, User from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.contrib.auth.password_validation import validate_password 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, transaction from django.db import models, 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 _
import django_filters
from sapl.comissoes.models import Comissao from sapl.comissoes.models import Comissao
from sapl.crispy_layout_mixin import form_actions, to_row from sapl.crispy_layout_mixin import form_actions, to_row
@ -25,6 +26,7 @@ from .models import (AcompanhamentoMateria, Anexada, Autor, Autoria,
Numeracao, Proposicao, Relatoria, TipoMateriaLegislativa, Numeracao, Proposicao, Relatoria, TipoMateriaLegislativa,
Tramitacao, UnidadeTramitacao) Tramitacao, UnidadeTramitacao)
ANO_CHOICES = [('', '---------')] + RANGE_ANOS ANO_CHOICES = [('', '---------')] + RANGE_ANOS
@ -670,11 +672,11 @@ class AutorForm(ModelForm):
return True return True
def valida_email_existente(self): def valida_email_existente(self):
return User.objects.filter( return get_user_model().objects.filter(
email=self.cleaned_data['email']).exists() email=self.cleaned_data['email']).exists()
def usuario_existente(self): def usuario_existente(self):
return User.objects.filter( return get_user_model().objects.filter(
username=self.cleaned_data['username']).exists() username=self.cleaned_data['username']).exists()
def clean(self): def clean(self):
@ -721,7 +723,7 @@ class AutorForm(ModelForm):
autor = super(AutorForm, self).save(commit) autor = super(AutorForm, self).save(commit)
try: try:
u = User.objects.get( u = get_user_model().objects.get(
username=autor.username, username=autor.username,
email=autor.email) email=autor.email)
except ObjectDoesNotExist: except ObjectDoesNotExist:

8
sapl/materia/models.py

@ -1,4 +1,4 @@
from django.contrib.auth.models import Group, User from django.contrib.auth.models import Group
from django.db import models from django.db import models
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
@ -6,7 +6,8 @@ from model_utils import Choices
from sapl.comissoes.models import Comissao from sapl.comissoes.models import Comissao
from sapl.parlamentares.models import Parlamentar, Partido from sapl.parlamentares.models import Parlamentar, Partido
from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES, from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES,
restringe_tipos_de_arquivo_txt, xstr) restringe_tipos_de_arquivo_txt, xstr,
get_settings_auth_user_model)
def grupo_autor(): def grupo_autor():
@ -208,7 +209,8 @@ class TipoAutor(models.Model):
class Autor(models.Model): class Autor(models.Model):
user = models.ForeignKey(User, blank=True, null=True) user = models.ForeignKey(
get_settings_auth_user_model(), blank=True, null=True)
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)

10
sapl/materia/tests/test_materia.py

@ -1,8 +1,8 @@
import pytest from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from model_mommy import mommy from model_mommy import mommy
import pytest
from sapl.comissoes.models import Comissao, TipoComissao from sapl.comissoes.models import Comissao, TipoComissao
from sapl.materia.models import (Anexada, Autor, Autoria, DespachoInicial, from sapl.materia.models import (Anexada, Autor, Autoria, DespachoInicial,
@ -434,7 +434,7 @@ def test_form_errors_relatoria(admin_client):
@pytest.mark.django_db(transaction=False) @pytest.mark.django_db(transaction=False)
def test_proposicao_submit(admin_client): def test_proposicao_submit(admin_client):
tipo_autor = mommy.make(TipoAutor, descricao='Teste Tipo_Autor') tipo_autor = mommy.make(TipoAutor, descricao='Teste Tipo_Autor')
user = User.objects.filter(is_active=True)[0] user = get_user_model().objects.filter(is_active=True)[0]
autor = mommy.make( autor = mommy.make(
Autor, Autor,
@ -469,7 +469,7 @@ def test_proposicao_submit(admin_client):
def test_form_errors_proposicao(admin_client): def test_form_errors_proposicao(admin_client):
tipo_autor = mommy.make(TipoAutor, descricao='Teste Tipo_Autor') tipo_autor = mommy.make(TipoAutor, descricao='Teste Tipo_Autor')
user = User.objects.filter(is_active=True)[0] user = get_user_model().objects.filter(is_active=True)[0]
autor = mommy.make( autor = mommy.make(
Autor, Autor,
@ -484,7 +484,7 @@ def test_form_errors_proposicao(admin_client):
{'autor': autor.pk, {'autor': autor.pk,
'justificativa_devolucao': ' ', 'justificativa_devolucao': ' ',
'texto_original': texto, 'texto_original': texto,
'salvar': 'salvar'}, 'salvar': 'salvar'},
follow=True) follow=True)
assert (response.context_data['form'].errors['tipo'] == assert (response.context_data['form'].errors['tipo'] ==

29
sapl/materia/views.py

@ -6,8 +6,8 @@ from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, Button from crispy_forms.layout import HTML, Button
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from django.contrib.auth.models import User
from django.contrib.auth.tokens import default_token_generator from django.contrib.auth.tokens import default_token_generator
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.core.mail import send_mail from django.core.mail import send_mail
@ -54,6 +54,7 @@ from .models import (AcompanhamentoMateria, Anexada, Autor, Autoria,
TipoFimRelatoria, TipoMateriaLegislativa, TipoProposicao, TipoFimRelatoria, TipoMateriaLegislativa, TipoProposicao,
Tramitacao, UnidadeTramitacao) Tramitacao, UnidadeTramitacao)
AnexadaCrud = Crud.build(Anexada, '') AnexadaCrud = Crud.build(Anexada, '')
@ -62,6 +63,7 @@ class OrigemCrud(Crud):
help_path = 'origem' help_path = 'origem'
class BaseMixin(PermissionRequiredMixin, CrudBaseMixin): class BaseMixin(PermissionRequiredMixin, CrudBaseMixin):
def has_permission(self): def has_permission(self):
return permissao_tb_aux(self) return permissao_tb_aux(self)
@ -71,6 +73,7 @@ class TipoMateriaCrud(Crud):
help_path = 'tipo_materia_legislativa' help_path = 'tipo_materia_legislativa'
class BaseMixin(PermissionRequiredMixin, CrudBaseMixin): class BaseMixin(PermissionRequiredMixin, CrudBaseMixin):
def has_permission(self): def has_permission(self):
return permissao_tb_aux(self) return permissao_tb_aux(self)
@ -80,6 +83,7 @@ class RegimeTramitacaoCrud(Crud):
help_path = 'regime_tramitacao' help_path = 'regime_tramitacao'
class BaseMixin(PermissionRequiredMixin, CrudBaseMixin): class BaseMixin(PermissionRequiredMixin, CrudBaseMixin):
def has_permission(self): def has_permission(self):
return permissao_tb_aux(self) return permissao_tb_aux(self)
@ -89,6 +93,7 @@ class TipoDocumentoCrud(Crud):
help_path = 'tipo_documento' help_path = 'tipo_documento'
class BaseMixin(PermissionRequiredMixin, CrudBaseMixin): class BaseMixin(PermissionRequiredMixin, CrudBaseMixin):
def has_permission(self): def has_permission(self):
return permissao_tb_aux(self) return permissao_tb_aux(self)
@ -98,6 +103,7 @@ class TipoFimRelatoriaCrud(Crud):
help_path = 'fim_relatoria' help_path = 'fim_relatoria'
class BaseMixin(PermissionRequiredMixin, CrudBaseMixin): class BaseMixin(PermissionRequiredMixin, CrudBaseMixin):
def has_permission(self): def has_permission(self):
return permissao_tb_aux(self) return permissao_tb_aux(self)
@ -107,6 +113,7 @@ class TipoAutorCrud(Crud):
help_path = 'tipo_autor' help_path = 'tipo_autor'
class BaseMixin(PermissionRequiredMixin, CrudBaseMixin): class BaseMixin(PermissionRequiredMixin, CrudBaseMixin):
def has_permission(self): def has_permission(self):
return permissao_tb_aux(self) return permissao_tb_aux(self)
@ -130,7 +137,8 @@ class AutorCrud(Crud):
pk_autor = Autor.objects.get( pk_autor = Autor.objects.get(
email=self.request.POST.get('email')).id email=self.request.POST.get('email')).id
kwargs = {} kwargs = {}
user = User.objects.get(email=self.request.POST.get('email')) user = get_user_model().objects.get(
email=self.request.POST.get('email'))
kwargs['token'] = default_token_generator.make_token(user) kwargs['token'] = default_token_generator.make_token(user)
kwargs['uidb64'] = urlsafe_base64_encode(force_bytes(user.pk)) kwargs['uidb64'] = urlsafe_base64_encode(force_bytes(user.pk))
assunto = "SAPL - Confirmação de Conta" assunto = "SAPL - Confirmação de Conta"
@ -159,7 +167,7 @@ class ConfirmarEmailView(TemplateView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
uid = urlsafe_base64_decode(self.kwargs['uidb64']) uid = urlsafe_base64_decode(self.kwargs['uidb64'])
user = User.objects.get(id=uid) user = get_user_model().objects.get(id=uid)
user.is_active = True user.is_active = True
user.save() user.save()
context = self.get_context_data(**kwargs) context = self.get_context_data(**kwargs)
@ -181,6 +189,7 @@ class TipoProposicaoCrud(Crud):
help_path = 'tipo_proposicao' help_path = 'tipo_proposicao'
class BaseMixin(PermissionRequiredMixin, CrudBaseMixin): class BaseMixin(PermissionRequiredMixin, CrudBaseMixin):
def has_permission(self): def has_permission(self):
return permissao_tb_aux(self) return permissao_tb_aux(self)
@ -453,7 +462,7 @@ class ProposicaoCrud(Crud):
id=self.kwargs['pk'], id=self.kwargs['pk'],
autor__user_id=self.request.user.id) autor__user_id=self.request.user.id)
if (not proposicao.data_recebimento or if (not proposicao.data_recebimento or
proposicao.data_devolucao): proposicao.data_devolucao):
return True return True
else: else:
msg = _('Essa proposição já foi recebida. ' + msg = _('Essa proposição já foi recebida. ' +
@ -544,13 +553,13 @@ class ReciboProposicaoView(TemplateView):
permission_required = permissoes_autor() permission_required = permissoes_autor()
def has_permission(self): def has_permission(self):
perms = self.get_permission_required() perms = self.get_permission_required()
if not self.request.user.has_perms(perms): if not self.request.user.has_perms(perms):
return False return False
return (Proposicao.objects.filter( return (Proposicao.objects.filter(
id=self.kwargs['pk'], id=self.kwargs['pk'],
autor__user_id=self.request.user.id).exists()) autor__user_id=self.request.user.id).exists())
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(ReciboProposicaoView, self).get_context_data( context = super(ReciboProposicaoView, self).get_context_data(

8
scripts/inicializa_grupos_autorizacoes.py

@ -1,5 +1,6 @@
from django.apps import apps from django.apps import apps
from django.contrib.auth.models import Group, Permission, User from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
@ -12,7 +13,7 @@ def cria_ou_reseta_grupo(nome):
def cria_usuario(nome, grupo): def cria_usuario(nome, grupo):
nome_usuario = nome nome_usuario = nome
usuario = User.objects.get_or_create(username=nome_usuario)[0] usuario = get_user_model().objects.get_or_create(username=nome_usuario)[0]
usuario.set_password('interlegis') usuario.set_password('interlegis')
usuario.save() usuario.save()
grupo.user_set.add(usuario) grupo.user_set.add(usuario)
@ -82,7 +83,8 @@ def cria_grupos_permissoes():
# Cria o Usuario # Cria o Usuario
nome_usuario = 'operador_%s' % nome_app nome_usuario = 'operador_%s' % nome_app
usuario = User.objects.get_or_create(username=nome_usuario)[0] usuario = get_user_model().objects.get_or_create(
username=nome_usuario)[0]
usuario.set_password('interlegis') usuario.set_password('interlegis')
usuario.save() usuario.save()
grupo.user_set.add(usuario) grupo.user_set.add(usuario)

Loading…
Cancel
Save