Browse Source

Refatora upload de foto de parlamentar

pull/919/head
LeandroRoberto 8 years ago
parent
commit
93c961c4c7
  1. 33
      sapl/parlamentares/models.py
  2. 19
      sapl/utils.py

33
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 '<img class="avatar-parlamentar" src='\
+ self.fotografia.url + '>'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'))

19
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,

Loading…
Cancel
Save