diff --git a/sapl/materia/forms.py b/sapl/materia/forms.py index f5ca6d8bd..7b3797b81 100644 --- a/sapl/materia/forms.py +++ b/sapl/materia/forms.py @@ -986,29 +986,35 @@ class ProposicaoForm(forms.ModelForm): return cd def save(self, commit=True): + cd = self.cleaned_data + inst = self.instance + if inst.pk: + if 'tipo_texto' in cd: - if self.instance.pk: - if 'tipo_texto' in self.cleaned_data: - if self.cleaned_data['tipo_texto'] in ['T', ''] and\ - self.instance.texto_original: - self.instance.texto_original.delete() + if cd['tipo_texto'] == 'T' and inst.texto_original: + inst.texto_original.delete() + + elif cd['tipo_texto'] != 'T': + inst.texto_articulado.all().delete() - if self.cleaned_data['tipo_texto'] in ['D', '']: - self.instance.texto_articulado.all().delete() + if 'texto_original' in cd and\ + not cd['texto_original'] and \ + inst.texto_original: + inst.texto_original.delete() return super().save(commit) - self.instance.ano = datetime.now().year + inst.ano = datetime.now().year numero__max = Proposicao.objects.filter( - autor=self.instance.autor, + autor=inst.autor, ano=datetime.now().year).aggregate(Max('numero_proposicao')) numero__max = numero__max['numero_proposicao__max'] - self.instance.numero_proposicao = ( + inst.numero_proposicao = ( numero__max + 1) if numero__max else 1 - self.instance.save() + inst.save() - return self.instance + return inst class ConfirmarProposicaoForm(ProposicaoForm): @@ -1209,6 +1215,7 @@ class ConfirmarProposicaoForm(ProposicaoForm): self.instance.justificativa_devolucao = '' self.instance.data_devolucao = None self.instance.data_recebimento = datetime.now() + self.instance.materia_de_vinculo = cd['materia_de_vinculo'] if self.instance.texto_articulado.exists(): ta = self.instance.texto_articulado.first() diff --git a/sapl/materia/models.py b/sapl/materia/models.py index d84e0f2bf..26937e6a4 100644 --- a/sapl/materia/models.py +++ b/sapl/materia/models.py @@ -200,6 +200,13 @@ class MateriaLegislativa(models.Model): return _('%(tipo)s nº %(numero)s de %(ano)s') % { 'tipo': self.tipo, 'numero': self.numero, 'ano': self.ano} + def delete(self, using=None, keep_parents=False): + if self.texto_original: + self.texto_original.delete() + + return models.Model.delete( + self, using=using, keep_parents=keep_parents) + def save(self, force_insert=False, force_update=False, using=None, update_fields=None): @@ -346,6 +353,13 @@ class DocumentoAcessorio(models.Model): 'data': self.data, 'autor': self.autor} + def delete(self, using=None, keep_parents=False): + if self.arquivo: + self.arquivo.delete() + + return models.Model.delete( + self, using=using, keep_parents=keep_parents) + def save(self, force_insert=False, force_update=False, using=None, update_fields=None): @@ -590,6 +604,13 @@ class Proposicao(models.Model): self.numero_proposicao, self.ano) + def delete(self, using=None, keep_parents=False): + if self.texto_original: + self.texto_original.delete() + + return models.Model.delete( + self, using=using, keep_parents=keep_parents) + def save(self, force_insert=False, force_update=False, using=None, update_fields=None): diff --git a/sapl/norma/models.py b/sapl/norma/models.py index 49e85182c..5b30933e6 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -116,6 +116,13 @@ class NormaJuridica(models.Model): 'numero': self.numero, 'data': defaultfilters.date(self.data, "d \d\e F \d\e Y")} + def delete(self, using=None, keep_parents=False): + if self.texto_integral: + self.texto_integral.delete() + + return models.Model.delete( + self, using=using, keep_parents=keep_parents) + def save(self, force_insert=False, force_update=False, using=None, update_fields=None): diff --git a/sapl/protocoloadm/models.py b/sapl/protocoloadm/models.py index 68401a7d8..d472155ed 100644 --- a/sapl/protocoloadm/models.py +++ b/sapl/protocoloadm/models.py @@ -81,6 +81,30 @@ class DocumentoAdministrativo(models.Model): 'tipo': self.tipo, 'assunto': self.assunto } + def delete(self, using=None, keep_parents=False): + if self.texto_integral: + self.texto_integral.delete() + + return models.Model.delete( + self, using=using, keep_parents=keep_parents) + + def save(self, force_insert=False, force_update=False, using=None, + update_fields=None): + + if not self.pk and self.texto_integral: + texto_integral = self.texto_integral + self.texto_integral = None + models.Model.save(self, force_insert=force_insert, + force_update=force_update, + using=using, + update_fields=update_fields) + self.texto_integral = texto_integral + + return models.Model.save(self, force_insert=force_insert, + force_update=force_update, + using=using, + update_fields=update_fields) + class DocumentoAcessorioAdministrativo(models.Model): documento = models.ForeignKey(DocumentoAdministrativo) @@ -106,6 +130,30 @@ class DocumentoAcessorioAdministrativo(models.Model): def __str__(self): return self.nome + def delete(self, using=None, keep_parents=False): + if self.arquivo: + self.arquivo.delete() + + return models.Model.delete( + self, using=using, keep_parents=keep_parents) + + def save(self, force_insert=False, force_update=False, using=None, + update_fields=None): + + if not self.pk and self.arquivo: + arquivo = self.arquivo + self.arquivo = None + models.Model.save(self, force_insert=force_insert, + force_update=force_update, + using=using, + update_fields=update_fields) + self.arquivo = arquivo + + return models.Model.save(self, force_insert=force_insert, + force_update=force_update, + using=using, + update_fields=update_fields) + class Protocolo(models.Model): numero = models.PositiveIntegerField( diff --git a/sapl/sessao/models.py b/sapl/sessao/models.py index cf74399b5..f29194710 100644 --- a/sapl/sessao/models.py +++ b/sapl/sessao/models.py @@ -7,7 +7,7 @@ from sapl.materia.models import MateriaLegislativa from sapl.parlamentares.models import (CargoMesa, Legislatura, Parlamentar, Partido, SessaoLegislativa) from sapl.utils import (YES_NO_CHOICES, SaplGenericRelation, - restringe_tipos_de_arquivo_txt) + restringe_tipos_de_arquivo_txt, texto_upload_path) class CargoBancada(models.Model): @@ -77,11 +77,13 @@ def get_sessao_media_path(instance, subpath, filename): def pauta_upload_path(instance, filename): - return get_sessao_media_path(instance, 'pauta', filename) + return texto_upload_path(instance, filename, subpath='pauta') + # return get_sessao_media_path(instance, 'pauta', filename) def ata_upload_path(instance, filename): - return get_sessao_media_path(instance, 'ata', filename) + return texto_upload_path(instance, filename, subpath='ata') + # return get_sessao_media_path(instance, 'ata', filename) class SessaoPlenaria(models.Model): @@ -144,6 +146,37 @@ class SessaoPlenaria(models.Model): # XXX check if it shouldn't be legislatura.numero 'legislatura_id': self.legislatura.numero} + def delete(self, using=None, keep_parents=False): + if self.upload_pauta: + self.upload_pauta.delete() + + if self.upload_ata: + self.upload_ata.delete() + + return models.Model.delete( + self, using=using, keep_parents=keep_parents) + + def save(self, force_insert=False, force_update=False, using=None, + update_fields=None): + + if not self.pk and (self.upload_pauta or self.upload_ata): + upload_pauta = self.upload_pauta + upload_ata = self.upload_ata + self.upload_pauta = None + self.upload_ata = None + models.Model.save(self, force_insert=force_insert, + force_update=force_update, + using=using, + update_fields=update_fields) + + self.upload_pauta = upload_pauta + self.upload_ata = upload_ata + + return models.Model.save(self, force_insert=force_insert, + force_update=force_update, + using=using, + update_fields=update_fields) + class AbstractOrdemDia(models.Model): TIPO_VOTACAO_CHOICES = Choices( diff --git a/sapl/templates/materia/proposicao_form.html b/sapl/templates/materia/proposicao_form.html index 3ebd898ba..f9f9414b6 100644 --- a/sapl/templates/materia/proposicao_form.html +++ b/sapl/templates/materia/proposicao_form.html @@ -17,15 +17,19 @@ if (this.selectedOptions[0].getAttribute('data-has-perfil') === "True") { $("input[name=tipo_texto]").closest('label').removeClass('disabled'); + $("input[name=tipo_texto]").closest('.form-group').parent().removeClass('hidden'); $("input[name=tipo_texto]").prop('disabled', false); } else { $("input[name=tipo_texto]").closest('label').addClass('disabled'); + $("input[name=tipo_texto]").closest('.form-group').parent().addClass('hidden'); $("input[name=tipo_texto]").prop('disabled', true); } + if ($("input[name=tipo_texto]:checked").length == 0) { + $("input[name=tipo_texto]").first().prop('checked', true); + $("input[name=tipo_texto]").first().closest('label').addClass('checked'); + } - $("input[name=tipo_texto]").first().prop('checked', true); - $("input[name=tipo_texto]").first().closest('label').addClass('checked'); }); $("select[name=tipo_materia], input[name=numero_materia], input[name=ano_materia]").change(function(event) { @@ -46,7 +50,7 @@ }); }); - $("input[name=tipo_texto], select[name=tipo_materia]").trigger('change'); + $("input[name=tipo_texto], select[name=tipo_materia], select[name=tipo]").trigger('change'); }); diff --git a/sapl/utils.py b/sapl/utils.py index e61d68f85..89f3829c8 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -1,12 +1,10 @@ -import hashlib -import logging -import re from datetime import date from functools import wraps from unicodedata import normalize as unicodedata_normalize +import hashlib +import logging +import re -import django_filters -import magic from crispy_forms.helper import FormHelper from crispy_forms.layout import HTML, Button from django import forms @@ -18,10 +16,13 @@ from django.contrib.contenttypes.fields import (GenericForeignKey, GenericRel, from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ from floppyforms import ClearableFileInput +import django_filters +import magic from sapl.crispy_layout_mixin import SaplFormLayout, form_actions, to_row from sapl.settings import BASE_DIR + sapl_logger = logging.getLogger(BASE_DIR.name) @@ -573,7 +574,7 @@ def generic_relations_for_model(model): )) -def texto_upload_path(instance, filename): +def texto_upload_path(instance, filename, subpath=''): """ O path gerado por essa função leva em conta a pk de instance. isso não é possível naturalmente em uma inclusão pois a implementação @@ -595,11 +596,15 @@ def texto_upload_path(instance, filename): seguida para armazenar o arquivo. """ - filename = re.sub('[^a-zA-Z0-9]', '-', filename).strip('-').lower() + if subpath and '/' not in subpath: + subpath = subpath + '/' + + filename = re.sub('[^a-zA-Z0-9.]', '-', filename).strip('-').lower() filename = re.sub('[-]+', '-', filename) - path = './sapl/%(model_name)s/%(pk)s/%(filename)s' % { + path = './sapl/%(model_name)s/%(pk)s/%(subpath)s%(filename)s' % { 'model_name': instance._meta.model_name, 'pk': instance.pk, + 'subpath': subpath, 'filename': filename} return path