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 = '''