From 66bf26911befcc9fff6f9e7d332982e763aea441 Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Tue, 27 Feb 2018 15:25:31 -0300 Subject: [PATCH] =?UTF-8?q?refatora=20gera=C3=A7=C3=A3o=20de=20base=20inic?= =?UTF-8?q?ial=20de=20compila=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sapl/compilacao/apps.py | 96 +++++++++++++++++++++++++++++++++++++++- sapl/compilacao/views.py | 63 +------------------------- 2 files changed, 97 insertions(+), 62 deletions(-) diff --git a/sapl/compilacao/apps.py b/sapl/compilacao/apps.py index 85c8ba349..6b96175b5 100644 --- a/sapl/compilacao/apps.py +++ b/sapl/compilacao/apps.py @@ -1,8 +1,102 @@ +import logging + from django import apps -from django.utils.translation import ugettext_lazy as _ +from django.conf import settings +from django.db import models, connection +from django.db.utils import DEFAULT_DB_ALIAS, IntegrityError +from django.utils.translation import ugettext_lazy as _, string_concat + + +from sapl.settings import BASE_DIR + +logger = logging.getLogger(BASE_DIR.name) class AppConfig(apps.AppConfig): name = 'sapl.compilacao' label = 'compilacao' verbose_name = _('Compilação') + + @staticmethod + def import_pattern(): + + from sapl.compilacao.models import TipoTextoArticulado + from sapl.compilacao.utils import get_integrations_view_names + + from django.contrib.contenttypes.models import ContentType + from unipath import Path + + compilacao_app = Path(__file__).ancestor(1) + # print(compilacao_app) + with open(compilacao_app + '/compilacao_data_tables.sql', 'r') as f: + lines = f.readlines() + lines = [line.rstrip('\n') for line in lines] + + with connection.cursor() as cursor: + for line in lines: + line = line.strip() + if not line or line.startswith('#'): + continue + + try: + cursor.execute(line) + except IntegrityError as e: + if not settings.DEBUG: + logger.error( + string_concat( + _('Ocorreu erro na importação: '), + line, + str(e))) + except Exception as ee: + print(ee) + + integrations_view_names = get_integrations_view_names() + + def cria_sigla(verbose_name): + verbose_name = verbose_name.upper().split() + if len(verbose_name) == 1: + verbose_name = verbose_name[0] + sigla = '' + for letra in verbose_name: + if letra in 'BCDFGHJKLMNPQRSTVWXYZ': + sigla += letra + else: + sigla = ''.join([palavra[0] for palavra in verbose_name]) + return sigla[:3] + + for view in integrations_view_names: + try: + tipo = TipoTextoArticulado() + tipo.sigla = cria_sigla( + view.model._meta.verbose_name + if view.model._meta.verbose_name + else view.model._meta.model_name) + tipo.descricao = view.model._meta.verbose_name + tipo.content_type = ContentType.objects.get_by_natural_key( + view.model._meta.app_label, view.model._meta.model_name) + tipo.save() + except IntegrityError as e: + if not settings.DEBUG: + logger.error( + string_concat( + _('Ocorreu erro na criação tipo de ta: '), + str(e))) + + +def init_compilacao_base(app_config, verbosity=2, interactive=True, + using=DEFAULT_DB_ALIAS, **kwargs): + + if app_config != AppConfig and not isinstance(app_config, AppConfig): + return + from sapl.compilacao.models import TipoDispositivo + if not TipoDispositivo.objects.exists(): + + print('') + print(string_concat('\033[93m\033[1m', + _('Iniciando Textos Articulados...'), + '\033[0m')) + AppConfig.import_pattern() + + +models.signals.post_migrate.connect( + receiver=init_compilacao_base) diff --git a/sapl/compilacao/views.py b/sapl/compilacao/views.py index 523de19a2..b24bfd677 100644 --- a/sapl/compilacao/views.py +++ b/sapl/compilacao/views.py @@ -27,6 +27,7 @@ from django.views.generic.edit import (CreateView, DeleteView, FormView, UpdateView) from django.views.generic.list import ListView +from sapl.compilacao.apps import AppConfig from sapl.compilacao.forms import (DispositivoDefinidorVigenciaForm, DispositivoEdicaoAlteracaoForm, DispositivoEdicaoBasicaForm, @@ -107,7 +108,7 @@ class IntegracaoTaView(TemplateView): try: if settings.DEBUG or not TipoDispositivo.objects.exists(): - self.import_pattern() + AppConfig.import_pattern() if hasattr(self, 'map_funcs'): tipo_ta = TipoTextoArticulado.objects.get( @@ -195,66 +196,6 @@ class IntegracaoTaView(TemplateView): return redirect(to=reverse_lazy('sapl.compilacao:ta_text', kwargs={'ta_id': ta.pk})) - def import_pattern(self): - - from unipath import Path - - compilacao_app = Path(__file__).ancestor(1) - # print(compilacao_app) - with open(compilacao_app + '/compilacao_data_tables.sql', 'r') as f: - lines = f.readlines() - lines = [line.rstrip('\n') for line in lines] - - with connection.cursor() as cursor: - for line in lines: - line = line.strip() - if not line or line.startswith('#'): - continue - - try: - cursor.execute(line) - except IntegrityError as e: - if not settings.DEBUG: - logger.error( - string_concat( - _('Ocorreu erro na importação: '), - line, - str(e))) - except Exception as ee: - print(ee) - - integrations_view_names = get_integrations_view_names() - - def cria_sigla(verbose_name): - verbose_name = verbose_name.upper().split() - if len(verbose_name) == 1: - verbose_name = verbose_name[0] - sigla = '' - for letra in verbose_name: - if letra in 'BCDFGHJKLMNPQRSTVWXYZ': - sigla += letra - else: - sigla = ''.join([palavra[0] for palavra in verbose_name]) - return sigla[:3] - - for view in integrations_view_names: - try: - tipo = TipoTextoArticulado() - tipo.sigla = cria_sigla( - view.model._meta.verbose_name - if view.model._meta.verbose_name - else view.model._meta.model_name) - tipo.descricao = view.model._meta.verbose_name - tipo.content_type = ContentType.objects.get_by_natural_key( - view.model._meta.app_label, view.model._meta.model_name) - tipo.save() - except IntegrityError as e: - if not settings.DEBUG: - logger.error( - string_concat( - _('Ocorreu erro na criação tipo de ta: '), - str(e))) - class Meta: abstract = True