From d2ccad6bd79eb414ab69a6d6b03e6ce216aa1677 Mon Sep 17 00:00:00 2001 From: Eduardo Calil Date: Tue, 15 Mar 2016 13:34:08 -0300 Subject: [PATCH] WIP --- base/forms.py | 2 +- materia/models.py | 6 +++-- parlamentares/forms.py | 5 +++- parlamentares/models.py | 5 ++-- parlamentares/views.py | 17 ++++++------ sapl/utils.py | 57 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 15 deletions(-) diff --git a/base/forms.py b/base/forms.py index 50161fb8c..f37f886ac 100644 --- a/base/forms.py +++ b/base/forms.py @@ -78,7 +78,7 @@ class CasaLegislativaTabelaAuxForm(ModelForm): row3, row4, row5, - HTML("""{% if form.logotipo.value %} + HTML("""{% if not form.fotografia.errors and form.fotografia.value %}

diff --git a/materia/models.py b/materia/models.py index 1d6f96d5c..a9e864931 100644 --- a/materia/models.py +++ b/materia/models.py @@ -4,7 +4,8 @@ from model_utils import Choices from comissoes.models import Comissao from parlamentares.models import Parlamentar, Partido -from sapl.utils import RANGE_ANOS, YES_NO_CHOICES, xstr +from sapl.utils import (restringe_tipos_de_arquivo_txt, + RANGE_ANOS, YES_NO_CHOICES, xstr) class TipoMateriaLegislativa(models.Model): @@ -457,7 +458,8 @@ class Proposicao(models.Model): blank=True, null=True, upload_to=texto_upload_path, - verbose_name=_('Texto Original (PDF)')) + verbose_name=_('Texto Original (PDF)'), + validators=[restringe_tipos_de_arquivo_txt]) class Meta: verbose_name = _('Proposição') diff --git a/parlamentares/forms.py b/parlamentares/forms.py index 9faa5df27..4cf589023 100644 --- a/parlamentares/forms.py +++ b/parlamentares/forms.py @@ -131,7 +131,9 @@ class ParlamentaresForm (ModelForm): row1, row2, row3, row4, row5, row6, row7, row8, row9, row10, row11, row12, row13, - HTML("""{% if form.fotografia.value %} + HTML(""" + {% if not form.fotografia.errors %} + {% if form.fotografia.value %}

@@ -140,6 +142,7 @@ class ParlamentaresForm (ModelForm): id="remover" class="btn btn-warning" value="Remover Foto"/> + {% endif %} {% endif %}""", ), row14, form_actions()) diff --git a/parlamentares/models.py b/parlamentares/models.py index a1055602d..6842ced10 100644 --- a/parlamentares/models.py +++ b/parlamentares/models.py @@ -4,7 +4,7 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ from model_utils import Choices -from sapl.utils import UF, YES_NO_CHOICES +from sapl.utils import restringe_tipos_de_arquivo_img, UF, YES_NO_CHOICES class Legislatura(models.Model): @@ -241,7 +241,8 @@ class Parlamentar(models.Model): blank=True, null=True, upload_to=foto_upload_path, - verbose_name=_('Fotografia')) + verbose_name=_('Fotografia'), + validators=[restringe_tipos_de_arquivo_img]) class Meta: verbose_name = _('Parlamentar') diff --git a/parlamentares/views.py b/parlamentares/views.py index b6803f182..7ff036d5e 100644 --- a/parlamentares/views.py +++ b/parlamentares/views.py @@ -3,7 +3,7 @@ import os from django.contrib import messages from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ -from django.views.generic import FormView +from django.views.generic import CreateView, FormView from crud import Crud @@ -139,8 +139,9 @@ class ParlamentaresCadastroView(FormView): {'form': form, 'legislatura_id': pk}) -class ParlamentaresEditarView(FormView): +class ParlamentaresEditarView(UpdateView): template_name = "parlamentares/parlamentares_cadastro.html" + form_class = ParlamentaresForm def get_success_url(self): return reverse('parlamentares') @@ -155,15 +156,14 @@ class ParlamentaresEditarView(FormView): def post(self, request, *args, **kwargs): pk = kwargs['pk'] parlamentar = Parlamentar.objects.get(pk=pk) - form = ParlamentaresEditForm(request.POST, instance=parlamentar) + + form = ParlamentaresEditForm(request.POST, + request.FILES, + instance=parlamentar) if form.is_valid(): if 'salvar' in request.POST: - parlamentar = form.save(commit=False) - if 'fotografia' in request.FILES: - parlamentar.fotografia = request.FILES['fotografia'] - parlamentar.biografia = form.data['biografia'] - parlamentar.save() + form.save() elif 'excluir' in request.POST: Mandato.objects.get(parlamentar=parlamentar).delete() parlamentar.delete() @@ -172,7 +172,6 @@ class ParlamentaresEditarView(FormView): os.unlink(parlamentar.fotografia.path) except OSError: pass # Should log this error!!!!! - parlamentar = form.save(commit=False) parlamentar.fotografia = None parlamentar.save() return self.form_valid(form) diff --git a/sapl/utils.py b/sapl/utils.py index f5707d197..83a3fc820 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -1,10 +1,13 @@ from datetime import date + from functools import wraps from django.apps import apps from django.contrib import admin +from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ +import magic autor_label = '''
@@ -119,3 +122,57 @@ UF = [ ] RANGE_ANOS = [(year, year) for year in range(date.today().year, 1889, -1)] + +TIPOS_TEXTO_PERMITIDOS = ( + 'application/vnd.oasis.opendocument.text', + 'application/x-vnd.oasis.opendocument.text', + 'application/pdf', + 'application/x-pdf', + 'application/acrobat', + 'applications/vnd.pdf', + 'text/pdf', + 'text/x-pdf', + 'text/plain', + 'application/txt', + 'browser/internal', + 'text/anytext', + 'widetext/plain', + 'widetext/paragraph', + 'application/msword', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', +) + +TIPOS_IMG_PERMITIDOS = ( + 'image/jpeg', + 'image/jpg', + 'image/jpe_', + 'image/pjpeg', + 'image/vnd.swiftview-jpeg', + 'application/jpg', + 'application/x-jpg', + 'image/pjpeg', + 'image/pipeg', + 'image/vnd.swiftview-jpeg', + 'image/x-xbitmap', + 'image/bmp', + 'image/x-bmp', + 'image/x-bitmap', + 'image/png', + 'application/png', + 'application/x-png', +) + + +def fabrica_validador_de_tipos_de_arquivo(lista): + + def restringe_tipos_de_arquivo(value): + mime = magic.from_buffer(value.read(), mime=True) + mime = mime.decode() + if mime not in lista: + raise ValidationError(_('Tipo de arquivo não suportado')) + return restringe_tipos_de_arquivo + +restringe_tipos_de_arquivo_txt = fabrica_validador_de_tipos_de_arquivo( + TIPOS_TEXTO_PERMITIDOS) +restringe_tipos_de_arquivo_img = fabrica_validador_de_tipos_de_arquivo( + TIPOS_IMG_PERMITIDOS)