diff --git a/sapl/parlamentares/apps.py b/sapl/parlamentares/apps.py index dd523cd49..49c8f20aa 100644 --- a/sapl/parlamentares/apps.py +++ b/sapl/parlamentares/apps.py @@ -1,38 +1,10 @@ from django import apps +from django.core.exceptions import ObjectDoesNotExist from django.db.models.signals import post_migrate from django.utils.translation import ugettext_lazy as _ -from django.core.exceptions import ObjectDoesNotExist - - -def criar_grupo(permission): - from django.contrib.auth.models import Group - - g = Group.objects.get_or_create(name='Votante') - g[0].permissions.add(permission) - - -def criar_permissao(sender, **kwargs): - from django.contrib.auth.models import Permission - from django.contrib.contenttypes.models import ContentType - - try: - content_type = ContentType.objects.get( - app_label='parlamentares', - model='Votante') - except ObjectDoesNotExist: - content_type = ContentType.objects.create( - app_label='parlamentares', - model='Votante') - - p = Permission.objects.get_or_create( - name='Can Vote', codename='can_vote', content_type=content_type) - criar_grupo(p[0]) class AppConfig(apps.AppConfig): name = 'sapl.parlamentares' label = 'parlamentares' verbose_name = _('Parlamentares') - - def ready(self): - post_migrate.connect(criar_permissao, sender=self) diff --git a/sapl/parlamentares/forms.py b/sapl/parlamentares/forms.py index a5f23697b..54022912e 100644 --- a/sapl/parlamentares/forms.py +++ b/sapl/parlamentares/forms.py @@ -1,9 +1,9 @@ from datetime import date, timedelta -from django.contrib.auth.models import Group from crispy_forms.helper import FormHelper from crispy_forms.layout import Fieldset, Layout from django import forms +from django.contrib.auth.models import Group from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db import transaction @@ -13,6 +13,7 @@ from django.utils.translation import ugettext_lazy as _ from floppyforms.widgets import ClearableFileInput from sapl.crispy_layout_mixin import form_actions, to_row +from sapl.rules import SAPL_GROUP_VOTANTE from .models import (ComposicaoColigacao, Filiacao, Frente, Legislatura, Mandato, Parlamentar, Votante) @@ -228,10 +229,10 @@ class ComposicaoColigacaoForm(ModelForm): class FrenteForm(ModelForm): def __init__(self, *args, **kwargs): - super(FrenteForm, self).__init__(*args, **kwargs) - self.fields['parlamentares'].queryset = Parlamentar.objects.filter( - ativo=True).order_by('nome_completo') - self.fields['parlamentares'].label = _('Parlamentares \ + super(FrenteForm, self).__init__(*args, **kwargs) + self.fields['parlamentares'].queryset = Parlamentar.objects.filter( + ativo=True).order_by('nome_completo') + self.fields['parlamentares'].label = _('Parlamentares \ (Mantenha CTRL pressionado para selecionar vários)') class Meta: @@ -317,7 +318,7 @@ class VotanteForm(ModelForm): u.save() # Adiciona user ao grupo - g = Group.objects.filter(name='Votante')[0] + g = Group.objects.filter(name=SAPL_GROUP_VOTANTE)[0] u.groups.add(g) votante.user = u diff --git a/sapl/parlamentares/migrations/0040_auto_20170214_1709.py b/sapl/parlamentares/migrations/0040_auto_20170214_1709.py new file mode 100644 index 000000000..5c28f30ca --- /dev/null +++ b/sapl/parlamentares/migrations/0040_auto_20170214_1709.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.12 on 2017-02-14 17:09 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('parlamentares', '0039_remove_votante_ip'), + ] + + operations = [ + migrations.AlterModelOptions( + name='votante', + options={'permissions': (('can_vote', 'Can Vote'),), 'verbose_name': 'Usuário', 'verbose_name_plural': 'Usuários'}, + ), + ] diff --git a/sapl/parlamentares/models.py b/sapl/parlamentares/models.py index a03d63c24..1c5e38e80 100644 --- a/sapl/parlamentares/models.py +++ b/sapl/parlamentares/models.py @@ -5,6 +5,7 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ from model_utils import Choices +from sapl.api import permissions from sapl.base.models import Autor from sapl.utils import (INDICADOR_AFASTAMENTO, UF, YES_NO_CHOICES, SaplGenericRelation, intervalos_tem_intersecao, @@ -509,6 +510,9 @@ class Votante(models.Model): class Meta: verbose_name = _('Usuário') verbose_name_plural = _('Usuários') + permissions = ( + ('can_vote', _('Can Vote')), + ) def __str__(self): return self.user.username diff --git a/sapl/rules/__init__.py b/sapl/rules/__init__.py index 6c5987c64..bc4d27653 100644 --- a/sapl/rules/__init__.py +++ b/sapl/rules/__init__.py @@ -28,6 +28,7 @@ SAPL_GROUP_PAINEL = _("Operador de Painel Eletrônico") SAPL_GROUP_GERAL = _("Operador Geral") SAPL_GROUP_AUTOR = _("Autor") SAPL_GROUP_PARLAMENTAR = _("Parlamentar") +SAPL_GROUP_VOTANTE = _("Votante") # TODO - funcionalidade ainda não existe mas está aqui para efeito de anotação SAPL_GROUP_LOGIN_SOCIAL = _("Usuários com Login Social") @@ -48,6 +49,7 @@ SAPL_GROUPS = [ SAPL_GROUP_GERAL, SAPL_GROUP_AUTOR, SAPL_GROUP_PARLAMENTAR, + SAPL_GROUP_VOTANTE, SAPL_GROUP_LOGIN_SOCIAL, SAPL_GROUP_ANONYMOUS, ] diff --git a/sapl/rules/map_rules.py b/sapl/rules/map_rules.py index f5565b372..c132045c4 100644 --- a/sapl/rules/map_rules.py +++ b/sapl/rules/map_rules.py @@ -13,7 +13,8 @@ from sapl.rules import (SAPL_GROUP_ADMINISTRATIVO, SAPL_GROUP_ANONYMOUS, SAPL_GROUP_MATERIA, SAPL_GROUP_NORMA, SAPL_GROUP_PAINEL, SAPL_GROUP_PARLAMENTAR, SAPL_GROUP_PROTOCOLO, SAPL_GROUP_SESSAO, - RP_LIST, RP_DETAIL, RP_ADD, RP_CHANGE, RP_DELETE) + RP_LIST, RP_DETAIL, RP_ADD, RP_CHANGE, RP_DELETE, + SAPL_GROUP_VOTANTE) from sapl.sessao import models as sessao @@ -178,6 +179,13 @@ rules_group_parlamentar = { 'rules': [] } +rules_group_votante = { + 'group': SAPL_GROUP_VOTANTE, + 'rules': [ + (parlamentares.Votante, ['can_vote']) + ] +} + rules_group_geral = { 'group': SAPL_GROUP_GERAL, 'rules': [ @@ -232,6 +240,7 @@ rules_group_geral = { (parlamentares.CargoMesa, __base__), (parlamentares.ComposicaoMesa, __base__), (parlamentares.Frente, __base__), + (parlamentares.Votante, __base__), (sessao.CargoBancada, __base__), (sessao.Bancada, __base__), @@ -305,6 +314,7 @@ rules_patterns = [ rules_group_geral, rules_group_autor, rules_group_parlamentar, + rules_group_votante, rules_group_anonymous, # anotação para validação do teste de rules rules_group_login_social # TODO não implementado