Browse Source

Merge 56-valida-tipo-imagem-texto

fix #56
pull/285/merge
Marcio Mazza 9 years ago
parent
commit
fabcef2288
  1. 2
      base/forms.py
  2. 11
      materia/forms.py
  3. 9
      materia/models.py
  4. 11
      materia/views.py
  5. 13
      parlamentares/forms.py
  6. 5
      parlamentares/models.py
  7. 2
      parlamentares/views.py
  8. 1
      requirements/requirements.txt
  9. 6
      sapl/settings.py
  10. 59
      sapl/utils.py
  11. 15
      sessao/forms.py
  12. 8
      sessao/models.py
  13. 14
      templates/materia/proposicao/proposicao_list.html
  14. 2
      templates/sessao/sessao_list.html

2
base/forms.py

@ -89,7 +89,7 @@ class CasaLegislativaTabelaAuxForm(ModelForm):
row4, row4,
row5, row5,
HTML("""<div class="col-md-12"> HTML("""<div class="col-md-12">
{% if form.logotipo.value %} {% if not form.fotografia.errors and form.fotografia.value %}
<img class="img-responsive" width="225" height="300" <img class="img-responsive" width="225" height="300"
src="{{ MEDIA_URL }}{{ form.logotipo.value }}"> src="{{ MEDIA_URL }}{{ form.logotipo.value }}">
<br /> <br />

11
materia/forms.py

@ -70,6 +70,17 @@ class ProposicaoForm(ModelForm):
self.helper.layout = Layout( self.helper.layout = Layout(
Fieldset(_('Incluir Proposição'), Fieldset(_('Incluir Proposição'),
row1, row2, row3, row4, row1, row2, row3, row4,
HTML("""
<div class="img-responsive" width="225" height="300"
src="{{ MEDIA_URL }}{{ form.texto_original.value }}">
<br /><br />
<input type="submit"
name="remover-texto"
id="remover-texto"
class="btn btn-warning"
value="Remover Texto"/>
<p></p>
""", ),
form_actions(more=more)) form_actions(more=more))
) )
super(ProposicaoForm, self).__init__( super(ProposicaoForm, self).__init__(

9
materia/models.py

@ -4,7 +4,8 @@ from model_utils import Choices
from comissoes.models import Comissao from comissoes.models import Comissao
from parlamentares.models import Parlamentar, Partido from parlamentares.models import Parlamentar, Partido
from sapl.utils import RANGE_ANOS, YES_NO_CHOICES, xstr from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES,
restringe_tipos_de_arquivo_txt, xstr)
class TipoMateriaLegislativa(models.Model): class TipoMateriaLegislativa(models.Model):
@ -122,7 +123,8 @@ class MateriaLegislativa(models.Model):
blank=True, blank=True,
null=True, null=True,
upload_to=texto_upload_path, upload_to=texto_upload_path,
verbose_name=_('Texto Original (PDF)')) verbose_name=_('Texto Original (PDF)'),
validators=[restringe_tipos_de_arquivo_txt])
class Meta: class Meta:
verbose_name = _('Matéria Legislativa') verbose_name = _('Matéria Legislativa')
@ -457,7 +459,8 @@ class Proposicao(models.Model):
blank=True, blank=True,
null=True, null=True,
upload_to=texto_upload_path, upload_to=texto_upload_path,
verbose_name=_('Texto Original (PDF)')) verbose_name=_('Texto Original (PDF)'),
validators=[restringe_tipos_de_arquivo_txt])
class Meta: class Meta:
verbose_name = _('Proposição') verbose_name = _('Proposição')

11
materia/views.py

@ -1,3 +1,4 @@
import os
from datetime import datetime from datetime import datetime
from random import choice from random import choice
from string import ascii_letters, digits from string import ascii_letters, digits
@ -1358,8 +1359,10 @@ class ProposicaoEditView(CreateView):
proposicao.save() proposicao.save()
else: else:
proposicao.delete() proposicao.delete()
elif 'salvar' in request.POST: if 'salvar' or "remover-foto" in request.POST:
if 'texto_original' in request.FILES: if 'texto_original' in request.FILES:
# if os.unlink(proposicao.texto_original.path):
# proposicao.texto_original = None
proposicao.texto_original = request.FILES['texto_original'] proposicao.texto_original = request.FILES['texto_original']
tipo = TipoProposicao.objects.get(id=form.data['tipo']) tipo = TipoProposicao.objects.get(id=form.data['tipo'])
proposicao.tipo = tipo proposicao.tipo = tipo
@ -1379,6 +1382,12 @@ class ProposicaoEditView(CreateView):
proposicao.materia = materia proposicao.materia = materia
if not proposicao.data_envio: if not proposicao.data_envio:
proposicao.data_envio = datetime.now() proposicao.data_envio = datetime.now()
if "remover-texto" in request.POST:
try:
os.unlink(proposicao.texto_original.path)
except OSError:
pass # Should log this error!!!!!
proposicao.texto_original = None
proposicao.save() proposicao.save()
return redirect(self.get_success_url()) return redirect(self.get_success_url())
else: else:

