From 5f1c7c8b4ab0dfbe277dcf940648eb68e1e14a0d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 15 Jun 2023 17:56:34 +0200 Subject: [PATCH] add command adduser --- ereuse_devicehub/commands/adduser.py | 24 ++++++++++++++++++++++++ ereuse_devicehub/commands/initdatas.py | 1 - ereuse_devicehub/devicehub.py | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 ereuse_devicehub/commands/adduser.py diff --git a/ereuse_devicehub/commands/adduser.py b/ereuse_devicehub/commands/adduser.py new file mode 100644 index 00000000..21c4b25d --- /dev/null +++ b/ereuse_devicehub/commands/adduser.py @@ -0,0 +1,24 @@ +import click + +from ereuse_devicehub.db import db +from ereuse_devicehub.resources.agent.models import Person +from ereuse_devicehub.resources.user.models import User + + +class AddUser: + def __init__(self, app) -> None: + super().__init__() + self.app = app + self.schema = app.config.get('DB_SCHEMA') + self.app.cli.command('adduser', short_help='add a user.')(self.run) + + @click.argument('email') + @click.argument('password') + def run(self, email, password): + name = email.split('@')[0] + + user = User(email=email, password=password) + user.individuals.add(Person(name=name)) + db.session.add(user) + + db.session.commit() diff --git a/ereuse_devicehub/commands/initdatas.py b/ereuse_devicehub/commands/initdatas.py index bf76cd9c..4e199fa4 100644 --- a/ereuse_devicehub/commands/initdatas.py +++ b/ereuse_devicehub/commands/initdatas.py @@ -1,6 +1,5 @@ from uuid import uuid4 -import click from boltons.urlutils import URL from ereuse_devicehub.db import db diff --git a/ereuse_devicehub/devicehub.py b/ereuse_devicehub/devicehub.py index dbf93a2d..a6c6b7c9 100644 --- a/ereuse_devicehub/devicehub.py +++ b/ereuse_devicehub/devicehub.py @@ -12,6 +12,7 @@ from flask_sqlalchemy import SQLAlchemy import ereuse_devicehub.ereuse_utils.cli from ereuse_devicehub.auth import Auth from ereuse_devicehub.client import Client, UserClient +from ereuse_devicehub.commands.adduser import AddUser from ereuse_devicehub.commands.initdatas import InitDatas # from ereuse_devicehub.commands.reports import Report @@ -75,6 +76,7 @@ class Devicehub(Teal): # self.report = Report(self) self.get_token = GetToken(self) self.initdata = InitDatas(self) + self.adduser = AddUser(self) @self.cli.group( short_help='Inventory management.',