From cab54d80202e044af9206cefddb122e9557d5e8d Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Mon, 3 Oct 2016 16:17:00 -0300 Subject: [PATCH] Add masterdetailcrud para Legislacao Citada --- sapl/crud/base.py | 3 -- sapl/materia/forms.py | 30 +++++++++++++---- sapl/materia/views.py | 12 ++++--- sapl/norma/models.py | 3 ++ sapl/norma/views.py | 2 +- .../materia/materialegislativa_filter.html | 2 +- .../test_inicializa_grupos_autorizacoes.py | 32 ++++++++++++++++++- 7 files changed, 67 insertions(+), 17 deletions(-) diff --git a/sapl/crud/base.py b/sapl/crud/base.py index 060153993..87f8050fd 100644 --- a/sapl/crud/base.py +++ b/sapl/crud/base.py @@ -1086,9 +1086,6 @@ class MasterDetailCrud(Crud): def get_url_regex(cls): return r'^%s/(?P\d+)/edit$' % cls.model._meta.model_name - def get(self, request, *args, **kwargs): - return Crud.UpdateView.get(self, request, *args, **kwargs) - class DeleteView(Crud.DeleteView): permission_required = RP_DELETE, diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py index f4575658a..c33c5bc7b 100644 --- a/sapl/materia/forms.py +++ b/sapl/materia/forms.py @@ -364,17 +364,35 @@ class LegislacaoCitadaForm(ModelForm): else: cleaned_data['norma'] = norma - if LegislacaoCitada.objects.filter( + filtro_base = LegislacaoCitada.objects.filter( materia=self.instance.materia, - norma=cleaned_data['norma'] - ).exists(): - msg = _('Essa Legislação já foi cadastrada.') - raise ValidationError(msg) - + norma=self.cleaned_data['norma'], + disposicoes=self.cleaned_data['disposicoes'], + parte=self.cleaned_data['parte'], + livro=self.cleaned_data['livro'], + titulo=self.cleaned_data['titulo'], + capitulo=self.cleaned_data['capitulo'], + secao=self.cleaned_data['secao'], + subsecao=self.cleaned_data['subsecao'], + artigo=self.cleaned_data['artigo'], + paragrafo=self.cleaned_data['paragrafo'], + inciso=self.cleaned_data['inciso'], + alinea=self.cleaned_data['alinea'], + item=self.cleaned_data['item']) + + if not self.instance.id: + if filtro_base.exists(): + msg = _('Essa Legislação já foi cadastrada.') + raise ValidationError(msg) + else: + if filtro_base.exclude(id=self.instance.id).exists(): + msg = _('Essa Legislação já foi cadastrada.') + raise ValidationError(msg) return cleaned_data def save(self, commit=False): legislacao = super(LegislacaoCitadaForm, self).save(commit) + legislacao.norma = self.cleaned_data['norma'] legislacao.save() return legislacao diff --git a/sapl/materia/views.py b/sapl/materia/views.py index d5b13e4a9..3b12c145e 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -29,7 +29,7 @@ from sapl.crispy_layout_mixin import SaplFormLayout, form_actions, to_row from sapl.crud.base import (RP_DETAIL, RP_LIST, Crud, CrudAux, CrudDetailView, MasterDetailCrud, make_pagination, ACTION_CREATE, ACTION_UPDATE, ACTION_LIST, - ACTION_DELETE) + ACTION_DELETE, ACTION_DETAIL) from sapl.materia.forms import AnexadaForm, LegislacaoCitadaForm from sapl.norma.models import LegislacaoCitada from sapl.utils import (TURNO_TRAMITACAO_CHOICES, YES_NO_CHOICES, autor_label, @@ -731,15 +731,17 @@ class LegislacaoCitadaCrud(MasterDetailCrud): @property def create_url(self): - obj = self.crud if hasattr(self, 'crud') else self - if not obj.CreateView: - return '' return self.resolve_url(ACTION_CREATE, args=(self.kwargs['pk'],))\ if self.request.user.has_module_perms('materia') else '' + @property + def detail_url(self): + return self.resolve_url(ACTION_DETAIL, args=(self.object.id,))\ + if self.request.user.has_module_perms('materia') else '' + @property def update_url(self): - return self.resolve_url(ACTION_CREATE, args=(self.kwargs['pk'],))\ + return self.resolve_url(ACTION_UPDATE, args=(self.kwargs['pk'],))\ if self.request.user.has_module_perms('materia') else '' @property diff --git a/sapl/norma/models.py b/sapl/norma/models.py index c03c4c305..55f07ad16 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -167,6 +167,9 @@ class LegislacaoCitada(models.Model): verbose_name = _('Legislação') verbose_name_plural = _('Legislações') + def __str__(self): + return str(self.norma) + class VinculoNormaJuridica(models.Model): TIPO_VINCULO_CHOICES = ( diff --git a/sapl/norma/views.py b/sapl/norma/views.py index f00494323..c2dd31724 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -12,7 +12,7 @@ from .models import (AssuntoNorma, LegislacaoCitada, NormaJuridica, TipoNormaJuridica) -LegislacaoCitadaCrud = Crud.build(LegislacaoCitada, '') +# LegislacaoCitadaCrud = Crud.build(LegislacaoCitada, '') AssuntoNormaCrud = CrudAux.build(AssuntoNorma, 'assunto_norma_juridica', diff --git a/sapl/templates/materia/materialegislativa_filter.html b/sapl/templates/materia/materialegislativa_filter.html index ec49f450a..48bcaf1d6 100644 --- a/sapl/templates/materia/materialegislativa_filter.html +++ b/sapl/templates/materia/materialegislativa_filter.html @@ -4,7 +4,7 @@ {% block actions %}
- {% if user.is_authenticated %} + {% if perms.materia %} {% blocktrans with verbose_name=view.verbose_name %} Adicionar Matéria Legislativa {% endblocktrans %} diff --git a/scripts/test_inicializa_grupos_autorizacoes.py b/scripts/test_inicializa_grupos_autorizacoes.py index f356e85aa..9498ac40b 100644 --- a/scripts/test_inicializa_grupos_autorizacoes.py +++ b/scripts/test_inicializa_grupos_autorizacoes.py @@ -1,6 +1,9 @@ from django.apps import apps from django.contrib.auth.models import Group, Permission from django.contrib.contenttypes.models import ContentType +from django.utils.translation import string_concat + +from django.utils.translation import ugettext_lazy as _ import pytest from inicializa_grupos_autorizacoes import cria_grupos_permissoes @@ -17,12 +20,25 @@ def test_grupo_padrao_tem_permissoes_sobre_todo_o_app(app_label): app = apps.get_app_config(app_label) + for klass in list(app.get_models()): + opts = klass._meta + permissions = ( + ("list_" + opts.model_name, + string_concat( + _('Visualizaçao da lista de'), ' ', + opts.verbose_name_plural)), + ("detail_" + opts.model_name, + string_concat( + _('Visualização dos detalhes de'), ' ', + opts.verbose_name_plural)), + ) + # código testado cria_grupos_permissoes() def gerar_permissoes(app): for model in app.get_models(): - for op in ['add', 'change', 'delete']: + for op in ['add', 'change', 'delete', 'list', 'detail']: yield model, 'Can %s %s' % (op, model._meta.verbose_name) grupo = Group.objects.get(name='Operador de %s' % app.verbose_name) esperado = set(gerar_permissoes(app)) @@ -36,6 +52,20 @@ def test_grupo_padrao_tem_permissoes_sobre_todo_o_app(app_label): def test_permissoes_extras_sao_apagadas(app_label): app = apps.get_app_config(app_label) + + for klass in list(app.get_models()): + opts = klass._meta + permissions = ( + ("list_" + opts.model_name, + string_concat( + _('Visualizaçao da lista de'), ' ', + opts.verbose_name_plural)), + ("detail_" + opts.model_name, + string_concat( + _('Visualização dos detalhes de'), ' ', + opts.verbose_name_plural)), + ) + grupo = Group.objects.create(name='Operador de %s' % app.verbose_name) permissao_errada = Permission.objects.create(