13
parlamentares/forms.py

@ -131,17 +131,18 @@ class ParlamentaresForm (ModelForm):
row6, row7, row8, row9, row10, row6, row7, row8, row9, row10,
row11, row12, row13, row11, row12, row13,
HTML("""<div class="col-md-12"> HTML("""<div class="col-md-12">
{% if not form.fotografia.errors %}
{% if form.fotografia.value %} {% if form.fotografia.value %}
<img class="img-responsive" <img class="img-responsive" width="225" height="300"
width="225" height="300" src="{{ MEDIA_URL }}{{ form.fotografia.value }}">
src="{{MEDIA_URL}}{{form.fotografia.value}}"> <br /><br />
<br />
<input type="submit" <input type="submit"
name="remover" name="remover-foto"
id="remover" id="remover-foto"
class="btn btn-warning" class="btn btn-warning"
value="Remover Foto"/> value="Remover Foto"/>
{% endif %} {% endif %}
{% endif %}
</div>""", ), </div>""", ),
row14, row14,
form_actions()) form_actions())

5
parlamentares/models.py

@ -4,7 +4,7 @@ from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from model_utils import Choices from model_utils import Choices
from sapl.utils import UF, YES_NO_CHOICES from sapl.utils import UF, YES_NO_CHOICES, restringe_tipos_de_arquivo_img
class Legislatura(models.Model): class Legislatura(models.Model):
@ -244,7 +244,8 @@ class Parlamentar(models.Model):
blank=True, blank=True,
null=True, null=True,
upload_to=foto_upload_path, upload_to=foto_upload_path,
verbose_name=_('Fotografia')) verbose_name=_('Fotografia'),
validators=[restringe_tipos_de_arquivo_img])
class Meta: class Meta:
verbose_name = _('Parlamentar') verbose_name = _('Parlamentar')

2
parlamentares/views.py

@ -206,7 +206,7 @@ class ParlamentaresEditarView(UpdateView):
elif 'excluir' in self.request.POST: elif 'excluir' in self.request.POST:
Mandato.objects.get(parlamentar=parlamentar).delete() Mandato.objects.get(parlamentar=parlamentar).delete()
parlamentar.delete() parlamentar.delete()
elif "remover" in self.request.POST: elif "remover-foto" in self.request.POST:
try: try:
os.unlink(parlamentar.fotografia.path) os.unlink(parlamentar.fotografia.path)
except OSError: except OSError:

1
requirements/requirements.txt

@ -18,3 +18,4 @@ pytz==2015.7
pyyaml==3.11 pyyaml==3.11
rtyaml==0.0.2 rtyaml==0.0.2
unipath==1.1 unipath==1.1
python-magic==0.4.10

6
sapl/settings.py

@ -9,15 +9,13 @@ https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/ https://docs.djangoproject.com/en/1.8/ref/settings/
""" """
from decouple import config
from dj_database_url import parse as db_url
from unipath import Path from unipath import Path
from .temp_suppress_crispy_form_warnings import \ from .temp_suppress_crispy_form_warnings import \
SUPRESS_CRISPY_FORM_WARNINGS_LOGGING SUPRESS_CRISPY_FORM_WARNINGS_LOGGING
from decouple import config
from dj_database_url import parse as db_url
BASE_DIR = Path(__file__).ancestor(2) BASE_DIR = Path(__file__).ancestor(2)

59
sapl/utils.py

@ -1,8 +1,10 @@
from datetime import date from datetime import date
from functools import wraps from functools import wraps
import magic
from django.apps import apps from django.apps import apps
from django.contrib import admin from django.contrib import admin
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -123,3 +125,60 @@ UF = [
] ]
RANGE_ANOS = [(year, year) for year in range(date.today().year, 1889, -1)] 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',
'application/xml',
'text/xml',
'text/html',
)
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)

15
sessao/forms.py

