From c9d2295033cc70ad20f49185307ce3076f8c36ed Mon Sep 17 00:00:00 2001 From: Cesar Augusto de Carvalho Date: Fri, 29 Mar 2019 15:33:01 -0300 Subject: [PATCH] =?UTF-8?q?Fix=20Documento=20Acess=C3=B3rio=20em=20Lote=20?= =?UTF-8?q?para=20Arquivos=20Grandes=20(#2664)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0044_auto_20190327_1409.py | 22 +++++++++++++ sapl/materia/models.py | 1 + sapl/materia/views.py | 32 ++++++++++++++++--- 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 sapl/materia/migrations/0044_auto_20190327_1409.py diff --git a/sapl/materia/migrations/0044_auto_20190327_1409.py b/sapl/materia/migrations/0044_auto_20190327_1409.py new file mode 100644 index 000000000..5322d7833 --- /dev/null +++ b/sapl/materia/migrations/0044_auto_20190327_1409.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-03-27 17:09 +from __future__ import unicode_literals + +from django.db import migrations, models +import sapl.materia.models +import sapl.utils + + +class Migration(migrations.Migration): + + dependencies = [ + ('materia', '0043_auto_20190320_1749'), + ] + + operations = [ + migrations.AlterField( + model_name='documentoacessorio', + name='arquivo', + field=models.FileField(blank=True, max_length=255, null=True, upload_to=sapl.materia.models.anexo_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Texto Integral'), + ), + ] diff --git a/sapl/materia/models.py b/sapl/materia/models.py index af4cb1231..c7aaaed15 100644 --- a/sapl/materia/models.py +++ b/sapl/materia/models.py @@ -497,6 +497,7 @@ class DocumentoAcessorio(models.Model): arquivo = models.FileField( blank=True, null=True, + max_length=255, upload_to=anexo_upload_path, verbose_name=_('Texto Integral'), validators=[restringe_tipos_de_arquivo_txt]) diff --git a/sapl/materia/views.py b/sapl/materia/views.py index 696b1235a..846f367fa 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -1,5 +1,10 @@ -from datetime import datetime import logging +import os +import shutil +import tempfile +import weasyprint + +from datetime import datetime from random import choice from string import ascii_letters, digits @@ -45,6 +50,7 @@ from sapl.materia.forms import (AnexadaForm, AutoriaForm, from sapl.norma.models import LegislacaoCitada from sapl.parlamentares.models import Legislatura from sapl.protocoloadm.models import Protocolo +from sapl.settings import MEDIA_ROOT from sapl.utils import (YES_NO_CHOICES, autor_label, autor_modal, SEPARADOR_HASH_PROPOSICAO, gerar_hash_arquivo, get_base_url, get_mime_type_from_file_extension, montar_row_autor, @@ -2035,17 +2041,35 @@ class DocumentoAcessorioEmLoteView(PermissionRequiredMixin, FilterView): messages.add_message(request, messages.ERROR, msg) return self.get(request, self.kwargs) + tmp_name = os.path.join(tempfile.gettempdir(), request.FILES['arquivo'].name) + with open(tmp_name, 'wb') as destination: + for chunk in request.FILES['arquivo'].chunks(): + destination.write(chunk) + + doc_data = tz.localize(datetime.strptime( + request.POST['data'], "%d/%m/%Y")) for materia_id in marcadas: doc = DocumentoAcessorio() doc.materia_id = materia_id doc.tipo = tipo - doc.arquivo = request.FILES['arquivo'] doc.nome = request.POST['nome'] - doc.data = tz.localize(datetime.strptime( - request.POST['data'], "%d/%m/%Y")) + doc.data = doc_data doc.autor = request.POST['autor'] doc.ementa = request.POST['ementa'] doc.save() + diretorio = os.path.join(MEDIA_ROOT, + 'sapl/public/documentoacessorio', + str(doc_data.year), + str(doc.id)) + if not os.path.exists(diretorio): + os.makedirs(diretorio) + file_path = os.path.join(diretorio, + request.FILES['arquivo'].name) + shutil.copy2(tmp_name, file_path) + doc.arquivo.name = file_path.split(MEDIA_ROOT)[1] # Retira MEDIA_ROOT do nome + doc.save() + os.remove(tmp_name) + msg = _('Documento(s) criado(s).') messages.add_message(request, messages.SUCCESS, msg) return self.get(request, self.kwargs)