From 5debab804f03c556a9d7f1c0f3ef44b1672e54ea Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Thu, 13 Oct 2016 15:07:11 -0300 Subject: [PATCH 1/6] Fix #734 --- sapl/comissoes/views.py | 22 +--- sapl/crud/base.py | 109 ++++++++++++++---- .../comissoes/composicao_detail.html | 33 ------ sapl/templates/crud/detail_detail.html | 3 + 4 files changed, 94 insertions(+), 73 deletions(-) delete mode 100644 sapl/templates/comissoes/composicao_detail.html diff --git a/sapl/comissoes/views.py b/sapl/comissoes/views.py index b0c2551c3..8d3d80e88 100644 --- a/sapl/comissoes/views.py +++ b/sapl/comissoes/views.py @@ -3,7 +3,7 @@ from django.core.urlresolvers import reverse from django.db.models import F from django.views.generic import ListView -from sapl.crud.base import Crud, CrudAux, MasterDetailCrud +from sapl.crud.base import Crud, CrudAux, MasterDetailCrud, RP_DETAIL, RP_LIST from sapl.materia.models import MateriaLegislativa, Tramitacao from .models import (CargoComissao, Comissao, Composicao, Participacao, @@ -27,40 +27,30 @@ TipoComissaoCrud = CrudAux.build( class ParticipacaoCrud(MasterDetailCrud): model = Participacao parent_field = 'composicao__comissao' + public = [RP_DETAIL, ] + ListView = None + is_m2m = True class BaseMixin(MasterDetailCrud.BaseMixin): list_field_names = ['composicao', 'parlamentar', 'cargo'] - class DetailView(MasterDetailCrud.DetailView): - permission_required = [] - class ComposicaoCrud(MasterDetailCrud): model = Composicao parent_field = 'comissao' model_set = 'participacao_set' - - class ListView(MasterDetailCrud.ListView): - permission_required = [] - - class DetailView(MasterDetailCrud.DetailView): - permission_required = [] + public = [RP_LIST, RP_DETAIL, ] class ComissaoCrud(Crud): model = Comissao help_path = 'modulo_comissoes' + public = [RP_LIST, RP_DETAIL, ] class BaseMixin(Crud.BaseMixin): list_field_names = ['nome', 'sigla', 'tipo', 'data_criacao', 'ativa'] ordering = '-ativa', 'sigla' - class ListView(Crud.ListView): - permission_required = [] - - class DetailView(Crud.DetailView): - permission_required = [] - class MateriasTramitacaoListView(ListView): template_name = "comissoes/materias_em_tramitacao.html" diff --git a/sapl/crud/base.py b/sapl/crud/base.py index c7ec4d640..a870e697f 100644 --- a/sapl/crud/base.py +++ b/sapl/crud/base.py @@ -94,6 +94,7 @@ variáveis do crud: ordered_list = False desativa os clicks e controles de ord da listagem parent_field = parentesco reverso separado por '__' namespace + return_parent_field_url """ @@ -978,20 +979,23 @@ class MasterDetailCrud(Crud): parent_object = getattr(parent_object, field) else: parent_object = getattr(self.object, obj.parent_field) + if not isinstance(parent_object, models.Model): if parent_object.count() > 1: if 'pkk' not in self.request.GET: - raise Http404 + raise Http404() root_pk = self.request.GET['pkk'] parent_object = parent_object.filter(id=root_pk) parent_object = parent_object.first() if not parent_object: - raise Http404 + raise Http404() + root_pk = parent_object.pk else: - root_pk = self.kwargs['pk'] # in list and create + root_pk = self.kwargs['pk'] if 'pkk' not in self.request.GET\ + else self.request.GET['pkk'] kwargs.setdefault('root_pk', root_pk) context = super(CrudBaseMixin, self).get_context_data(**kwargs) @@ -1066,11 +1070,11 @@ class MasterDetailCrud(Crud): obj = self.crud if hasattr(self, 'crud') else self form = super(MasterDetailCrud.CreateView, self).get_form( self.form_class) - if not obj.is_m2m: - parent_field = obj.parent_field.split('__')[0] - field = self.model._meta.get_field(parent_field) + parent_field = obj.parent_field.split('__') + if not obj.is_m2m or len(parent_field) > 1: + field = self.model._meta.get_field(parent_field[0]) parent = field.related_model.objects.get(pk=self.kwargs['pk']) - setattr(form.instance, parent_field, parent) + setattr(form.instance, parent_field[0], parent) return form def get_context_data(self, **kwargs): @@ -1118,8 +1122,14 @@ class MasterDetailCrud(Crud): def get_success_url(self): obj = self.crud if hasattr(self, 'crud') else self - parent_object = getattr( - self.get_object(), obj.parent_field) + if '__' in obj.parent_field: + fields = obj.parent_field.split('__') + parent_object = self.object + for field in fields: + parent_object = getattr(parent_object, field) + break + else: + parent_object = getattr(self.object, obj.parent_field) if not isinstance(parent_object, models.Model): if parent_object.count() > 1: if 'pkk' not in self.request.GET: @@ -1134,7 +1144,12 @@ class MasterDetailCrud(Crud): root_pk = parent_object.pk pk = root_pk - return self.resolve_url(ACTION_LIST, args=(pk,)) + + namespace = parent_object._meta.app_config.name + return reverse('%s:%s' % ( + namespace, + '%s_%s' % (parent_object._meta.model_name, ACTION_DETAIL)), + args=(pk,)) class DetailView(Crud.DetailView): permission_required = RP_DETAIL, @@ -1147,6 +1162,10 @@ class MasterDetailCrud(Crud): @property def detail_list_url(self): obj = self.crud if hasattr(self, 'crud') else self + + if not obj.ListView: + return '' + if not obj.ListView.permission_required or\ self.request.user.has_perm(self.permission(RP_LIST)): if '__' in obj.parent_field: @@ -1197,8 +1216,20 @@ class MasterDetailCrud(Crud): if not parent_object: raise Http404 root_pk = parent_object.pk - pk = root_pk - return self.resolve_url(ACTION_CREATE, args=(pk,)) + + url = self.resolve_url(ACTION_CREATE, args=(root_pk,)) + if not obj.is_m2m: + return url + else: + if '__' in obj.parent_field: + fields = obj.parent_field.split('__') + parent_object = self.object + for field in fields: + parent_object = getattr(parent_object, field) + else: + parent_object = getattr(self.object, obj.parent_field) + + return url + '?pkk=' + str(parent_object.pk) else: return '' @@ -1208,29 +1239,59 @@ class MasterDetailCrud(Crud): if hasattr(obj, 'model_set') and obj.model_set\ and self.request.user.has_perm( self.permission_set(RP_ADD)): - root_pk = self.object .pk + root_pk = self.object.pk pk = root_pk - return self.resolve_url_set(ACTION_CREATE, args=(pk,)) + + url = self.resolve_url_set(ACTION_CREATE, args=(pk,)) + if not obj.is_m2m: + return url + else: + if '__' in obj.parent_field: + fields = obj.parent_field.split('__') + parent_object = self.object + for field in fields: + parent_object = getattr(parent_object, field) + else: + parent_object = getattr(self.object, obj.parent_field) + + return url + '?pkk=' + str(parent_object.pk) + else: return '' @property def detail_root_detail_url(self): - """ - Implementar retorno para o parent_field imediato no caso de - edição em cascata de MasterDetailDetail... - """ + obj = self.crud if hasattr(self, 'crud') else self + if hasattr(obj, 'parent_field'): + parent_field = obj.parent_field.split('__') + if not obj.is_m2m or len(parent_field) > 1: + field = self.model._meta.get_field(parent_field[0]) + + if isinstance(getattr( + self.object, parent_field[0]), models.Model): + parent_object = getattr(self.object, parent_field[0]) + + root_pk = parent_object.pk + pk = root_pk + + namespace = parent_object._meta.app_config.name + return reverse('%s:%s' % ( + namespace, + '%s_%s' % (parent_object._meta.model_name, + ACTION_DETAIL)), + args=(pk,)) return '' + @property + def detail_root_detail_verbose_name(self): obj = self.crud if hasattr(self, 'crud') else self if hasattr(obj, 'parent_field'): - # parent_field = obj.parent_field.split('__')[0] + parent_field = obj.parent_field.split('__') + if not obj.is_m2m or len(parent_field) > 1: + field = self.model._meta.get_field(parent_field[0]) - root_pk = self.object .pk - pk = root_pk - return self.resolve_url(ACTION_DELETE, args=(pk,)) - else: - return '' + return field.verbose_name + return '' @classonlymethod def build(cls, model, parent_field, help_path, diff --git a/sapl/templates/comissoes/composicao_detail.html b/sapl/templates/comissoes/composicao_detail.html deleted file mode 100644 index f47aa9c99..000000000 --- a/sapl/templates/comissoes/composicao_detail.html +++ /dev/null @@ -1,33 +0,0 @@ -{% extends "crud/detail.html" %} -{% load i18n %} -{% load crispy_forms_tags %} -{% block detail_content %} - - - - - - - - - - - - - {% for participacao in participacoes %} - - - - - - - - - - {% endfor %} -
NomeCargoTitularDesignaçãoDesligamentoMotivoObservação
{{participacao.parlamentar.nome_parlamentar}}{{participacao.cargo}}{{participacao.titular|yesno:"Sim,Não"}}{{participacao.data_designacao|date:"d/m/Y"}}{{participacao.data_desligamento|date:"d/m/Y"|default:"-"}}{{participacao.motivo_desligamento|default:"-"}}{{participacao.observacao|default:"-"}}
- - {% if user.is_authenticated %} - Incluir Parlamentar - {% endif %} -{% endblock detail_content %} diff --git a/sapl/templates/crud/detail_detail.html b/sapl/templates/crud/detail_detail.html index c72be069a..9f5472e97 100644 --- a/sapl/templates/crud/detail_detail.html +++ b/sapl/templates/crud/detail_detail.html @@ -6,6 +6,9 @@
{% block actions %}
+ {% if view.detail_root_detail_url %} + {% trans 'Retornar para' %} {{view.detail_root_detail_verbose_name}} + {% endif %} {% if view.detail_list_url %} {% trans 'Listar' %} {{view.verbose_name_plural}} {% endif %} From ffcf6271f359dabce6baa229f5c0742cbfac30fa Mon Sep 17 00:00:00 2001 From: Edward Ribeiro Date: Thu, 13 Oct 2016 15:26:31 -0300 Subject: [PATCH 2/6] HOT-FIX: Mesa diretora --- sapl/parlamentares/views.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index 0298eec97..55733dc22 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -267,9 +267,14 @@ class MesaDiretoraView(FormView): not SessaoLegislativa.objects.exists()): return self.validation(request) - mesa = SessaoLegislativa.objects.filter( + sessao = SessaoLegislativa.objects.filter( legislatura=Legislatura.objects.first()).first( - ).composicaomesa_set.all() + ) + + if sessao: + mesa = sessao.composicaomesa_set.all() + else: + mesa = [] cargos_ocupados = [m.cargo for m in mesa] cargos = CargoMesa.objects.all() From cca5cd23a412180cae90e3c0689fbee17e8fef0c Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Thu, 6 Oct 2016 18:56:25 -0300 Subject: [PATCH 3/6] Arruma cadastro de legislatura e de legislura edit Signed-off-by: Luciano Almeida --- sapl/parlamentares/forms.py | 9 ++++++++- sapl/parlamentares/views.py | 17 ++++++++++++++--- sapl/templates/parlamentares/layouts.yaml | 3 ++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/sapl/parlamentares/forms.py b/sapl/parlamentares/forms.py index b09c054ec..827ed4f6b 100644 --- a/sapl/parlamentares/forms.py +++ b/sapl/parlamentares/forms.py @@ -24,7 +24,6 @@ class LegislaturaForm(ModelForm): def clean(self): cleaned_data = self.cleaned_data - numero = cleaned_data['numero'] data_inicio = cleaned_data['data_inicio'] data_fim = cleaned_data['data_fim'] data_eleicao = cleaned_data['data_eleicao'] @@ -33,6 +32,14 @@ class LegislaturaForm(ModelForm): raise ValidationError(_('A data início deve ser menor que a ' + 'data fim, e a data eleição deve ser ' + 'menor que a data início')) + return cleaned_data + + +class LegislaturaCreateForm(LegislaturaForm): + + def clean(self): + cleaned_data = super(LegislaturaCreateForm, self).clean() + numero = cleaned_data['numero'] if Legislatura.objects.filter(numero=numero).exists(): raise ValidationError( _('Já cadastrada uma legislatura com este número')) diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index 55733dc22..2f243395a 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -11,8 +11,8 @@ from sapl.crud.base import (RP_CHANGE, RP_DETAIL, RP_LIST, Crud, CrudAux, from sapl.materia.models import Proposicao, Relatoria from sapl.parlamentares.apps import AppConfig -from .forms import (FiliacaoForm, LegislaturaForm, ParlamentarCreateForm, - ParlamentarForm) +from .forms import (FiliacaoForm, LegislaturaForm, LegislaturaCreateForm, + ParlamentarCreateForm, ParlamentarForm) from .models import (CargoMesa, Coligacao, ComposicaoColigacao, ComposicaoMesa, Dependente, Filiacao, Frente, Legislatura, Mandato, NivelInstrucao, Parlamentar, Partido, SessaoLegislativa, @@ -149,7 +149,18 @@ class LegislaturaCrud(CrudAux): model = Legislatura help_path = 'tabelas_auxiliares#legislatura' - class BaseMixin(CrudAux.BaseMixin): + class CreateView(CrudAux.CreateView): + form_class = LegislaturaCreateForm + + def get_initial(self): + try: + ultima_legislatura = Legislatura.objects.latest('numero') + numero = ultima_legislatura.numero + 1 + except Legislatura.DoesNotExist: + numero = 1 + return {'numero': numero} + + class UpdateView(CrudAux.UpdateView): form_class = LegislaturaForm diff --git a/sapl/templates/parlamentares/layouts.yaml b/sapl/templates/parlamentares/layouts.yaml index 9948256bd..8b7306922 100644 --- a/sapl/templates/parlamentares/layouts.yaml +++ b/sapl/templates/parlamentares/layouts.yaml @@ -5,7 +5,8 @@ CargoMesa: Legislatura: {% trans 'Legislatura' %}: - - numero:2 data_inicio data_fim data_eleicao + - numero:12 + - data_eleicao data_inicio data_fim Coligacao: {% trans 'Coligação' %}: From 6b8c77d46de6e6751636afa8c244a780b6ca202a Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Thu, 13 Oct 2016 12:53:24 -0300 Subject: [PATCH 4/6] =?UTF-8?q?Numera=C3=A7=C3=A3o=20da=20Legislatura=20de?= =?UTF-8?q?=20acordo=20com=20a=20data=20de=20in=C3=ADcio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- sapl/parlamentares/forms.py | 16 ++++++++++++---- sapl/parlamentares/views.py | 10 ++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/sapl/parlamentares/forms.py b/sapl/parlamentares/forms.py index 827ed4f6b..806e635eb 100644 --- a/sapl/parlamentares/forms.py +++ b/sapl/parlamentares/forms.py @@ -21,6 +21,7 @@ class LegislaturaForm(ModelForm): class Meta: model = Legislatura exclude = [] + widgets = {'numero': forms.HiddenInput(), } def clean(self): cleaned_data = self.cleaned_data @@ -39,11 +40,18 @@ class LegislaturaCreateForm(LegislaturaForm): def clean(self): cleaned_data = super(LegislaturaCreateForm, self).clean() - numero = cleaned_data['numero'] - if Legislatura.objects.filter(numero=numero).exists(): + inicio = cleaned_data['data_inicio'] + fim = cleaned_data['data_fim'] + eleicao = cleaned_data['data_eleicao'] + if Legislatura.objects.filter( + data_inicio__range=[inicio, fim]).exists() or \ + Legislatura.objects.filter( + data_fim__range=[inicio, fim]).exists(): raise ValidationError( - _('Já cadastrada uma legislatura com este número')) - + _('Já existe uma legislatura neste intervalo de datas')) + if Legislatura.objects.filter(data_eleicao=eleicao): + raise ValidationError( + _('Esta data de eleição já foi cadastrada')) return cleaned_data diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index 2f243395a..3254d8c90 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -3,6 +3,7 @@ from django.contrib import messages from django.core.urlresolvers import reverse, reverse_lazy from django.utils.translation import ugettext_lazy as _ from django.views.generic import FormView +from django.views.generic.edit import ModelFormMixin from sapl.comissoes.models import Participacao from sapl.crud.base import (RP_CHANGE, RP_DETAIL, RP_LIST, Crud, CrudAux, @@ -160,6 +161,15 @@ class LegislaturaCrud(CrudAux): numero = 1 return {'numero': numero} + def form_valid(self, form): + self.object = form.save() + queryset = Legislatura.objects.all().order_by('data_inicio') + for i, obj in enumerate(queryset): + obj.numero = i + 1 + obj.save() + + return super(ModelFormMixin, self).form_valid(form) + class UpdateView(CrudAux.UpdateView): form_class = LegislaturaForm From a216540c84186f64f6327350d215851ba7c5c885 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Thu, 13 Oct 2016 16:14:04 -0300 Subject: [PATCH 5/6] =?UTF-8?q?Arruma=20edi=C3=A7=C3=A3o=20de=20legislatur?= =?UTF-8?q?a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- sapl/parlamentares/forms.py | 63 ++++++++++++++++------- sapl/parlamentares/views.py | 17 ++---- sapl/templates/parlamentares/layouts.yaml | 3 +- 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/sapl/parlamentares/forms.py b/sapl/parlamentares/forms.py index 806e635eb..975ed7621 100644 --- a/sapl/parlamentares/forms.py +++ b/sapl/parlamentares/forms.py @@ -16,42 +16,65 @@ class ImageThumbnailFileInput(ClearableFileInput): template_name = 'floppyforms/image_thumbnail.html' +def validar_datas_legislatura(eleicao, inicio, fim, pk=None): + + # Verifica se data de eleição < inicio < fim + if inicio >= fim or eleicao >= inicio: + msg_error = _('A data início deve ser menor que a ' + + 'data fim, e a data eleição deve ser ' + + 'menor que a data início') + return [False, msg_error] + + # Verifica se há alguma data cadastrada no intervalo de tempo desejado + if Legislatura.objects.filter( + data_inicio__range=[inicio, fim]).exclude(pk=pk).exists()\ + or Legislatura.objects.filter( + data_fim__range=[inicio, fim]).exclude(pk=pk).exists(): + msg_error = _('Já existe uma legislatura neste intervalo de datas') + return [False, msg_error] + + # Verifica se há alguma outra data de eleição cadastrada + if Legislatura.objects.filter( + data_eleicao=eleicao).exclude(pk=pk).exists(): + msg_error = _('Esta data de eleição já foi cadastrada') + return [False, msg_error] + + return [True, ''] + + class LegislaturaForm(ModelForm): class Meta: model = Legislatura exclude = [] - widgets = {'numero': forms.HiddenInput(), } + + +class LegislaturaCreateForm(LegislaturaForm): def clean(self): cleaned_data = self.cleaned_data - data_inicio = cleaned_data['data_inicio'] - data_fim = cleaned_data['data_fim'] - data_eleicao = cleaned_data['data_eleicao'] - - if data_inicio >= data_fim or data_eleicao >= data_inicio: - raise ValidationError(_('A data início deve ser menor que a ' + - 'data fim, e a data eleição deve ser ' + - 'menor que a data início')) + eleicao = cleaned_data['data_eleicao'] + inicio = cleaned_data['data_inicio'] + fim = cleaned_data['data_fim'] + + valida_datas = validar_datas_legislatura(eleicao, inicio, fim) + if not valida_datas[0]: + raise ValidationError(valida_datas[1]) return cleaned_data -class LegislaturaCreateForm(LegislaturaForm): +class LegislaturaUpdateForm(LegislaturaCreateForm): def clean(self): cleaned_data = super(LegislaturaCreateForm, self).clean() + eleicao = cleaned_data['data_eleicao'] inicio = cleaned_data['data_inicio'] fim = cleaned_data['data_fim'] - eleicao = cleaned_data['data_eleicao'] - if Legislatura.objects.filter( - data_inicio__range=[inicio, fim]).exists() or \ - Legislatura.objects.filter( - data_fim__range=[inicio, fim]).exists(): - raise ValidationError( - _('Já existe uma legislatura neste intervalo de datas')) - if Legislatura.objects.filter(data_eleicao=eleicao): - raise ValidationError( - _('Esta data de eleição já foi cadastrada')) + + valida_datas = validar_datas_legislatura( + eleicao, inicio, fim, pk=self.instance.pk) + if not valida_datas[0]: + raise ValidationError(valida_datas[1]) return cleaned_data diff --git a/sapl/parlamentares/views.py b/sapl/parlamentares/views.py index 3254d8c90..6ee5a1e81 100644 --- a/sapl/parlamentares/views.py +++ b/sapl/parlamentares/views.py @@ -3,7 +3,6 @@ from django.contrib import messages from django.core.urlresolvers import reverse, reverse_lazy from django.utils.translation import ugettext_lazy as _ from django.views.generic import FormView -from django.views.generic.edit import ModelFormMixin from sapl.comissoes.models import Participacao from sapl.crud.base import (RP_CHANGE, RP_DETAIL, RP_LIST, Crud, CrudAux, @@ -12,8 +11,9 @@ from sapl.crud.base import (RP_CHANGE, RP_DETAIL, RP_LIST, Crud, CrudAux, from sapl.materia.models import Proposicao, Relatoria from sapl.parlamentares.apps import AppConfig -from .forms import (FiliacaoForm, LegislaturaForm, LegislaturaCreateForm, - ParlamentarCreateForm, ParlamentarForm) +from .forms import (FiliacaoForm, LegislaturaCreateForm, + LegislaturaUpdateForm, ParlamentarCreateForm, + ParlamentarForm) from .models import (CargoMesa, Coligacao, ComposicaoColigacao, ComposicaoMesa, Dependente, Filiacao, Frente, Legislatura, Mandato, NivelInstrucao, Parlamentar, Partido, SessaoLegislativa, @@ -161,17 +161,8 @@ class LegislaturaCrud(CrudAux): numero = 1 return {'numero': numero} - def form_valid(self, form): - self.object = form.save() - queryset = Legislatura.objects.all().order_by('data_inicio') - for i, obj in enumerate(queryset): - obj.numero = i + 1 - obj.save() - - return super(ModelFormMixin, self).form_valid(form) - class UpdateView(CrudAux.UpdateView): - form_class = LegislaturaForm + form_class = LegislaturaUpdateForm class FiliacaoCrud(MasterDetailCrud): diff --git a/sapl/templates/parlamentares/layouts.yaml b/sapl/templates/parlamentares/layouts.yaml index 8b7306922..98119cf06 100644 --- a/sapl/templates/parlamentares/layouts.yaml +++ b/sapl/templates/parlamentares/layouts.yaml @@ -5,8 +5,7 @@ CargoMesa: Legislatura: {% trans 'Legislatura' %}: - - numero:12 - - data_eleicao data_inicio data_fim + - numero data_eleicao data_inicio data_fim Coligacao: {% trans 'Coligação' %}: From c0a53d5ee49f94d253303e3e3314b1fe37866e43 Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Thu, 13 Oct 2016 16:15:56 -0300 Subject: [PATCH 6/6] Fix #733 --- sapl/comissoes/views.py | 1 + sapl/crud/base.py | 18 ++++++++++++------ sapl/materia/views.py | 26 +++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/sapl/comissoes/views.py b/sapl/comissoes/views.py index 8d3d80e88..fc679d611 100644 --- a/sapl/comissoes/views.py +++ b/sapl/comissoes/views.py @@ -30,6 +30,7 @@ class ParticipacaoCrud(MasterDetailCrud): public = [RP_DETAIL, ] ListView = None is_m2m = True + link_return_to_parent_field = True class BaseMixin(MasterDetailCrud.BaseMixin): list_field_names = ['composicao', 'parlamentar', 'cargo'] diff --git a/sapl/crud/base.py b/sapl/crud/base.py index a870e697f..19781c0c6 100644 --- a/sapl/crud/base.py +++ b/sapl/crud/base.py @@ -919,6 +919,7 @@ class CrudAux(Crud): class MasterDetailCrud(Crud): is_m2m = False + link_return_to_parent_field = False class BaseMixin(Crud.BaseMixin): @@ -1145,11 +1146,14 @@ class MasterDetailCrud(Crud): pk = root_pk - namespace = parent_object._meta.app_config.name - return reverse('%s:%s' % ( - namespace, - '%s_%s' % (parent_object._meta.model_name, ACTION_DETAIL)), - args=(pk,)) + if obj.is_m2m: + namespace = parent_object._meta.app_config.name + return reverse('%s:%s' % ( + namespace, + '%s_%s' % (parent_object._meta.model_name, ACTION_DETAIL)), + args=(pk,)) + else: + return self.resolve_url(ACTION_LIST, args=(pk,)) class DetailView(Crud.DetailView): permission_required = RP_DETAIL, @@ -1166,7 +1170,7 @@ class MasterDetailCrud(Crud): if not obj.ListView: return '' - if not obj.ListView.permission_required or\ + if obj.ListView.permission_required not in obj.public or\ self.request.user.has_perm(self.permission(RP_LIST)): if '__' in obj.parent_field: fields = obj.parent_field.split('__') @@ -1262,6 +1266,8 @@ class MasterDetailCrud(Crud): @property def detail_root_detail_url(self): obj = self.crud if hasattr(self, 'crud') else self + if not obj.link_return_to_parent_field: + return '' if hasattr(obj, 'parent_field'): parent_field = obj.parent_field.split('__') if not obj.is_m2m or len(parent_field) > 1: diff --git a/sapl/materia/views.py b/sapl/materia/views.py index 782f74201..432801f48 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -757,7 +757,28 @@ class LegislacaoCitadaCrud(MasterDetailCrud): args=args) def has_permission(self): - return self.request.user.has_module_perms('materia') + perms = self.get_permission_required() + # Torna a view pública se não possuir conteudo + # no atributo permission_required + return self.request.user.has_module_perms('materia')\ + if len(perms) else True + + def permission(self, rad): + return '%s%s%s' % ('norma' if rad.endswith('_') else '', + rad, + self.model_name if rad.endswith('_') else '') + + @property + def detail_create_url(self): + obj = self.crud if hasattr(self, 'crud') else self + if self.request.user.has_module_perms('materia'): + parent_field = obj.parent_field.split('__')[0] + parent_object = getattr(self.object, parent_field) + + root_pk = parent_object.pk + + return self.resolve_url(ACTION_CREATE, args=(root_pk,)) + return '' @property def list_url(self): @@ -802,6 +823,9 @@ class LegislacaoCitadaCrud(MasterDetailCrud): def layout_key(self): return 'LegislacaoCitadaDetail' + class DeleteView(MasterDetailCrud.DeleteView): + pass + class NumeracaoCrud(MasterDetailCrud): model = Numeracao