@ -1,7 +1,6 @@
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Fieldset, Layout from crispy_forms.layout import Fieldset, Layout
from django import forms from django import forms
from django.core.exceptions import ValidationError
from django.forms import ModelForm from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -94,20 +93,6 @@ class SessaoForm(ModelForm):
'hora_fim': forms.TextInput(attrs={'class': 'hora'}), 'hora_fim': forms.TextInput(attrs={'class': 'hora'}),
} }
def clean_url_audio(self):
url_audio = self.cleaned_data.get('url_audio', False)
if url_audio:
if url_audio.size > MAX_DOC_UPLOAD_SIZE:
raise ValidationError("Arquivo muito grande. ( > 5mb )")
return url_audio
def clean_url_video(self):
url_video = self.cleaned_data.get('url_video', False)
if url_video:
if url_video.size > MAX_DOC_UPLOAD_SIZE:
raise ValidationError("Arquivo muito grande. ( > 5mb )")
return url_video
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
row1 = crispy_layout_mixin.to_row( row1 = crispy_layout_mixin.to_row(

8
sessao/models.py

@ -5,7 +5,7 @@ from model_utils import Choices
from materia.models import MateriaLegislativa from materia.models import MateriaLegislativa
from parlamentares.models import (CargoMesa, Legislatura, Parlamentar, from parlamentares.models import (CargoMesa, Legislatura, Parlamentar,
SessaoLegislativa) SessaoLegislativa)
from sapl.utils import YES_NO_CHOICES from sapl.utils import YES_NO_CHOICES, restringe_tipos_de_arquivo_txt
class TipoSessaoPlenaria(models.Model): class TipoSessaoPlenaria(models.Model):
@ -63,12 +63,14 @@ class SessaoPlenaria(models.Model):
blank=True, blank=True,
null=True, null=True,
upload_to=pauta_upload_path, upload_to=pauta_upload_path,
verbose_name=_('Pauta da Sessão')) verbose_name=_('Pauta da Sessão'),
validators=[restringe_tipos_de_arquivo_txt])
upload_ata = models.FileField( upload_ata = models.FileField(
blank=True, blank=True,
null=True, null=True,
upload_to=ata_upload_path, upload_to=ata_upload_path,
verbose_name=_('Ata da Sessão')) verbose_name=_('Ata da Sessão'),
validators=[restringe_tipos_de_arquivo_txt])
iniciada = models.NullBooleanField(blank=True, iniciada = models.NullBooleanField(blank=True,
choices=YES_NO_CHOICES, choices=YES_NO_CHOICES,
verbose_name=_('Sessão iniciada?')) verbose_name=_('Sessão iniciada?'))

14
templates/materia/proposicao/proposicao_list.html

@ -3,16 +3,16 @@
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
{% block actions %}<!-- Remvoer botões 'Editar' e 'Excluir' -->{% endblock %} {% block actions %}<!-- Remvoer botões 'Editar' e 'Excluir' -->{% endblock %}
<!--
{% block sections_nav %} {% block sections_nav %}
<h2><b>Proposições</b></h2>
<br /> {% endblock %} -->
<div class="actions btn-group pull-right" role="group">
<a href="{% url 'adicionar_proposicao' %}" class="btn btn-default">Nova Proposição</a>
</div>
{% endblock %}
{% block detail_content %} {% block detail_content %}
<div class="actions btn-group pull-right" role="group">
<a href="{% url 'adicionar_proposicao' %}" class="btn btn-default">Nova Proposição</a>
</div>
<h2><b>Proposições</b></h2>
<table class="table table-striped table-bordered"> <table class="table table-striped table-bordered">
<thead class="thead-default"> <thead class="thead-default">
<tr> <tr>

2
templates/sessao/sessao_list.html

@ -3,8 +3,8 @@
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
{% block base_content %} {% block base_content %}
<h2><b>Sessões Plenárias</b></h2>
<div class="actions btn-group pull-right" role="group"> <div class="actions btn-group pull-right" role="group">
<h2>Sessões Plenárias</h2>
<a href="{% url 'sessaoplenaria:sessao_cadastro' %}" class="btn btn-default"> <a href="{% url 'sessaoplenaria:sessao_cadastro' %}" class="btn btn-default">
{% blocktrans with verbose_name=view.verbose_name %} Adicionar Sessão Plenária {% endblocktrans %} {% blocktrans with verbose_name=view.verbose_name %} Adicionar Sessão Plenária {% endblocktrans %}
</a> </a>

Loading…
Cancel
Save