Browse Source

WIP

pull/268/head
Eduardo Calil 9 years ago
parent
commit
d2ccad6bd7
  1. 2
      base/forms.py
  2. 6
      materia/models.py
  3. 5
      parlamentares/forms.py
  4. 5
      parlamentares/models.py
  5. 17
      parlamentares/views.py
  6. 57
      sapl/utils.py

2
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 %}
<img class="img-responsive" width="225" height="300"
src="{{ MEDIA_URL }}{{ form.logotipo.value }}">
<br /><br />

6
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')

5
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 %}
<img class="img-responsive" width="225" height="300"
src="{{ MEDIA_URL }}{{ form.fotografia.value }}">
<br /><br />
@ -140,6 +142,7 @@ class ParlamentaresForm (ModelForm):
id="remover"
class="btn btn-warning"
value="Remover Foto"/>
{% endif %}
{% endif %}""", ),
row14,
form_actions())

5
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')

17
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)

57
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 = '''
<div class="col-xs-12">
@ -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)

Loading…
Cancel
Save