From 1d2d104b8e4f2b7737d1c2933099664849248e05 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Mon, 11 Sep 2017 16:09:56 -0300 Subject: [PATCH 1/5] =?UTF-8?q?Restringe=20an=C3=A1lise=20de=20cobertura?= =?UTF-8?q?=20ao=20dir=20de=20fontes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index ac3d44b96..7baf22129 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,4 +1,5 @@ [run] +source = sapl omit = sapl/wsgi.py manage.py From 157c2271d6129b833018061a85448995882f83f6 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Mon, 11 Sep 2017 16:11:04 -0300 Subject: [PATCH 2/5] Simplifica salvar com id --- sapl/legacy/migration.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/sapl/legacy/migration.py b/sapl/legacy/migration.py index d45b9ee75..b03514366 100644 --- a/sapl/legacy/migration.py +++ b/sapl/legacy/migration.py @@ -165,20 +165,6 @@ def get_last_value(model): return last_value['pk__max'] or 0 -def alter_sequence(model, id): - sequence_name = '%s_id_seq' % model._meta.db_table - exec_sql('ALTER SEQUENCE %s RESTART WITH %s MINVALUE -1;' % ( - sequence_name, id)) - - -def save_with_id(new, id): - last_value = get_last_value(type(new)) - alter_sequence(type(new), id) - new.save() - alter_sequence(type(new), last_value + 1) - assert new.id == id, 'New id is different from provided!' - - def save_relation(obj, nome_campo='', problema='', descricao='', eh_stub=False, critico=False): link = ProblemaMigracao( @@ -364,7 +350,9 @@ class DataMigrator: else: def save(new, old): with reversion.create_revision(): - save_with_id(new, getattr(old, legacy_pk_name)) + # salva new com id de old + new.id = getattr(old, legacy_pk_name) + new.save() reversion.set_comment('Objeto criado pela migração') old_records = legacy_model.objects.all().order_by(legacy_pk_name) @@ -400,10 +388,6 @@ class DataMigrator: reversion.set_comment( 'Ajuste de data pela migração') - # necessário para ajustar sequence da tabela para o ultimo valor de id - ultimo_valor = get_last_value(model) - alter_sequence(model, ultimo_valor + 1) - def migrate(obj=appconfs, interativo=True): dm = DataMigrator() From 847a28721a7b1b86f49c590e3f7d21e44b54db62 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Mon, 11 Sep 2017 16:46:28 -0300 Subject: [PATCH 3/5] =?UTF-8?q?Criar=20usu=C3=A1rios=20padr=C3=A3o=20somen?= =?UTF-8?q?te=20de=20forma=20expl=C3=ADcita?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sapl/rules/apps.py | 50 +++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/sapl/rules/apps.py b/sapl/rules/apps.py index ab8c6322c..ad4ec1864 100644 --- a/sapl/rules/apps.py +++ b/sapl/rules/apps.py @@ -3,7 +3,6 @@ from builtins import LookupError import django import reversion from django.apps import apps -from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.management import _get_all_permissions from django.core import exceptions @@ -116,7 +115,8 @@ def create_proxy_permissions( def update_groups(app_config, verbosity=2, interactive=True, - using=DEFAULT_DB_ALIAS, **kwargs): + using=DEFAULT_DB_ALIAS, cria_usuarios_padrao=False, + **kwargs): if app_config != AppConfig and not isinstance(app_config, AppConfig): return @@ -149,41 +149,16 @@ def update_groups(app_config, verbosity=2, interactive=True, if not group_name: return - g = Group.objects.get_or_create(name=group_name) - if not isinstance(g, Group): - g = g[0] - g.permissions.clear() + group, created = Group.objects.get_or_create(name=group_name) + group.permissions.clear() try: - print(' ', group_name) for model, perms in rules_list: - self.associar(g, model, perms) + self.associar(group, model, perms) except Exception as e: print(group_name, e) - if settings.DEBUG: - user = '' - if group_name == SAPL_GROUP_ADMINISTRATIVO: - user = 'operador_administrativo' - elif group_name == SAPL_GROUP_PROTOCOLO: - user = 'operador_protocoloadm' - elif group_name == SAPL_GROUP_COMISSOES: - user = 'operador_comissoes' - elif group_name == SAPL_GROUP_MATERIA: - user = 'operador_materia' - elif group_name == SAPL_GROUP_NORMA: - user = 'operador_norma' - elif group_name == SAPL_GROUP_SESSAO: - user = 'operador_sessao' - elif group_name == SAPL_GROUP_PAINEL: - user = 'operador_painel' - elif group_name == SAPL_GROUP_GERAL: - user = 'operador_geral' - - if user: - self.cria_usuario(user, g) - def groups_add_user(self, user, groups_name): if not isinstance(groups_name, list): groups_name = [groups_name, ] @@ -213,6 +188,19 @@ def update_groups(app_config, verbosity=2, interactive=True, usuario.save() grupo.user_set.add(usuario) + def cria_usuarios_padrao(self): + for group, user in ( + (SAPL_GROUP_ADMINISTRATIVO, 'operador_administrativo'), + (SAPL_GROUP_PROTOCOLO, 'operador_protocoloadm'), + (SAPL_GROUP_COMISSOES, 'operador_comissoes'), + (SAPL_GROUP_MATERIA, 'operador_materia'), + (SAPL_GROUP_NORMA, 'operador_norma'), + (SAPL_GROUP_SESSAO, 'operador_sessao'), + (SAPL_GROUP_PAINEL, 'operador_painel'), + (SAPL_GROUP_GERAL, 'operador_geral'), + ): + self.cria_usuario(user, group) + def update_groups(self): print('') print(string_concat('\033[93m\033[1m', @@ -225,6 +213,8 @@ def update_groups(app_config, verbosity=2, interactive=True, rules = Rules(rules_patterns) rules.update_groups() + if cria_usuarios_padrao: + rules.cria_usuarios_padrao() def revision_pre_delete_signal(sender, **kwargs): From 057acefa79abbec7f5666fa4933bdf576672fc3a Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Mon, 11 Sep 2017 16:50:21 -0300 Subject: [PATCH 4/5] Roda migrate junto com comando migracao_25_31 --- sapl/legacy/management/commands/migracao_25_31.py | 2 ++ sapl/legacy/scripts/migra_um_db.sh | 5 ----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/sapl/legacy/management/commands/migracao_25_31.py b/sapl/legacy/management/commands/migracao_25_31.py index bddb26a8c..c0e2fa352 100644 --- a/sapl/legacy/management/commands/migracao_25_31.py +++ b/sapl/legacy/management/commands/migracao_25_31.py @@ -1,3 +1,4 @@ +from django.core import management from django.core.management.base import BaseCommand from sapl.legacy import migration @@ -17,4 +18,5 @@ class Command(BaseCommand): ) def handle(self, *args, **options): + management.call_command('migrate') migration.migrate(interativo=not options['force']) diff --git a/sapl/legacy/scripts/migra_um_db.sh b/sapl/legacy/scripts/migra_um_db.sh index 26240d8d4..18f233414 100755 --- a/sapl/legacy/scripts/migra_um_db.sh +++ b/sapl/legacy/scripts/migra_um_db.sh @@ -14,11 +14,6 @@ echo "########################################" | tee -a $LOG echo >> $LOG -echo "--- DJANGO MIGRATE ---" | tee -a $LOG -echo >> $LOG -DATABASE_NAME=$1 ./manage.py migrate --settings sapl.legacy_migration_settings -echo >> $LOG - echo "--- MIGRACAO DE DADOS ---" | tee -a $LOG echo >> $LOG DATABASE_NAME=$1 ./manage.py migracao_25_31 -f --settings sapl.legacy_migration_settings |& tee -a $LOG From 96c61dd5cdce051252e87d93071f62be78475f0c Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Mon, 11 Sep 2017 17:16:05 -0300 Subject: [PATCH 5/5] Configura fonte de destino da migracao em .env --- sapl/legacy_migration_settings.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sapl/legacy_migration_settings.py b/sapl/legacy_migration_settings.py index 2501a44dc..221ea9d48 100644 --- a/sapl/legacy_migration_settings.py +++ b/sapl/legacy_migration_settings.py @@ -12,14 +12,18 @@ INSTALLED_APPS += ( 'sapl.legacy', # legacy reversed model definitions ) -DATABASES['legacy'] = config('DATABASE_URL', cast=db_url,) +DATABASES['legacy'] = config('DATABASE_URL_FONTE', cast=db_url,) +DATABASES['default'] = config('DATABASE_URL_DESTINO', cast=db_url, + default=DATABASES['default']) # Sobrescreve o nome dos bancos caso a variável de ambiente seja definida # Útil para migração em lote de vários bancos DATABASE_NAME_OVERRIDE = os.environ.get('DATABASE_NAME') if DATABASE_NAME_OVERRIDE: - for db in DATABASES.values(): - db['NAME'] = DATABASE_NAME_OVERRIDE + DATABASES['legacy']['NAME'] = DATABASE_NAME_OVERRIDE + # não altera o nome se o destino é um banco em memória + if not DATABASES['default']['NAME'] == ':memory:': + DATABASES['default']['NAME'] = DATABASE_NAME_OVERRIDE DATABASE_ROUTERS = ['sapl.legacy.router.LegacyRouter', ]