From 44b27e3ab3e8cd022f9ab11a5e3214db607c2dd5 Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Tue, 25 Feb 2020 14:17:59 +0100 Subject: [PATCH] flush before import and nice output --- management/commands/import_nk15.py | 31 +++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/management/commands/import_nk15.py b/management/commands/import_nk15.py index 4b8d57a..10218c2 100644 --- a/management/commands/import_nk15.py +++ b/management/commands/import_nk15.py @@ -1,6 +1,7 @@ #!/usr/env/bin python3 from django.core.management.base import BaseCommand +from django.core.management import call_command from django.utils import timezone import psycopg2 as pg import psycopg2.extras as pge @@ -187,30 +188,38 @@ class Command(BaseCommand): Command for importing the database of NK15. Need to be run by a user with a registered role in postgres for the database nk15. """ + def print_success(self,to_print): + return self.stdout.write(self.style.SUCCESS(to_print)) + def add_arguments(self,parser): - parser.add_argument('-s', '--special', action = 'store_true') - parser.add_argument('-c', '--comptes', action = 'store_true') - parser.add_argument('-b', '--boutons', action = 'store_true') - parser.add_argument('-t', '--transactions', action = 'store_true') - parser.add_argument('-a', '--aliases', action = 'store_true') - + parser.add_argument('-s', '--special', action = 'store_true', help="create Minimum instance (special note and clubs)") + parser.add_argument('-c', '--comptes', action = 'store_true', help="import accounts") + parser.add_argument('-b', '--boutons', action = 'store_true', help="import boutons") + parser.add_argument('-t', '--transactions', action = 'store_true',help="import transaction") + parser.add_argument('-a', '--aliases', action = 'store_true',help="import aliases") + def handle(self, *args, **kwargs): + #reset database. + call_command("flush") + self.print_success("flush nk20 database") + # connecting to nk15 database conn = pg.connect(database="nk15",user="nk15_user") cur = conn.cursor(cursor_factory = pge.DictCursor) if kwargs["special"]: map_idbde = import_special(cur) - print("Minimal setup created") + self.print_success("Minimal setup created") if kwargs["comptes"]: map_idbde = import_comptes(cur,map_idbde) - print("comptes table imported") + self.print_success("comptes table imported") if kwargs["boutons"]: import_boutons(cur,map_idbde) - print("boutons table imported") + self.print_success("boutons table imported") if kwargs["transactions"]: - import_transaction(cur) + import_transaction(cur,map_idbde) + self.print_success("transaction imported") if kwargs["aliases"]: import_aliases(cur,map_idbde) - print("aliases imported") + self.print_success("aliases imported")