From 490fffbf4e0e50be52d47056821e7f626ce76dcd Mon Sep 17 00:00:00 2001 From: Eduardo Calil Date: Tue, 15 Mar 2016 13:34:08 -0300 Subject: [PATCH] =?UTF-8?q?Refatora=20edi=C3=A7=C3=A3o=20de=20parlamentar?= =?UTF-8?q?=20e=20verifica=C3=A7=C3=A3o=20de=20imagem=20v=C3=A1lida.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/forms.py | 2 +- materia/models.py | 6 ++-- parlamentares/forms.py | 9 +++-- parlamentares/models.py | 5 +-- parlamentares/views.py | 64 +++++++++++++---------------------- requirements/requirements.txt | 1 + sapl/utils.py | 57 +++++++++++++++++++++++++++++++ 7 files changed, 96 insertions(+), 48 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..5d57d8f22 100644 --- a/parlamentares/forms.py +++ b/parlamentares/forms.py @@ -131,15 +131,18 @@ 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 %}

+ {% 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..b22f95a31 100644 --- a/parlamentares/views.py +++ b/parlamentares/views.py @@ -1,9 +1,11 @@ import os +from braces.views import FormMessagesMixin from django.contrib import messages -from django.core.urlresolvers import reverse +from django.core.urlresolvers import reverse, reverse_lazy +from django.http import HttpResponseRedirect from django.utils.translation import ugettext_lazy as _ -from django.views.generic import FormView +from django.views.generic import CreateView, FormView, UpdateView from crud import Crud @@ -139,45 +141,27 @@ class ParlamentaresCadastroView(FormView): {'form': form, 'legislatura_id': pk}) -class ParlamentaresEditarView(FormView): +class ParlamentaresEditarView(UpdateView): template_name = "parlamentares/parlamentares_cadastro.html" - - def get_success_url(self): - return reverse('parlamentares') - - def get(self, request, *args, **kwargs): - pk = kwargs['pk'] - parlamentar = Parlamentar.objects.get(pk=pk) - form = ParlamentaresEditForm(instance=parlamentar) - return self.render_to_response( - {'form': form, 'object': parlamentar}) - - def post(self, request, *args, **kwargs): - pk = kwargs['pk'] - parlamentar = Parlamentar.objects.get(pk=pk) - form = ParlamentaresEditForm(request.POST, 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() - elif 'excluir' in request.POST: - Mandato.objects.get(parlamentar=parlamentar).delete() - parlamentar.delete() - elif "remover" in request.POST: - try: - 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) - else: - return self.render_to_response({'form': form}) + form_class = ParlamentaresEditForm + model = Parlamentar + success_url = reverse_lazy('parlamentares') + + def form_valid(self, form): + parlamentar = form.instance + if 'salvar' in self.request.POST: + form.save() + elif 'excluir' in self.request.POST: + Mandato.objects.get(parlamentar=parlamentar).delete() + parlamentar.delete() + elif "remover-foto" in self.request.POST: + try: + os.unlink(parlamentar.fotografia.path) + except OSError: + pass # Should log this error!!!!! + parlamentar.fotografia = None + parlamentar.save() + return HttpResponseRedirect(self.get_success_url()) class ParlamentaresDependentesView(FormView): diff --git a/requirements/requirements.txt b/requirements/requirements.txt index fa666f49b..d799f0b35 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -17,3 +17,4 @@ pytz==2015.7 pyyaml==3.11 rtyaml==0.0.2 unipath==1.1 +python-magic==0.4.10 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)