diff --git a/sapl/base/forms.py b/sapl/base/forms.py index 754cca79e..3ebc11689 100644 --- a/sapl/base/forms.py +++ b/sapl/base/forms.py @@ -40,7 +40,7 @@ from sapl.utils import (autor_label, autor_modal, ChoiceWithoutValidationField, FilterOverridesMetaMixin, FileFieldCheckMixin, ImageThumbnailFileInput, qs_override_django_filter, RANGE_ANOS, YES_NO_CHOICES, choice_tipos_normas, - GoogleRecapthaMixin, parlamentares_ativos, RANGE_MESES, is_weak_password) + GoogleRecapthaMixin, parlamentares_ativos, RANGE_MESES, is_weak_password, delete_cached_entry) from .models import AppConfig, CasaLegislativa @@ -921,13 +921,17 @@ class CasaLegislativaForm(FileFieldCheckMixin, ModelForm): # chama __clean de FileFieldCheckMixin # por estar em clean de campo super(CasaLegislativaForm, self)._check() - logotipo = self.cleaned_data.get('logotipo') - if logotipo: - if logotipo.size > MAX_IMAGE_UPLOAD_SIZE: - raise ValidationError("Imagem muito grande. ( > 2MB )") + if logotipo and logotipo.size > MAX_IMAGE_UPLOAD_SIZE: + raise ValidationError("Imagem muito grande. ( > 2MB )") return logotipo + def save(self, commit=True): + casa = super(CasaLegislativaForm, self).save(commit=commit) + delete_cached_entry("site-title") + return casa + + class LoginForm(AuthenticationForm): username = forms.CharField( diff --git a/sapl/context_processors.py b/sapl/context_processors.py index 2b8a422cb..b3f1631de 100644 --- a/sapl/context_processors.py +++ b/sapl/context_processors.py @@ -1,14 +1,13 @@ import logging -from django.conf import settings from django.utils.translation import ugettext_lazy as _ -from sapl.utils import google_recaptcha_configured as google_recaptcha_configured_utils, sapn_is_enabled +from sapl.utils import google_recaptcha_configured as \ + google_recaptcha_configured_utils, sapn_is_enabled, cached_call, get_base_url from sapl.utils import mail_service_configured as mail_service_configured_utils def parliament_info(request): - from sapl.base.views import get_casalegislativa casa = get_casalegislativa() if casa: @@ -18,7 +17,6 @@ def parliament_info(request): def mail_service_configured(request): - if not mail_service_configured_utils(request): logger = logging.getLogger(__name__) logger.warning(_('Servidor de email não configurado.')) @@ -27,7 +25,6 @@ def mail_service_configured(request): def google_recaptcha_configured(request): - if not google_recaptcha_configured_utils(): logger = logging.getLogger(__name__) logger.warning(_('Google Recaptcha não configurado.')) @@ -35,10 +32,19 @@ def google_recaptcha_configured(request): return {'google_recaptcha_configured': True} +@cached_call("site-title", timeout=2 * 60) def enable_sapn(request): + verbose_name = _('Sistema de Apoio ao Processo Legislativo') \ + if not sapn_is_enabled() \ + else _('Sistema de Apoio à Publicação de Leis e Normas') + + from sapl.base.models import CasaLegislativa + casa_legislativa = CasaLegislativa.objects.first() + if casa_legislativa: + verbose_name = casa_legislativa.nome + return { 'sapl_as_sapn': sapn_is_enabled(), - 'nome_sistema': _('Sistema de Apoio ao Processo Legislativo') - if not sapn_is_enabled() - else _('Sistema de Apoio à Publicação de Leis e Normas') + 'nome_sistema': verbose_name, + 'base_url': get_base_url(request), } diff --git a/sapl/settings.py b/sapl/settings.py index 5e379f861..b1c1abba5 100644 --- a/sapl/settings.py +++ b/sapl/settings.py @@ -194,6 +194,7 @@ CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', 'LOCATION': '/var/tmp/django_cache', + 'OPTIONS': {"MAX_ENTRIES": 1000}, } } diff --git a/sapl/templates/base.html b/sapl/templates/base.html index 4bc85410c..de36fe06e 100644 --- a/sapl/templates/base.html +++ b/sapl/templates/base.html @@ -13,6 +13,14 @@ {% block head_title %}{% switch "SAPLN_SWITCH" %}SAPL-Normas - {% else %}SAPL - {%endswitch%}{{nome_sistema}}{% endblock %} + diff --git a/sapl/utils.py b/sapl/utils.py index 6fd120b82..cddeb1f49 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -23,6 +23,7 @@ from django.conf import settings from django.contrib import admin from django.contrib.contenttypes.fields import (GenericForeignKey, GenericRel, GenericRelation) +from django.core.cache import cache from django.core.exceptions import ValidationError from django.core.files.storage import FileSystemStorage from django.core.files.uploadedfile import UploadedFile, InMemoryUploadedFile, \ @@ -1093,13 +1094,31 @@ def timing(f): ts = time() result = f(*args, **kw) te = time() - logger.info('funcao:%r args:[%r, %r] took: %2.4f sec' % + logger.info('function:%r args:[%r, %r] took: %2.4f sec' % (f.__name__, args, kw, te - ts)) return result return wrap +def cached_call(key, timeout=300): + def cache_decorator(f): + @wraps(f) + def wrap(*args, **kw): + result = cache.get(key) + if not result: + result = f(*args, **kw) + cache.set(key, result, timeout) + return result + + return wrap + return cache_decorator + + +def delete_cached_entry(key): + cache.delete(key) + + @timing def lista_anexados(principal): from sapl.materia.models import MateriaLegislativa @@ -1614,11 +1633,13 @@ class PautaMultiFormatOutputMixin(MultiFormatOutputMixin): v[rc] = f'{v[rc]}\r\n{cell}' data[mri] = dict( - map(lambda i, j: (i[0], j if type(j) in [str, int, list] else str(j)), self.fields_report['json'][index], v)) + map(lambda i, j: (i[0], j if type(j) in [str, int, list] else str(j)), + self.fields_report['json'][index], v)) - json_metadata.update({item[0]:{ + json_metadata.update({item[0]: { 'headers': dict( - map(lambda i, j: (i[0], j), self.fields_report['json'][index], self._headers(self.fields_report['json'][index]))), + map(lambda i, j: (i[0], j), self.fields_report['json'][index], + self._headers(self.fields_report['json'][index]))), 'results': data} }) response = JsonResponse(json_metadata)