From 946dda3099666a1031f975962c72bd8726443736 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Thu, 15 Apr 2021 19:11:53 +0200 Subject: [PATCH] New cli command user_update() --- app/commands/commands.py | 203 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------- 1 file changed, 116 insertions(+), 87 deletions(-) diff --git a/app/commands/commands.py b/app/commands/commands.py index c185743..02130f0 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -13,11 +13,35 @@ from sqlalchemy.orm import Session from sqlalchemy import create_engine from app.models import db, Agent, Service, Project, Capacity, Period, Charge -from app.auth.models import User, _nameToRole +from app.auth.models import User, _nameToRole, _roleToName from . import bp +@bp.cli.command('create_db') +def create_db(): + """ + Create the database structure. Database should be empty. + + configure the proper database uri in the db_config.py file. + """ + db.create_all() + admin = User(email='admin@nowhere.org', name='admin', login='admin', role='admin') + admin.set_password('admin') + sqlite_uri = db.engine.url.__str__() if 'sqlite' in db.engine.url.__str__() else None + try: + db.session.add(admin) + db.session.commit() + except IntegrityError: + current_app.logger.error("User admin already exists, database should be empty at create") + if sqlite_uri: + current_app.logger.error("see " + sqlite_uri) + sys.exit(-1) + + if sqlite_uri: + current_app.logger.info("Created sqlite db: " + sqlite_uri) + + @bp.cli.command("feed_from_irap") @click.option('--csv-file', '-f', 'csv_file_name', help="the csv file path to feed from") def feed_from_irap(csv_file_name): @@ -123,6 +147,59 @@ def feed_from_irap(csv_file_name): db.session.commit() +@bp.cli.command("feed_from_lesia") +def feed_from_lesia(): + """ + Feed db with agents from a lesia like mysql database. + + Remember to configure the proper database uri in the db_config.py file. + """ + from .lesia_db import lesia_agent, lesia_session, lesia_service, lesia_project, \ + lesia_fonction, lesia_periods, lesia_affectation + + agents = lesia_session.query(lesia_agent).all() + for a in agents: + n_a = Agent(id=a.IDagent, firstname=a.nom, secondname=a.prenom) + db.session.add(n_a) + db.session.commit() + + services = lesia_session.query(lesia_service).all() + for s in services: + n_s = Service(id=s.id, name=s.nom, abbr=s.abbreviation) + db.session.add(n_s) + db.session.commit() + + projects = lesia_session.query(lesia_project).all() + for p in projects: + n_p = Project(id=p.id, name=p.nom) + db.session.add(n_p) + db.session.commit() + + fonctions = lesia_session.query(lesia_fonction).all() + for f in fonctions: + n_c = Capacity(id=f.id, name=f.nom) + db.session.add(n_c) + db.session.commit() + + periods = lesia_session.query(lesia_periods) + for p in periods: + n_p = Period(name=p.id_semestre) + db.session.add(n_p) + db.session.commit() + + affectations = lesia_session.query(lesia_affectation) + for f in affectations: + p = Period.query.filter(Period.name == f.semestre_id).one() + n_c = Charge(agent_id=f.agent_id, + project_id=f.projet_id, + service_id=f.service_id, + capacity_id=f.fonction_id, + period_id=p.id, + charge_rate=f.charge) + db.session.add(n_c) + db.session.commit() + + @bp.cli.command("fake_lesia_names") def fake_lesia_names(): """ @@ -186,59 +263,6 @@ def fake_lesia_names(): db.session.commit() -@bp.cli.command("feed_from_lesia") -def feed_from_lesia(): - """ - Feed db with agents from a lesia like mysql database. - - Remember to configure the proper database uri in the db_config.py file. - """ - from .lesia_db import lesia_agent, lesia_session, lesia_service, lesia_project, \ - lesia_fonction, lesia_periods, lesia_affectation - - agents = lesia_session.query(lesia_agent).all() - for a in agents: - n_a = Agent(id=a.IDagent, firstname=a.nom, secondname=a.prenom) - db.session.add(n_a) - db.session.commit() - - services = lesia_session.query(lesia_service).all() - for s in services: - n_s = Service(id=s.id, name=s.nom, abbr=s.abbreviation) - db.session.add(n_s) - db.session.commit() - - projects = lesia_session.query(lesia_project).all() - for p in projects: - n_p = Project(id=p.id, name=p.nom) - db.session.add(n_p) - db.session.commit() - - fonctions = lesia_session.query(lesia_fonction).all() - for f in fonctions: - n_c = Capacity(id=f.id, name=f.nom) - db.session.add(n_c) - db.session.commit() - - periods = lesia_session.query(lesia_periods) - for p in periods: - n_p = Period(name=p.id_semestre) - db.session.add(n_p) - db.session.commit() - - affectations = lesia_session.query(lesia_affectation) - for f in affectations: - p = Period.query.filter(Period.name == f.semestre_id).one() - n_c = Charge(agent_id=f.agent_id, - project_id=f.projet_id, - service_id=f.service_id, - capacity_id=f.fonction_id, - period_id=p.id, - charge_rate=f.charge) - db.session.add(n_c) - db.session.commit() - - @bp.cli.command("feed_periods") @click.option('--begin-year', '-b', 'begin_year', default=2005, help="the start year to begin periods with") @click.option('--end-year', '-e', 'end_year', default=2025, help="the last year to end periods with") @@ -288,39 +312,6 @@ def feed_random_charges(agent): db.session.commit() -@bp.cli.command('user_delete') -@click.argument('user_id') -def user_delete(user_id): - """Delete the user by given id (see user_show_all").""" - user = User.query.get(user_id) - db.session.delete(user) - db.session.commit() - - -@bp.cli.command('create_db') -def create_db(): - """ - Create the database structure. Database should be empty. - - configure the proper database uri in the db_config.py file. - """ - db.create_all() - admin = User(email='admin@nowhere.org', name='admin', login='admin', role='admin') - admin.set_password('admin') - sqlite_uri = db.engine.url.__str__() if 'sqlite' in db.engine.url.__str__() else None - try: - db.session.add(admin) - db.session.commit() - except IntegrityError: - current_app.logger.error("User admin already exists, database should be empty at create") - if sqlite_uri: - current_app.logger.error("see " + sqlite_uri) - sys.exit(-1) - - if sqlite_uri: - current_app.logger.info("Created sqlite db: " + sqlite_uri) - - @bp.cli.command('user_add') @click.argument('email') @click.argument('name') @@ -339,6 +330,44 @@ def user_add(email, name, login, password, role): current_app.logger.info(f"added {name}") +@bp.cli.command('user_update') +@click.option('--name', '-n', 'name', default=None, help="the name to set for that user") +@click.option('--role', '-r', 'role', default=None, help="the role to set for that user") +@click.option('--email', '-e', 'email', default=None, help="the email to set for that user") +@click.option('--password', '-p', 'password', default=None, help="the password to set for that user") +@click.argument('user_id') +def user_update(user_id, name, role, email, password): + """Update the user by given id and given parameters.""" + user = User.query.get(user_id) + if not user: + current_app.logger.error(f"such user_id doesnt exists {user_id}") + return + if name: + user.name = name + print(f"User --{user.name}-- name updated to {user.name}") + if role: + user.set_role(role) + print(f"User --{user.name}-- role updated to {_roleToName[user.role]}") + if email: + user.email=email + print(f"User --{user.name}-- email updated to {user.email}") + if password: + print(f"User --{user.name}-- password updated") + user.set_password(password) + if not ( name or role or email or password): + print(f"No update for user --{user.name}--") + db.session.commit() + + +@bp.cli.command('user_delete') +@click.argument('user_id') +def user_delete(user_id): + """Delete the user by given id (see user_show_all").""" + user = User.query.get(user_id) + db.session.delete(user) + db.session.commit() + + @bp.cli.command('show_roles') def show_roles(): """ List all available roles for a user""" -- libgit2 0.21.2