From a17327bf837229c94e0ba103625edf6c765abb2c Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Tue, 9 Mar 2021 17:28:49 +0100 Subject: [PATCH] New db feeding commands --- app/cli/__init__.py | 48 ------------------------------------------------ app/commands/__init__.py | 5 +++++ app/commands/commands.py | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pdc_web.py | 2 +- requirements.txt | 1 + 5 files changed, 131 insertions(+), 49 deletions(-) delete mode 100644 app/cli/__init__.py create mode 100644 app/commands/__init__.py create mode 100644 app/commands/commands.py diff --git a/app/cli/__init__.py b/app/cli/__init__.py deleted file mode 100644 index 7a6c41b..0000000 --- a/app/cli/__init__.py +++ /dev/null @@ -1,48 +0,0 @@ -import click -from flask import Blueprint -from app.models import db, User - -bp = Blueprint('pdc_db', __name__) - - -@bp.cli.command('delete_user') -@click.argument('user_id') -def delete(user_id): - user = User.query.get(user_id) - db.session.delete(user) - db.session.commit() - - -@bp.cli.command('create_db') -def create(): - db.create_all() - admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin') - db.session.add(admin) - db.session.commit() - - -@bp.cli.command('add_user') -@click.argument('email') -@click.argument('name') -@click.argument('password') -def add(email, name, password): - user = User(email=email, name=name, password=password) - db.session.add(user) - db.session.commit() - print("added ", name) - - -@bp.cli.command('show_all') -def show_all(): - print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('id', 'name', 'login', 'passwd', 'email')) - print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15, '-' * 15)) - for user in User.query.all(): - print(user.login) - print("{:<5} {:<15} {:<15} {:<15} {:<15}".format( - user.id, - user.name, - user.login, - user.password, - user.email - )) - diff --git a/app/commands/__init__.py b/app/commands/__init__.py new file mode 100644 index 0000000..153a30c --- /dev/null +++ b/app/commands/__init__.py @@ -0,0 +1,5 @@ +from flask import Blueprint + +bp = Blueprint('pdc_db', __name__) + +from . import commands \ No newline at end of file diff --git a/app/commands/commands.py b/app/commands/commands.py new file mode 100644 index 0000000..16b82dd --- /dev/null +++ b/app/commands/commands.py @@ -0,0 +1,124 @@ +import click +import random + +from sqlalchemy.ext.automap import automap_base +from sqlalchemy.orm import Session +from sqlalchemy import create_engine + +from app.models import db, User, Agent, Service, Project, Capacity, Period, Charge + +from db_config import mysql_uri + +from . import bp + +@bp.cli.command("feed_from_lesia") +def feed_from_lesia(): + Base = automap_base() + + engine = create_engine(mysql_uri) + + # reflect the tables + Base.prepare(engine, reflect=True) + + # mapped classes are now created with names by default + # matching that of the table name. + LesiaAgent = Base.classes.agent + LesiaService = Base.classes.gestit_services + LesiaProject = Base.classes.gestit_projets + LesiaFonction = Base.classes.gestit_fonctions + + lesia_session = Session(engine) + agents = lesia_session.query(LesiaAgent).all() + for a in agents: + n_a = Agent(firstname=a.nom, secondname=a.prenom) + db.session.add(n_a) + db.session.commit() + + services = lesia_session.query(LesiaService).all() + for s in services: + n_s = Service(name=s.nom) + db.session.add(n_s) + db.session.commit() + + projects = lesia_session.query(LesiaProject).all() + for p in projects: + n_p = Project(name=p.nom) + db.session.add(n_p) + db.session.commit() + + fonctions = lesia_session.query(LesiaFonction).all() + for f in fonctions: + n_c = Capacity(name=f.nom) + db.session.add(n_c) + db.session.commit() + +@bp.cli.command("feed_periods") +def feed_periods(): + for y in ['2019', '2020', '2021']: + for s in ['S1', 'S2']: + period_name = "{}_{}".format(y, s) + p = Period(name=period_name) + db.session.add(p) + db.session.commit() + + +@bp.cli.command("random_charges") +def random_charges(): + for i in range(0, 100): + agent_id = random.choice([i for (i,) in db.session.query(Agent.id).all() ]) + project_id = random.choice([i for (i,) in db.session.query(Project.id).all()]) + service_id = random.choice([i for (i,) in db.session.query(Service.id).all()]) + capacity_id = random.choice([i for (i,) in db.session.query(Capacity.id).all()]) + period_id = random.choice([i for (i,) in db.session.query(Period.id).all()]) + percent = random.choice(range(10, 110, 10)) + charge = Charge(agent_id=agent_id , + project_id=project_id, + service_id=service_id, + capacity_id=capacity_id, + period_id=period_id, + charge_rate=percent) + print("adding {}_{}_{}_{}_{}_{}".format(agent_id, project_id, service_id, capacity_id, period_id, percent)) + db.session.add(charge) + db.session.commit() + + +@bp.cli.command('delete_user') +@click.argument('user_id') +def delete_user(user_id): + user = User.query.get(user_id) + db.session.delete(user) + db.session.commit() + + +@bp.cli.command('create_db') +def create_db(): + db.create_all() + admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin') + db.session.add(admin) + db.session.commit() + + +@bp.cli.command('add_user') +@click.argument('email') +@click.argument('name') +@click.argument('password') +def add_user(email, name, password): + user = User(email=email, name=name, password=password) + db.session.add(user) + db.session.commit() + print("added ", name) + + +@bp.cli.command('show_all') +def show_all(): + print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('id', 'name', 'login', 'passwd', 'email')) + print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15, '-' * 15)) + for user in User.query.all(): + print(user.login) + print("{:<5} {:<15} {:<15} {:<15} {:<15}".format( + user.id, + user.name, + user.login, + user.password, + user.email + )) diff --git a/pdc_web.py b/pdc_web.py index 2706170..6f08276 100644 --- a/pdc_web.py +++ b/pdc_web.py @@ -17,7 +17,7 @@ pdc_app = create_app() # used with 'flask pdc_db' command # see app/cli/__init__.py for more details. # -from app.cli import bp as cli_bp +from app.commands import bp as cli_bp pdc_app.register_blueprint(cli_bp) diff --git a/requirements.txt b/requirements.txt index cfce53b..419c3c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ wheel Flask-Login Flask-SQLAlchemy Flask-Migrate +PyMysql -- libgit2 0.21.2