From 87989e524f11f8605d3177f39b1628d34d17cb08 Mon Sep 17 00:00:00 2001 From: "tapumar@gmail.com" Date: Thu, 9 Aug 2018 16:05:37 -0100 Subject: [PATCH] Fix #697 --- sapl/norma/forms.py | 41 ++++++++++++-------- sapl/norma/urls.py | 1 - sapl/norma/views.py | 16 +++++--- sapl/templates/norma/layouts.yaml | 6 +-- sapl/templates/norma/normajuridica_form.html | 5 --- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/sapl/norma/forms.py b/sapl/norma/forms.py index dfe48f6a0..678f468b7 100644 --- a/sapl/norma/forms.py +++ b/sapl/norma/forms.py @@ -97,10 +97,7 @@ class NormaJuridicaForm(ModelForm): choices=ANO_CHOICES, widget=forms.Select(attrs={'autocomplete': 'off'}) ) - anexo_arquivo = forms.FileField( - label='Anexo Norma Jurídica', - required=False - ) + class Meta: model = NormaJuridica fields = ['tipo', @@ -120,7 +117,6 @@ class NormaJuridicaForm(ModelForm): 'indexacao', 'observacao', 'texto_integral', - 'anexo_arquivo', 'assuntos'] widgets = {'assuntos': widgets.CheckboxSelectMultiple} @@ -176,27 +172,38 @@ class NormaJuridicaForm(ModelForm): return texto_integral def save(self, commit=False): - norma = self.instance norma.timestamp = timezone.now() norma.materia = self.cleaned_data['materia'] norma = super(NormaJuridicaForm, self).save(commit=True) - if self.cleaned_data['anexo_arquivo']: - anexo = AnexoNormaJuridica() - anexo.anexo_arquivo = self.cleaned_data['anexo_arquivo'] - anexo.norma = norma - anexo.ano = timezone.now().year - anexo.save() return norma class AnexoNormaJuridicaForm(ModelForm): - # class Meta: - # model = AnexoNormaJuridica - # fields = ['norma', 'anexo_arquivo'] + class Meta: + model = AnexoNormaJuridica + fields = ['norma', 'anexo_arquivo'] - def __init__(self, *args, **kwargs): - super(AnexoNormaJuridicaForm, self).__init__(*args, **kwargs) + def clean(self): + cleaned_data = super(AnexoNormaJuridicaForm, self).clean() + if not self.is_valid(): + return cleaned_data + anexo_arquivo = self.cleaned_data.get('anexo_arquivo', False) + if anexo_arquivo: + if anexo_arquivo.size > MAX_DOC_UPLOAD_SIZE: + max_size = str(MAX_DOC_UPLOAD_SIZE / (1024 * 1024)) + raise ValidationError( + "Arquivo muito grande. ( > {0}MB )".format(max_size)) + return cleaned_data + + def save(self, commit=False): + anexo = self.instance + anexo.ano = self.cleaned_data['norma'].ano + anexo = super(AnexoNormaJuridicaForm, self).save(commit=True) + anexo.norma = self.cleaned_data['norma'] + anexo.anexo_arquivo = self.cleaned_data['anexo_arquivo'] + anexo.save() + return anexo diff --git a/sapl/norma/urls.py b/sapl/norma/urls.py index 21f292476..d943f71e8 100644 --- a/sapl/norma/urls.py +++ b/sapl/norma/urls.py @@ -17,7 +17,6 @@ urlpatterns = [ # Integração com Compilação url(r'^norma/(?P[0-9]+)/ta$', NormaTaView.as_view(), name='norma_ta'), - url(r'^sistema/norma/tipo/', include(TipoNormaCrud.get_urls())), url(r'^sistema/norma/assunto/', include(AssuntoNormaCrud.get_urls())), url(r'^sistema/norma/vinculo/', include( diff --git a/sapl/norma/views.py b/sapl/norma/views.py index 7b1e2cbdc..de3efa8ef 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -101,18 +101,24 @@ class NormaPesquisaView(FilterView): class AnexoNormaJuridicaCrud(MasterDetailCrud): model = AnexoNormaJuridica parent_field = 'norma' - help_topic = 'anexo_normajuridica' + help_topic = 'anexonormajuridica' public = [RP_LIST, RP_DETAIL] class BaseMixin(MasterDetailCrud.BaseMixin): - list_field_names = ['anexo_arquivo'] + list_field_names = ['id','anexo_arquivo'] class CreateView(MasterDetailCrud.CreateView): form_class = AnexoNormaJuridicaForm + layout_key = 'AnexoNormaJuridica' + def get_initial(self): + norma_id = str(self.request).split("/")[-3] + self.initial = super(MasterDetailCrud.CreateView, self).get_initial() + self.initial['norma'] = NormaJuridica.objects.get(id=norma_id) + return self.initial class UpdateView(MasterDetailCrud.UpdateView): form_class = AnexoNormaJuridicaForm - + layout_key = 'AnexoNormaJuridica' def get_initial(self): initial = super(UpdateView, self).get_initial() initial['norma'] = self.object.norma @@ -121,8 +127,8 @@ class AnexoNormaJuridicaCrud(MasterDetailCrud): return initial class DetailView(MasterDetailCrud.DetailView): - - layout_key = 'AnexoNormaJuridicaDetail' + form_class = AnexoNormaJuridicaForm + layout_key = 'AnexoNormaJuridica' class NormaTaView(IntegracaoTaView): diff --git a/sapl/templates/norma/layouts.yaml b/sapl/templates/norma/layouts.yaml index 7cd9eb0e7..591faed38 100644 --- a/sapl/templates/norma/layouts.yaml +++ b/sapl/templates/norma/layouts.yaml @@ -25,8 +25,8 @@ NormaJuridica: - assuntos AnexoNormaJuridica: - {% trans 'Adicionar Anexos à Norma Jurídica' %} - - anexo + {% trans 'Adicionar Anexos à Norma Jurídica' %}: + - anexo_arquivo - norma NormaJuridicaCreate: @@ -40,8 +40,6 @@ NormaJuridicaCreate: - indexacao - observacao - assuntos - {% trans 'Anexos da Norma Jurídica' %}: - - anexo_arquivo LegislacaoCitada: {% trans 'Legislação Citada' %}: diff --git a/sapl/templates/norma/normajuridica_form.html b/sapl/templates/norma/normajuridica_form.html index b7244e0b9..58a48046a 100644 --- a/sapl/templates/norma/normajuridica_form.html +++ b/sapl/templates/norma/normajuridica_form.html @@ -5,11 +5,6 @@ {% block extra_js %} -{% block content %} -
-Testando essa bagaça
-{% endblock %} -