cic-internal-integration/apps/data-seeding/cic_ussd/import_pins.py

65 lines
2.0 KiB
Python
Raw Permalink Normal View History

2021-08-29 11:55:47 +02:00
# standard imports
2021-05-15 09:40:34 +02:00
import argparse
import csv
import logging
import os
2021-08-29 11:55:47 +02:00
import psycopg2
2021-05-15 09:40:34 +02:00
2021-08-29 11:55:47 +02:00
# external imports
from confini import Config
2021-05-15 09:40:34 +02:00
# local imports
default_config_dir = './config'
2021-08-29 11:55:47 +02:00
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
2021-05-15 09:40:34 +02:00
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.')
2021-05-15 09:40:34 +02:00
arg_parser.add_argument('--env-prefix',
default=os.environ.get('CONFINI_ENV_PREFIX'),
dest='env_prefix',
type=str,
2021-08-29 11:55:47 +02:00
help='environment prefix for variables to overwrite configuration.')
arg_parser.add_argument('import_dir', default='out', type=str, help='user export directory')
2021-05-15 09:40:34 +02:00
arg_parser.add_argument('-v', help='be verbose', action='store_true')
arg_parser.add_argument('-vv', help='be more verbose', action='store_true')
2021-08-29 11:55:47 +02:00
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-07-03 18:55:51 +02:00
config.censor('PASSWORD', 'DATABASE')
2021-08-29 11:55:47 +02:00
logg.debug(f'config loaded from {args.c}:\n{config}')
2021-05-15 09:40:34 +02:00
def main():
2021-08-29 11:55:47 +02:00
with open(f'{args.import_dir}/pins.csv') as pins_file:
2021-05-15 09:40:34 +02:00
phone_to_pins = [tuple(row) for row in csv.reader(pins_file)]
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')
2021-05-15 09:40:34 +02:00
)
2021-08-29 11:55:47 +02:00
db_cursor = db_conn.cursor()
sql = 'UPDATE account SET password_hash = %s WHERE phone_number = %s'
for element in phone_to_pins:
db_cursor.execute(sql, (element[1], element[0]))
logg.debug(f'Updating account: {element[0]} with: {element[1]}')
db_conn.commit()
db_cursor.close()
db_conn.close()
2021-05-15 09:40:34 +02:00
if __name__ == '__main__':
main()