From 68c8aeed8c5e3880e383895182a0033b13a77114 Mon Sep 17 00:00:00 2001 From: Frank Bessou Date: Tue, 1 Oct 2024 15:44:20 +0200 Subject: [PATCH 1/2] chore: add .env directory to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index cd45b77..a5c3e27 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ __pycache__ .DS_Store +env venv db adele.sqlite From 00c0570be040b5ced5b5786f9361e61c88b965e7 Mon Sep 17 00:00:00 2001 From: Frank Bessou Date: Tue, 1 Oct 2024 15:44:50 +0200 Subject: [PATCH 2/2] feat: add a add-user command to manage cli --- app/cli.py | 103 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 26 deletions(-) diff --git a/app/cli.py b/app/cli.py index 8213574..4fb2d49 100644 --- a/app/cli.py +++ b/app/cli.py @@ -1,3 +1,4 @@ +import datetime import pprint from urllib.request import urlopen @@ -6,25 +7,27 @@ from app import create_app from app.api.routes import json_loads -from app.models import Image, ImageUrl +from app.models import Image, ImageUrl, Role, User app = None env = None + def add_default_users(db): # TODO db.session.flush() def make_cli(): - """ Creates a Command Line Interface for everydays tasks + """Creates a Command Line Interface for everydays tasks :return: Click groum """ + @click.group() - @click.option('--config', default="dev") + @click.option("--config", default="dev") def cli(config): - """ Generates the client""" + """Generates the client""" click.echo("Loading the application") global app global env @@ -33,10 +36,10 @@ def cli(config): @click.command("db-create") def db_create(): - """ Creates a local database - """ + """Creates a local database""" with app.app_context(): from app import db + db.create_all() add_default_users(db) @@ -46,11 +49,12 @@ def db_create(): @click.command("db-recreate") def db_recreate(): - """ Recreates a local database. You probably should not use this on + """Recreates a local database. You probably should not use this on production. """ with app.app_context(): from app import db + db.drop_all() db.create_all() @@ -61,8 +65,7 @@ def db_recreate(): @click.command("load-fixtures") def db_load_fixtures(): - """ Reload fixtures - """ + """Reload fixtures""" with app.app_context(): from app import db from tests.data.entities import load_fixtures @@ -73,8 +76,8 @@ def db_load_fixtures(): click.echo("Fixtures (re)loaded") @click.command("add-manifest") - @click.option('--manifest-url', required=True) - @click.option('--doc-id', required=True) + @click.option("--manifest-url", required=True) + @click.option("--doc-id", required=True) def db_add_manifest(manifest_url, doc_id): """ Fill the image & image_url tables with every image in the given manifest @@ -82,8 +85,8 @@ def db_add_manifest(manifest_url, doc_id): manifest_data = urlopen(manifest_url).read() data = json_loads(manifest_data) - if data['@context'] == "http://iiif.io/api/presentation/3/context.json": - print('IIIF Presentation v3 detected') + if data["@context"] == "http://iiif.io/api/presentation/3/context.json": + print("IIIF Presentation v3 detected") with app.app_context(): from app import db @@ -96,25 +99,25 @@ def db_add_manifest(manifest_url, doc_id): manifest_url=manifest_url, canvas_idx=canvas_idx, img_idx=img_idx, - doc_id=doc_id + doc_id=doc_id, ) new_image_url = ImageUrl( manifest_url=manifest_url, canvas_idx=canvas_idx, img_idx=img_idx, - img_url=image_url + img_url=image_url, ) db.session.add(new_image) db.session.add(new_image_url) db.session.flush() - print('Adding new image:', new_image.serialize()) - print('Adding new image_url:', new_image_url.serialize()) + print("Adding new image:", new_image.serialize()) + print("Adding new image_url:", new_image_url.serialize()) db.session.commit() - elif data['@context'] == "http://iiif.io/api/presentation/2/context.json": - print('IIIF Presentation v2 detected') + elif data["@context"] == "http://iiif.io/api/presentation/2/context.json": + print("IIIF Presentation v2 detected") with app.app_context(): from app import db @@ -126,38 +129,86 @@ def db_add_manifest(manifest_url, doc_id): manifest_url=manifest_url, canvas_idx=canvas_idx, img_idx=img_idx, - doc_id=doc_id + doc_id=doc_id, ) new_image_url = ImageUrl( manifest_url=manifest_url, canvas_idx=canvas_idx, img_idx=img_idx, - img_url=image_url + img_url=image_url, ) db.session.add(new_image) db.session.add(new_image_url) db.session.flush() - print('Adding new image:') + print("Adding new image:") pprint.pprint(new_image.serialize()) - print('Adding new image_url:') + print("Adding new image_url:") pprint.pprint(new_image_url.serialize()) db.session.commit() else: - print('@context not supported:', data['@context']) + print("@context not supported:", data["@context"]) return + @click.command("add-user") + @click.option("--email", required=True) + @click.option("--username", required=True) + @click.option("--password", required=True) + @click.option( + "--role", required=True, type=click.Choice(["admin", "teacher", "student"]) + ) + @click.option("--firstname", required=False) + @click.option("--lastname", required=False) + def db_add_user(email, username, password, role, firstname, lastname): + with app.app_context(): + from app import db + from werkzeug.security import generate_password_hash + + pwd_hash = generate_password_hash(password) + + roles = [] + + if role == "admin": + roles.append(Role.query.filter(Role.name == "admin").first()) + elif role == "teacher": + roles.append(Role.query.filter(Role.name == "teacher").first()) + elif role == "student": + roles.append(Role.query.filter(Role.name == "student").first()) + + default_name = email.split("@")[0] + + if firstname is None: + firstname = default_name + + if lastname is None: + lastname = default_name + + new_user = User( + username=username, + password=pwd_hash, + email=email, + email_confirmed_at=datetime.datetime.now(), + active=True, + first_name=firstname, + last_name=lastname, + roles=roles, + ) + + db.session.add(new_user) + db.session.commit() + print('User "%s" added' % username) + @click.command("run") def run(): - """ Run the application in Debug Mode [Not Recommended on production] - """ + """Run the application in Debug Mode [Not Recommended on production]""" app.run() cli.add_command(db_create) cli.add_command(db_recreate) cli.add_command(db_add_manifest) cli.add_command(db_load_fixtures) + cli.add_command(db_add_user) cli.add_command(run)