From 34786902e63bd9a16b51f7fa3cb3f6b66ef33275 Mon Sep 17 00:00:00 2001 From: Cesar Augusto de Carvalho Date: Mon, 8 Apr 2019 13:50:14 -0300 Subject: [PATCH] =?UTF-8?q?Fix=20Verifica=C3=A7=C3=A3o=20de=20Erros=20do?= =?UTF-8?q?=20Formul=C3=A1rio=20em=20Documento=20Acess=C3=B3rio=20em=20Lot?= =?UTF-8?q?e=20(#2693)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix Verificação de Erros do Formulário em Documento Acessorio em Lote * Altera o logger --- sapl/materia/views.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/sapl/materia/views.py b/sapl/materia/views.py index 6c0da0ce0..8d7278e5f 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -14,7 +14,7 @@ from django.conf import settings from django.contrib import messages from django.contrib.auth.decorators import permission_required from django.contrib.auth.mixins import PermissionRequiredMixin -from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned +from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, ValidationError from django.core.urlresolvers import reverse from django.db.models import Max, Q from django.http import HttpResponse, JsonResponse @@ -1945,6 +1945,7 @@ class DocumentoAcessorioEmLoteView(PermissionRequiredMixin, FilterView): filterset_class = AcessorioEmLoteFilterSet template_name = 'materia/em_lote/acessorio.html' permission_required = ('materia.add_documentoacessorio',) + logger = logging.getLogger(__name__) def get_context_data(self, **kwargs): context = super(DocumentoAcessorioEmLoteView, @@ -1966,6 +1967,7 @@ class DocumentoAcessorioEmLoteView(PermissionRequiredMixin, FilterView): return context def post(self, request, *args, **kwargs): + username = request.user.username marcadas = request.POST.getlist('materia_id') if len(marcadas) == 0: @@ -1981,14 +1983,21 @@ class DocumentoAcessorioEmLoteView(PermissionRequiredMixin, FilterView): msg = _('Autor tem que ter menos do que 50 caracteres.') messages.add_message(request, messages.ERROR, msg) return self.get(request, self.kwargs) - - tmp_name = os.path.join(tempfile.gettempdir(), request.FILES['arquivo'].name) + + tmp_name = os.path.join(MEDIA_ROOT, request.FILES['arquivo'].name) with open(tmp_name, 'wb') as destination: for chunk in request.FILES['arquivo'].chunks(): destination.write(chunk) + try: + doc_data = tz.localize(datetime.strptime( + request.POST['data'], "%d/%m/%Y")) + except Exception as e: + msg = _('Formato da data incorreto. O formato deve ser da forma dd/mm/aaaa.') + messages.add_message(request, messages.ERROR, msg) + self.logger.error("User={}. {}. Data inserida: {}".format(username, str(msg), request.POST['data'])) + os.remove(tmp_name) + return self.get(request, self.kwargs) - doc_data = tz.localize(datetime.strptime( - request.POST['data'], "%d/%m/%Y")) for materia_id in marcadas: doc = DocumentoAcessorio() doc.materia_id = materia_id @@ -1997,6 +2006,18 @@ class DocumentoAcessorioEmLoteView(PermissionRequiredMixin, FilterView): doc.data = doc_data doc.autor = request.POST['autor'] doc.ementa = request.POST['ementa'] + doc.arquivo.name = tmp_name + try: + doc.clean_fields() + except ValidationError as e: + for m in [ '%s: %s' % (DocumentoAcessorio()._meta.get_field(k).verbose_name, '
'.join(v)) + for k,v in e.message_dict.items() ]: + # Insere as mensagens de erro no formato: + # 'verbose_name do nome do campo': 'mensagem de erro' + messages.add_message(request, messages.ERROR, m) + self.logger.error("User={}. {}. Nome do arquivo: {}.".format(username, str(msg), request.FILES['arquivo'].name)) + os.remove(tmp_name) + return self.get(request, self.kwargs) doc.save() diretorio = os.path.join(MEDIA_ROOT, 'sapl/public/documentoacessorio',