From 75c98903bed20ff487faa5b877f0247e0b710f06 Mon Sep 17 00:00:00 2001 From: Leandro Roberto Date: Tue, 25 Sep 2018 10:30:36 -0300 Subject: [PATCH] Retira config e impl da lib logging no sapl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Foram desativadas as configurações superficiais e imprecisas não condisentes com o sapl em produção. A impl atual estava causando dificuldades na migração da versão do django, além disso não estava tendo mínimo efeito para o mais urgente, registro claro de problemas na produção. Uma nova solução deve ser apresentada como tarefa da Issue #52, na qual tomei a liberdade de adicionar a label 'prioridade alta' e alterar o Milestone para 3.1... Por que? por não ser uma melhoria para usuário final e sim uma demanda urgente dos devs e da infra. --- sapl/api/views.py | 14 +++---- sapl/base/search_indexes.py | 13 ++---- sapl/base/templatetags/menus.py | 5 +-- sapl/base/views.py | 70 ++++++++++++++++++++------------- sapl/compilacao/apps.py | 7 +--- sapl/compilacao/views.py | 8 +--- sapl/crud/base.py | 6 +-- sapl/settings.py | 7 ++-- sapl/utils.py | 18 ++++----- 9 files changed, 69 insertions(+), 79 deletions(-) diff --git a/sapl/api/views.py b/sapl/api/views.py index 9853e6af0..3ec527646 100644 --- a/sapl/api/views.py +++ b/sapl/api/views.py @@ -1,3 +1,4 @@ + from django.contrib.contenttypes.models import ContentType from django.db.models import Q from django.http import Http404 @@ -19,7 +20,7 @@ from sapl.api.serializers import (AutorChoiceSerializer, AutorSerializer, from sapl.base.models import Autor, TipoAutor from sapl.materia.models import MateriaLegislativa from sapl.sessao.models import SessaoPlenaria -from sapl.utils import SaplGenericRelation, sapl_logger +from sapl.utils import SaplGenericRelation class ModelChoiceView(ListAPIView): @@ -141,17 +142,14 @@ class AutorListView(ListAPIView): tr = int(self.request.GET.get ('tr', AutorListView.TR_AUTOR_CHOICE_SERIALIZER)) - assert tr in ( - AutorListView.TR_AUTOR_CHOICE_SERIALIZER, - AutorListView.TR_AUTOR_SERIALIZER), sapl_logger.info( - _("Tipo do Resultado a ser fornecido não existe!")) + if tr not in (AutorListView.TR_AUTOR_CHOICE_SERIALIZER, + AutorListView.TR_AUTOR_SERIALIZER): + return AutorListView.TR_AUTOR_CHOICE_SERIALIZER except: return AutorListView.TR_AUTOR_CHOICE_SERIALIZER - else: - return tr + return tr def get(self, request, *args, **kwargs): - if self.tr == AutorListView.TR_AUTOR_SERIALIZER: self.serializer_class = AutorSerializer self.permission_classes = (IsAuthenticated,) diff --git a/sapl/base/search_indexes.py b/sapl/base/search_indexes.py index ed811cebb..479ecb426 100644 --- a/sapl/base/search_indexes.py +++ b/sapl/base/search_indexes.py @@ -1,30 +1,25 @@ -import logging import os.path import re import string -import textract from django.db.models import F, Q, Value from django.db.models.fields import TextField -from django.db.models.fields.files import FieldFile from django.db.models.functions import Concat from django.template import loader from haystack.constants import Indexable from haystack.fields import CharField from haystack.indexes import SearchIndex from haystack.utils import get_model_ct_tuple +import textract from textract.exceptions import ExtensionNotSupported from sapl.compilacao.models import (STATUS_TA_IMMUTABLE_PUBLIC, - STATUS_TA_PUBLIC, Dispositivo, - TextoArticulado) + STATUS_TA_PUBLIC, Dispositivo) from sapl.materia.models import DocumentoAcessorio, MateriaLegislativa from sapl.norma.models import NormaJuridica -from sapl.settings import BASE_DIR, SOLR_URL +from sapl.settings import SOLR_URL from sapl.utils import RemoveTag -logger = logging.getLogger(BASE_DIR.name) - class TextExtractField(CharField): @@ -68,7 +63,6 @@ class TextExtractField(CharField): msg = 'Erro inesperado processando arquivo: %s' % ( arquivo.path) print(msg) - logger.error(msg) def file_extractor(self, arquivo): if not os.path.exists(arquivo.path) or \ @@ -89,7 +83,6 @@ class TextExtractField(CharField): return self.whoosh_extraction(arquivo) except ExtensionNotSupported as e: print(str(e)) - logger.error(str(e)) except Exception as e2: print(str(e2)) self.print_error(arquivo) diff --git a/sapl/base/templatetags/menus.py b/sapl/base/templatetags/menus.py index f90a6940f..289cd20e6 100644 --- a/sapl/base/templatetags/menus.py +++ b/sapl/base/templatetags/menus.py @@ -1,9 +1,8 @@ -import yaml from django import template from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ +import yaml -from sapl.utils import sapl_logger register = template.Library() @@ -85,7 +84,7 @@ def nav_run(context, path=None): menu = yaml.load(rendered) resolve_urls_inplace(menu, root_pk, rm, context) except Exception as e: - sapl_logger.error(_("""Erro na conversão do yaml %s. App: %s. + print(_("""Erro na conversão do yaml %s. App: %s. Erro: %s """) % ( diff --git a/sapl/base/views.py b/sapl/base/views.py index ce0e8e0cf..2ce1abf92 100644 --- a/sapl/base/views.py +++ b/sapl/base/views.py @@ -13,8 +13,8 @@ from django.template import TemplateDoesNotExist from django.template.loader import get_template from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode -from django.utils.translation import ugettext_lazy as _ from django.utils.translation import string_concat +from django.utils.translation import ugettext_lazy as _ from django.views.generic import (CreateView, DeleteView, FormView, ListView, UpdateView) from django.views.generic.base import RedirectView, TemplateView @@ -22,16 +22,16 @@ from django_filters.views import FilterView from haystack.views import SearchView from sapl import settings +from sapl.audiencia.models import AudienciaPublica, TipoAudienciaPublica from sapl.base.forms import AutorForm, AutorFormForAdmin, TipoAutorForm from sapl.base.models import Autor, TipoAutor -from sapl.crud.base import CrudAux, make_pagination -from sapl.audiencia.models import AudienciaPublica, TipoAudienciaPublica from sapl.comissoes.models import Reuniao, Comissao +from sapl.crud.base import CrudAux, make_pagination from sapl.materia.models import (Autoria, MateriaLegislativa, TipoMateriaLegislativa, StatusTramitacao, UnidadeTramitacao) from sapl.sessao.models import (PresencaOrdemDia, SessaoPlenaria, SessaoPlenariaPresenca) -from sapl.utils import (parlamentares_ativos, sapl_logger, +from sapl.utils import (parlamentares_ativos, show_results_filter_set) from .forms import (AlterarSenhaForm, CasaLegislativaForm, @@ -52,9 +52,11 @@ def filtra_url_materias_em_tramitacao(qr, qs, campo_url, local_ou_status): id_materias = [] filtro_url = qr[campo_url] if local_ou_status == 'local': - id_materias = [item.id for item in qs if item.tramitacao_set.order_by('-id').first().unidade_tramitacao_destino_id == int(filtro_url)] + id_materias = [item.id for item in qs if item.tramitacao_set.order_by( + '-id').first().unidade_tramitacao_destino_id == int(filtro_url)] elif local_ou_status == 'status': - id_materias = [item.id for item in qs if item.tramitacao_set.order_by('-id').first().status_id == int(filtro_url)] + id_materias = [item.id for item in qs if item.tramitacao_set.order_by( + '-id').first().status_id == int(filtro_url)] return qs.filter(em_tramitacao=True, id__in=id_materias) @@ -193,7 +195,7 @@ class AutorCrud(CrudAux): send_mail(assunto, mensagem, remetente, destinatario, fail_silently=False) except: - sapl_logger.error( + print( _('Erro no envio de email na edição de Autores.')) return url_reverse @@ -243,7 +245,7 @@ class AutorCrud(CrudAux): send_mail(assunto, mensagem, remetente, destinatario, fail_silently=False) except: - sapl_logger.error( + print( _('Erro no envio de email na criação de Autores.')) return url_reverse @@ -267,8 +269,8 @@ class RelatorioAtasView(FilterView): context['show_results'] = show_results_filter_set(qr) context['periodo'] = ( - self.request.GET['data_inicio_0'] + - ' - ' + self.request.GET['data_inicio_1']) + self.request.GET['data_inicio_0'] + + ' - ' + self.request.GET['data_inicio_1']) return context @@ -384,20 +386,23 @@ class RelatorioHistoricoTramitacaoView(FilterView): context['show_results'] = show_results_filter_set(qr) context['data_tramitacao'] = (self.request.GET['tramitacao__data_tramitacao_0'] + ' - ' + - self.request.GET['tramitacao__data_tramitacao_1']) + self.request.GET['tramitacao__data_tramitacao_1']) if self.request.GET['tipo']: tipo = self.request.GET['tipo'] - context['tipo'] = (str(TipoMateriaLegislativa.objects.get(id=tipo))) + context['tipo'] = ( + str(TipoMateriaLegislativa.objects.get(id=tipo))) else: context['tipo'] = '' if self.request.GET['tramitacao__status']: tramitacao_status = self.request.GET['tramitacao__status'] - context['tramitacao__status'] = (str(StatusTramitacao.objects.get(id=tramitacao_status))) + context['tramitacao__status'] = ( + str(StatusTramitacao.objects.get(id=tramitacao_status))) else: context['tramitacao__status'] = '' if self.request.GET['tramitacao__unidade_tramitacao_local']: context['tramitacao__unidade_tramitacao_local'] = \ - (str(UnidadeTramitacao.objects.get(id=self.request.GET['tramitacao__unidade_tramitacao_local']))) + (str(UnidadeTramitacao.objects.get( + id=self.request.GET['tramitacao__unidade_tramitacao_local']))) else: context['tramitacao__unidade_tramitacao_destino'] = '' @@ -424,22 +429,26 @@ class RelatorioDataFimPrazoTramitacaoView(FilterView): self.request.GET['tramitacao__data_fim_prazo_1']) if self.request.GET['tipo']: tipo = self.request.GET['tipo'] - context['tipo'] = (str(TipoMateriaLegislativa.objects.get(id=tipo))) + context['tipo'] = ( + str(TipoMateriaLegislativa.objects.get(id=tipo))) else: context['tipo'] = '' if self.request.GET['tramitacao__status']: tramitacao_status = self.request.GET['tramitacao__status'] - context['tramitacao__status'] = (str(StatusTramitacao.objects.get(id=tramitacao_status))) + context['tramitacao__status'] = ( + str(StatusTramitacao.objects.get(id=tramitacao_status))) else: context['tramitacao__status'] = '' if self.request.GET['tramitacao__unidade_tramitacao_local']: context['tramitacao__unidade_tramitacao_local'] = \ - (str(UnidadeTramitacao.objects.get(id=self.request.GET['tramitacao__unidade_tramitacao_local']))) + (str(UnidadeTramitacao.objects.get( + id=self.request.GET['tramitacao__unidade_tramitacao_local']))) else: context['tramitacao__unidade_tramitacao_destino'] = '' return context + class RelatorioReuniaoView(FilterView): model = Reuniao filterset_class = RelatorioReuniaoFilterSet @@ -459,7 +468,7 @@ class RelatorioReuniaoView(FilterView): if not self.filterset.form.is_valid(): return context qr = self.request.GET.copy() - + context['filter_url'] = ('&' + qr.urlencode()) if len(qr) > 0 else '' context['show_results'] = show_results_filter_set(qr) @@ -472,6 +481,7 @@ class RelatorioReuniaoView(FilterView): return context + class RelatorioAudienciaView(FilterView): model = AudienciaPublica filterset_class = RelatorioAudienciaFilterSet @@ -505,7 +515,6 @@ class RelatorioAudienciaView(FilterView): return context - class RelatorioMateriasTramitacaoView(FilterView): model = MateriaLegislativa filterset_class = RelatorioMateriasTramitacaoilterSet @@ -524,9 +533,11 @@ class RelatorioMateriasTramitacaoView(FilterView): qs = qs.filter(em_tramitacao=True) if qr.get('tramitacao__unidade_tramitacao_destino'): - qs = filtra_url_materias_em_tramitacao(qr, qs, 'tramitacao__unidade_tramitacao_destino', 'local') + qs = filtra_url_materias_em_tramitacao( + qr, qs, 'tramitacao__unidade_tramitacao_destino', 'local') if qr.get('tramitacao__status'): - qs = filtra_url_materias_em_tramitacao(qr, qs, 'tramitacao__status', 'status') + qs = filtra_url_materias_em_tramitacao( + qr, qs, 'tramitacao__status', 'status') context['object_list'] = qs @@ -540,17 +551,19 @@ class RelatorioMateriasTramitacaoView(FilterView): context['ano'] = (self.request.GET['ano']) if self.request.GET['tipo']: tipo = self.request.GET['tipo'] - context['tipo'] = (str(TipoMateriaLegislativa.objects.get(id=tipo))) + context['tipo'] = ( + str(TipoMateriaLegislativa.objects.get(id=tipo))) else: context['tipo'] = '' if self.request.GET['tramitacao__status']: tramitacao_status = self.request.GET['tramitacao__status'] - context['tramitacao__status'] = (str(StatusTramitacao.objects.get(id=tramitacao_status))) + context['tramitacao__status'] = ( + str(StatusTramitacao.objects.get(id=tramitacao_status))) else: context['tramitacao__status'] = '' if self.request.GET['tramitacao__unidade_tramitacao_destino']: - context['tramitacao__unidade_tramitacao_destino'] = (str(UnidadeTramitacao.objects.get(id= - self.request.GET['tramitacao__unidade_tramitacao_destino']))) + context['tramitacao__unidade_tramitacao_destino'] = (str(UnidadeTramitacao.objects.get( + id=self.request.GET['tramitacao__unidade_tramitacao_destino']))) else: context['tramitacao__unidade_tramitacao_destino'] = '' context['filter_url'] = ('&' + qr.urlencode()) if len(qr) > 0 else '' @@ -677,7 +690,8 @@ class RelatorioMateriasPorAutorView(FilterView): context['show_results'] = show_results_filter_set(qr) if self.request.GET['tipo']: tipo = int(self.request.GET['tipo']) - context['tipo'] = (str(TipoMateriaLegislativa.objects.get(id=tipo))) + context['tipo'] = ( + str(TipoMateriaLegislativa.objects.get(id=tipo))) else: context['tipo'] = '' if self.request.GET['autoria__autor']: @@ -686,8 +700,8 @@ class RelatorioMateriasPorAutorView(FilterView): else: context['autor'] = '' context['periodo'] = ( - self.request.GET['data_apresentacao_0'] + - ' - ' + self.request.GET['data_apresentacao_1']) + self.request.GET['data_apresentacao_0'] + + ' - ' + self.request.GET['data_apresentacao_1']) return context diff --git a/sapl/compilacao/apps.py b/sapl/compilacao/apps.py index 96bd10b87..f20336fba 100644 --- a/sapl/compilacao/apps.py +++ b/sapl/compilacao/apps.py @@ -1,16 +1,13 @@ -import logging from django import apps from django.conf import settings from django.db import connection, models from django.db.utils import DEFAULT_DB_ALIAS, IntegrityError -from django.utils.translation import ugettext_lazy as _ from django.utils.translation import string_concat +from django.utils.translation import ugettext_lazy as _ from sapl.settings import BASE_DIR -logger = logging.getLogger(BASE_DIR.name) - class AppConfig(apps.AppConfig): name = 'sapl.compilacao' @@ -42,7 +39,7 @@ class AppConfig(apps.AppConfig): cursor.execute(line) except IntegrityError as e: if not settings.DEBUG: - logger.error( + print( string_concat( _('Ocorreu erro na importação: '), line, diff --git a/sapl/compilacao/views.py b/sapl/compilacao/views.py index 5d4a81ef2..dc8dc42c8 100644 --- a/sapl/compilacao/views.py +++ b/sapl/compilacao/views.py @@ -1,6 +1,5 @@ from collections import OrderedDict from datetime import timedelta -import logging import sys from braces.views import FormMessagesMixin @@ -11,9 +10,8 @@ from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.contenttypes.models import ContentType from django.core.signing import Signer from django.core.urlresolvers import reverse, reverse_lazy -from django.db import connection, transaction +from django.db import transaction from django.db.models import Q -from django.db.utils import IntegrityError from django.http.response import (HttpResponse, HttpResponseRedirect, JsonResponse, Http404) from django.shortcuts import get_object_or_404, redirect @@ -58,8 +56,6 @@ VeiculoPublicacaoCrud = CrudAux.build(VeiculoPublicacao, 'veiculo_publicacao') TipoDispositivoCrud = CrudAux.build( TipoDispositivo, 'tipo_dispositivo') -logger = logging.getLogger(BASE_DIR.name) - def choice_models_in_extenal_views(): integrations_view_names = get_integrations_view_names() @@ -119,7 +115,7 @@ class IntegracaoTaView(TemplateView): tipo_ta.save() except Exception as e: - logger.error( + print( string_concat( _('Ocorreu erro na importação do arquivo base dos Tipos de' 'Dispositivos, entre outras informações iniciais.'), diff --git a/sapl/crud/base.py b/sapl/crud/base.py index 77462a5f8..162668c76 100644 --- a/sapl/crud/base.py +++ b/sapl/crud/base.py @@ -1,4 +1,3 @@ -import logging from braces.views import FormMessagesMixin from compressor.utils.decorators import cached_property @@ -30,9 +29,6 @@ from sapl.rules.map_rules import (RP_ADD, RP_CHANGE, RP_DELETE, RP_DETAIL, from sapl.settings import BASE_DIR from sapl.utils import normalize - -logger = logging.getLogger(BASE_DIR.name) - ACTION_LIST, ACTION_CREATE, ACTION_DETAIL, ACTION_UPDATE, ACTION_DELETE = \ 'list', 'create', 'detail', 'update', 'delete' @@ -583,7 +579,7 @@ class CrudListView(PermissionRequiredContainerCrudMixin, ListView): # print(ordering) except Exception as e: - logger.error(string_concat(_( + print(string_concat(_( 'ERRO: construção da tupla de ordenação.'), str(e))) # print(queryset.query) diff --git a/sapl/settings.py b/sapl/settings.py index b4da172f8..7413ecb81 100644 --- a/sapl/settings.py +++ b/sapl/settings.py @@ -13,7 +13,6 @@ Quick-start development settings - unsuitable for production See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ """ -import logging from decouple import config from dj_database_url import parse as db_url @@ -300,8 +299,10 @@ FILTERS_HELP_TEXT_FILTER = False LOGGING = SUPRESS_CRISPY_FORM_WARNINGS_LOGGING +# FIXME: gerando problemas na alternancia entre django 1.9.13 e 1.10.8 +# Issue 52 https://github.com/interlegis/sapl/issues/52 LOGGING_CONSOLE = config('LOGGING_CONSOLE', default=False, cast=bool) -if DEBUG and LOGGING_CONSOLE: +"""if DEBUG and LOGGING_CONSOLE: # Descomentar linha abaixo fará com que logs aparecam, inclusive SQL # LOGGING['handlers']['console']['level'] = 'DEBUG' LOGGING['loggers']['django']['level'] = 'DEBUG' @@ -325,7 +326,7 @@ def excepthook(*args): logging.getLogger(BASE_DIR.name).error( 'Uncaught exception:', exc_info=args) -# sys.excepthook = excepthook +# sys.excepthook = excepthook""" PASSWORD_HASHERS = [ diff --git a/sapl/utils.py b/sapl/utils.py index 691034789..257489321 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -1,14 +1,11 @@ +from functools import wraps import hashlib -import logging +from operator import itemgetter import os import re -import unicodedata -from functools import wraps -from operator import itemgetter from unicodedata import normalize as unicodedata_normalize +import unicodedata -import django_filters -import magic from crispy_forms.helper import FormHelper from crispy_forms.layout import HTML, Button from django import forms @@ -21,17 +18,15 @@ from django.core.exceptions import ValidationError from django.db.models import Q from django.utils import six, timezone from django.utils.translation import ugettext_lazy as _ +import django_filters from django_filters.filterset import STRICTNESS from easy_thumbnails import source_generators from floppyforms import ClearableFileInput -from reversion.admin import VersionAdmin +import magic from reversion_compare.admin import CompareVersionAdmin from unipath.path import Path from sapl.crispy_layout_mixin import SaplFormLayout, form_actions, to_row -from sapl.settings import BASE_DIR - -sapl_logger = logging.getLogger(BASE_DIR.name) def pil_image(source, exif_orientation=False, **options): @@ -765,5 +760,6 @@ def RemoveTag(texto): return textoSaida + def remover_acentos(string): - return unicodedata.normalize('NFKD', string).encode('ASCII', 'ignore').decode() + return unicodedata.normalize('NFKD', string).encode('ASCII', 'ignore').decode()