diff --git a/sapl/parlamentares/models.py b/sapl/parlamentares/models.py index 1a4e29ac6..8cd2b6d4b 100644 --- a/sapl/parlamentares/models.py +++ b/sapl/parlamentares/models.py @@ -9,7 +9,7 @@ from sapl.base.models import Autor from sapl.utils import (INDICADOR_AFASTAMENTO, UF, YES_NO_CHOICES, SaplGenericRelation, intervalos_tem_intersecao, restringe_tipos_de_arquivo_img, - get_settings_auth_user_model) + get_settings_auth_user_model, texto_upload_path) class Legislatura(models.Model): @@ -189,12 +189,8 @@ class SituacaoMilitar(models.Model): return self.descricao -def get_foto_media_path(instance, subpath, filename): - return './sapl/parlamentar/%s/%s/%s' % (instance, subpath, filename) - - def foto_upload_path(instance, filename): - return get_foto_media_path(instance, 'foto', filename) + return texto_upload_path(instance, filename, subpath='') class Parlamentar(models.Model): @@ -272,6 +268,7 @@ class Parlamentar(models.Model): biografia = models.TextField( blank=True, verbose_name=_('Biografia')) # XXX Esse atribuito foi colocado aqui para não atrapalhar a migração + fotografia = models.ImageField( blank=True, null=True, @@ -312,6 +309,30 @@ class Parlamentar(models.Model): return ''if self.fotografia else '' + def delete(self, using=None, keep_parents=False): + if self.fotografia: + self.fotografia.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.fotografia: + fotografia = self.fotografia + self.fotografia = None + models.Model.save(self, force_insert=force_insert, + force_update=force_update, + using=using, + update_fields=update_fields) + self.fotografia = fotografia + + return models.Model.save(self, force_insert=force_insert, + force_update=force_update, + using=using, + update_fields=update_fields) + class TipoDependente(models.Model): descricao = models.CharField(max_length=50, verbose_name=_('Descrição')) diff --git a/sapl/utils.py b/sapl/utils.py index bda144e5d..11ef6ed84 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 @@ -16,12 +14,16 @@ from django.contrib import admin from django.contrib.contenttypes.fields import (GenericForeignKey, GenericRel, GenericRelation) from django.core.exceptions import ValidationError +from django.utils.text import slugify 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) @@ -598,8 +600,15 @@ def texto_upload_path(instance, filename, subpath=''): if subpath and '/' not in subpath: subpath = subpath + '/' + """ TODO: Verifique possibilidade de otimização do código de normalização + do filename... + Não use slugify... arquivos, + geralmente, possuem [.][alguma extensão] + Slugify retira esse ponto... + """ filename = re.sub('[^a-zA-Z0-9.]', '-', filename).strip('-').lower() filename = re.sub('[-]+', '-', filename) + path = './sapl/%(model_name)s/%(pk)s/%(subpath)s%(filename)s' % { 'model_name': instance._meta.model_name, 'pk': instance.pk,