|
@ -7,6 +7,9 @@ from django.utils.html import strip_tags |
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
from django.views.generic.edit import FormMixin |
|
|
from django.views.generic.edit import FormMixin |
|
|
from vanilla import GenericView |
|
|
from vanilla import GenericView |
|
|
|
|
|
from django.forms.fields import ImageField |
|
|
|
|
|
from io import BytesIO |
|
|
|
|
|
from django.core.exceptions import ValidationError |
|
|
|
|
|
|
|
|
from crud import build_crud |
|
|
from crud import build_crud |
|
|
|
|
|
|
|
@ -256,6 +259,40 @@ class ParlamentaresCadastroView(FormMixin, GenericView): |
|
|
{'form': form, 'legislatura_id': pk}) |
|
|
{'form': form, 'legislatura_id': pk}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DmitryImageField(ImageField): |
|
|
|
|
|
|
|
|
|
|
|
def to_python(self, data): |
|
|
|
|
|
f = super(DmitryImageField, self).to_python(data) |
|
|
|
|
|
if f is None: |
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
from PIL import Image |
|
|
|
|
|
except ImportError: |
|
|
|
|
|
import Image |
|
|
|
|
|
|
|
|
|
|
|
if hasattr(data, 'temporary_file_path'): |
|
|
|
|
|
file = data.temporary_file_path() |
|
|
|
|
|
else: |
|
|
|
|
|
if hasattr(data, 'read'): |
|
|
|
|
|
file = BytesIO(data.read()) |
|
|
|
|
|
else: |
|
|
|
|
|
file = BytesIO(data['content']) |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
im = Image.open(file) |
|
|
|
|
|
if im.format not in ('BMP', 'PNG', 'JPEG'): |
|
|
|
|
|
return -1 |
|
|
|
|
|
except ImportError: |
|
|
|
|
|
return -2 |
|
|
|
|
|
except Exception: |
|
|
|
|
|
return -3 |
|
|
|
|
|
|
|
|
|
|
|
if hasattr(f, 'seek') and callable(f.seek): |
|
|
|
|
|
f.seek(0) |
|
|
|
|
|
return f |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParlamentaresEditarView(FormMixin, GenericView): |
|
|
class ParlamentaresEditarView(FormMixin, GenericView): |
|
|
template_name = "parlamentares/parlamentares_cadastro.html" |
|
|
template_name = "parlamentares/parlamentares_cadastro.html" |
|
|
|
|
|
|
|
@ -279,6 +316,17 @@ class ParlamentaresEditarView(FormMixin, GenericView): |
|
|
parlamentar = form.save(commit=False) |
|
|
parlamentar = form.save(commit=False) |
|
|
if 'fotografia' in request.FILES: |
|
|
if 'fotografia' in request.FILES: |
|
|
parlamentar.fotografia = request.FILES['fotografia'] |
|
|
parlamentar.fotografia = request.FILES['fotografia'] |
|
|
|
|
|
valida_imagem = DmitryImageField() |
|
|
|
|
|
# import ipdb; ipdb.set_trace() |
|
|
|
|
|
try: |
|
|
|
|
|
valida_imagem.to_python(request.FILES['fotografia']) |
|
|
|
|
|
except ValidationError: |
|
|
|
|
|
mensagem = "Por favor, insira uma imagem válida dos formatos\ |
|
|
|
|
|
JPEG, PNG ou BMP" |
|
|
|
|
|
messages.add_message(request, messages.INFO, mensagem) |
|
|
|
|
|
return self.render_to_response({'form': form}) |
|
|
|
|
|
else: |
|
|
|
|
|
pass |
|
|
parlamentar.biografia = sub(' ', |
|
|
parlamentar.biografia = sub(' ', |
|
|
' ', |
|
|
' ', |
|
|
strip_tags(form.data['biografia'])) |
|
|
strip_tags(form.data['biografia'])) |
|
@ -294,7 +342,6 @@ class ParlamentaresEditarView(FormMixin, GenericView): |
|
|
parlamentar = form.save(commit=False) |
|
|
parlamentar = form.save(commit=False) |
|
|
parlamentar.fotografia = None |
|
|
parlamentar.fotografia = None |
|
|
parlamentar.save() |
|
|
parlamentar.save() |
|
|
|
|
|
|
|
|
return self.form_valid(form) |
|
|
return self.form_valid(form) |
|
|
else: |
|
|
else: |
|
|
return self.render_to_response({'form': form}) |
|
|
return self.render_to_response({'form': form}) |
|
|