cic-stack/apps/data-seeding/cic_ussd/import_ussd_data.py

68 lines
2.2 KiB
Python
Raw Normal View History

2021-05-15 09:40:34 +02:00
# standard imports
import argparse
2021-08-29 11:55:47 +02:00
import csv
2021-05-15 09:40:34 +02:00
import logging
import os
2021-08-29 11:55:47 +02:00
import psycopg2
2021-05-15 09:40:34 +02:00
# external imports
from confini import Config
# local imports
2021-08-29 11:55:47 +02:00
default_config_dir = './config'
2021-05-15 09:40:34 +02:00
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
2021-08-29 11:55:47 +02:00
arg_parser = argparse.ArgumentParser(description='Pins import script.')
arg_parser.add_argument('-c', type=str, default=default_config_dir, help='config root to use.')
arg_parser.add_argument('--env-prefix',
default=os.environ.get('CONFINI_ENV_PREFIX'),
dest='env_prefix',
type=str,
help='environment prefix for variables to overwrite configuration.')
arg_parser.add_argument('import_dir', default='out', type=str, help='user export directory')
arg_parser.add_argument('-v', help='be verbose', action='store_true')
arg_parser.add_argument('-vv', help='be more verbose', action='store_true')
2021-05-15 09:40:34 +02:00
args = arg_parser.parse_args()
2021-08-29 11:55:47 +02:00
if args.vv:
logging.getLogger().setLevel(logging.DEBUG)
elif args.v:
logging.getLogger().setLevel(logging.INFO)
2021-05-15 09:40:34 +02:00
2021-08-29 11:55:47 +02:00
config = Config(args.c, args.env_prefix)
2021-05-15 09:40:34 +02:00
config.process()
2021-08-29 11:55:47 +02:00
config.censor('PASSWORD', 'DATABASE')
logg.debug(f'config loaded from {args.c}:\n{config}')
2021-05-15 09:40:34 +02:00
2021-08-29 11:55:47 +02:00
def main():
with open(f'{args.import_dir}/ussd_data.csv') as ussd_data_file:
ussd_data = [tuple(row) for row in csv.reader(ussd_data_file)]
2021-05-15 09:40:34 +02:00
2021-08-29 11:55:47 +02:00
db_conn = psycopg2.connect(
database=config.get('DATABASE_NAME'),
host=config.get('DATABASE_HOST'),
port=config.get('DATABASE_PORT'),
user=config.get('DATABASE_USER'),
password=config.get('DATABASE_PASSWORD')
)
db_cursor = db_conn.cursor()
sql = 'UPDATE account SET status = %s, preferred_language = %s WHERE phone_number = %s'
for element in ussd_data:
status = 2 if int(element[1]) == 1 else 1
preferred_language = element[2]
phone_number = element[0]
db_cursor.execute(sql, (status, preferred_language, phone_number))
logg.debug(f'Updating account:{phone_number} with: preferred language: {preferred_language} status: {status}.')
db_conn.commit()
db_cursor.close()
db_conn.close()
2021-05-15 09:40:34 +02:00
2021-08-29 11:55:47 +02:00
if __name__ == '__main__':
main()