From fad008b25d0ab8d2826ffe620aa629d77b2b230d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 15 Jun 2023 16:41:47 +0200 Subject: [PATCH 1/4] command initdatas --- ereuse_devicehub/commands/initdatas.py | 41 +++++++++++++++++++ ereuse_devicehub/devicehub.py | 6 ++- .../versions/fbb7e2a0cde0_initial.py | 8 ++-- 3 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 ereuse_devicehub/commands/initdatas.py diff --git a/ereuse_devicehub/commands/initdatas.py b/ereuse_devicehub/commands/initdatas.py new file mode 100644 index 00000000..bf76cd9c --- /dev/null +++ b/ereuse_devicehub/commands/initdatas.py @@ -0,0 +1,41 @@ +from uuid import uuid4 + +import click +from boltons.urlutils import URL + +from ereuse_devicehub.db import db +from ereuse_devicehub.resources.agent.models import Person +from ereuse_devicehub.resources.inventory.model import Inventory +from ereuse_devicehub.resources.user.models import User + + +class InitDatas: + def __init__(self, app) -> None: + super().__init__() + self.app = app + self.schema = app.config.get('DB_SCHEMA') + self.app.cli.command( + 'initdata', short_help='Save a minimum structure of datas.' + )(self.run) + + def run(self): + inv = Inventory( + id=self.schema, + name="usody", + tag_provider=URL('http://localhost:8081'), + tag_token=uuid4(), + org_id=uuid4(), + ) + + db.session.add(inv) + db.session.commit() + + email = 'user@dhub.com' + password = '1234' + name = 'user' + + user = User(email=email, password=password) + user.individuals.add(Person(name=name)) + db.session.add(user) + + db.session.commit() diff --git a/ereuse_devicehub/devicehub.py b/ereuse_devicehub/devicehub.py index e5e76145..dbf93a2d 100644 --- a/ereuse_devicehub/devicehub.py +++ b/ereuse_devicehub/devicehub.py @@ -5,20 +5,21 @@ from typing import Type import boltons.urlutils import click import click_spinner -import ereuse_devicehub.ereuse_utils.cli -from ereuse_devicehub.ereuse_utils.session import DevicehubClient from flask import _app_ctx_stack, g from flask_login import LoginManager, current_user 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.initdatas import InitDatas # from ereuse_devicehub.commands.reports import Report from ereuse_devicehub.commands.users import GetToken from ereuse_devicehub.config import DevicehubConfig from ereuse_devicehub.db import db from ereuse_devicehub.dummy.dummy import Dummy +from ereuse_devicehub.ereuse_utils.session import DevicehubClient from ereuse_devicehub.resources.device.search import DeviceSearch from ereuse_devicehub.resources.inventory import Inventory, InventoryDef from ereuse_devicehub.resources.user.models import User @@ -73,6 +74,7 @@ class Devicehub(Teal): self.dummy = Dummy(self) # self.report = Report(self) self.get_token = GetToken(self) + self.initdata = InitDatas(self) @self.cli.group( short_help='Inventory management.', diff --git a/ereuse_devicehub/migrations/versions/fbb7e2a0cde0_initial.py b/ereuse_devicehub/migrations/versions/fbb7e2a0cde0_initial.py index 08758ec3..dc39795b 100644 --- a/ereuse_devicehub/migrations/versions/fbb7e2a0cde0_initial.py +++ b/ereuse_devicehub/migrations/versions/fbb7e2a0cde0_initial.py @@ -8,12 +8,11 @@ Create Date: 2020-05-07 10:04:40.269511 import citext import sqlalchemy as sa import sqlalchemy_utils -from ereuse_devicehub import teal -from alembic import context -from alembic import op +from alembic import context, op from sqlalchemy.dialects import postgresql -from ereuse_devicehub.resources.enums import TransferState, Severity +from ereuse_devicehub import teal +from ereuse_devicehub.resources.enums import Severity, TransferState # revision identifiers, used by Alembic. revision = 'fbb7e2a0cde0' @@ -348,6 +347,7 @@ def upgrade(): postgresql_using='hash', schema=f'{get_inv()}', ) + op.execute(f"CREATE SEQUENCE {get_inv()}.device_seq START 1;") op.create_table( 'agent', sa.Column( From 5f1c7c8b4ab0dfbe277dcf940648eb68e1e14a0d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 15 Jun 2023 17:56:34 +0200 Subject: [PATCH 2/4] 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.', From 57caf52c024e3ecc0ee7014ff46b8aede030bba5 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 16 Jun 2023 11:55:21 +0200 Subject: [PATCH 3/4] fix initial id --- ereuse_devicehub/resources/device/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index ca7ea9be..c7950d1d 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -76,8 +76,8 @@ logger = logging.getLogger(__name__) def create_code(context): - _id = Device.query.order_by(Device.id.desc()).first() or 3 - if not _id == 3: + _id = Device.query.order_by(Device.id.desc()).first() or 1 + if not _id == 1: _id = _id.id + 1 return hashcode.encode(_id) From 99f4c71ee134853f0e1fd7045e63f1383abb993c Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 16 Jun 2023 13:04:37 +0200 Subject: [PATCH 4/4] fix test --- tests/files/devices_lots.csv | 2 +- tests/test_metrics.py | 6 +++--- tests/test_render_2_0.py | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/files/devices_lots.csv b/tests/files/devices_lots.csv index d9bd6680..abb9e9d5 100644 --- a/tests/files/devices_lots.csv +++ b/tests/files/devices_lots.csv @@ -1,2 +1,2 @@ "DHID";"Lot Id";"Lot Name";"Lot Type";"Transfer Status";"Transfer Code";"Transfer Date";"Transfer Creation Date";"Transfer Update Date" -"O48N2";"b33c5a0d-bc80-453f-805a-560fab88a761";"lot1";"Temporary";"";"";"";"";"" +"93652";"b33c5a0d-bc80-453f-805a-560fab88a761";"lot1";"Temporary";"";"";"";"";"" diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 706e7260..e81d7af9 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -160,7 +160,7 @@ def test_metrics_action_status(user: UserClient, user2: UserClient): head += '"Status Supplier – Created Date";"Status Receiver – Created Date";"Trade-Weight";' head += '"Action-Create";"Allocate-Start";"Allocate-User-Code";"Allocate-NumUsers";' head += '"UsageTimeAllocate";"Type";"LiveCreate";"UsageTimeHdd"\n' - body = '"O48N2";"adebcc5506213fac43cd8473a9c81bcf0cadaed9cb98b2eae651e377a3533c5a";' + body = '"93652";"adebcc5506213fac43cd8473a9c81bcf0cadaed9cb98b2eae651e377a3533c5a";' body += '"";"Status";"";"foo@foo.com";"Receiver";"";"";"Use";"";"' assert head in csv_str assert body in csv_str @@ -207,7 +207,7 @@ def test_complet_metrics_with_trade(user: UserClient, user2: UserClient): query=[('filter', {'type': ['Computer'], 'ids': devices_id})], ) - body1_lenovo = '"O48N2";"adebcc5506213fac43cd8473a9c81bcf0cadaed9cb98b2eae651e377a3533c5a";"";"Trade";"foo@foo.com";' + body1_lenovo = '"93652";"adebcc5506213fac43cd8473a9c81bcf0cadaed9cb98b2eae651e377a3533c5a";"";"Trade";"foo@foo.com";' body1_lenovo += '"foo2@foo.com";"Supplier";"NeedConfirmation";"Use";"";' body2_lenovo = ';"";"0";"0";"Trade";"0";"0"\n' @@ -232,7 +232,7 @@ def test_complet_metrics_with_trade(user: UserClient, user2: UserClient): query=[('filter', {'type': ['Computer'], 'ids': devices_id})], ) - body1_lenovo = '"O48N2";"adebcc5506213fac43cd8473a9c81bcf0cadaed9cb98b2eae651e377a3533c5a";"";"Trade";"foo@foo.com";' + body1_lenovo = '"93652";"adebcc5506213fac43cd8473a9c81bcf0cadaed9cb98b2eae651e377a3533c5a";"";"Trade";"foo@foo.com";' body1_lenovo += '"foo2@foo.com";"Supplier";"NeedConfirmation";"Use";"Use";' body2_lenovo = ';"";"0";"0";"Trade";"0";"0"\n' body2_acer = ';"";"0";"0";"Trade";"0";"4692.0"\n' diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index 4e10a86d..2665d12a 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -531,7 +531,7 @@ def test_add_monitor(user3: UserClientFlask): assert dev.placeholder.id_device_supplier == "b2" assert dev.hid == 'monitor-samsung-lc27t55-aaaab' assert phid == '1' - assert dhid == 'O48N2' + assert dhid == '93652' txt = f'Device "{typ}" placeholder with PHID {phid} and DHID {dhid} ' txt += 'created successfully' @@ -571,7 +571,7 @@ def test_update_monitor(user3: UserClientFlask): assert dev.placeholder.id_device_supplier == "b2" assert dev.hid == 'monitor-samsung-lc27t55-aaaab' assert phid == '1' - assert dhid == 'O48N2' + assert dhid == '93652' assert dev.model == 'lc27t55' assert dev.depth == 0.1 assert dev.placeholder.pallet == "l34" @@ -640,7 +640,7 @@ def test_add_2_monitor(user3: UserClientFlask): assert dev.placeholder.id_device_supplier == "b1" assert dev.hid == 'monitor-samsung-lc27t55-aaaab' assert phid == '1' - assert dhid == 'O48N2' + assert dhid == '93652' assert dev.model == 'lc27t55' assert dev.placeholder.pallet == "l34" @@ -717,7 +717,7 @@ def test_add_laptop(user3: UserClientFlask): dev.chid == '274f05421e4d394c5b3cd10266fed6f0500029b104b5db3521689bda589e3150' ) assert phid == '1' - assert dhid == 'O48N2' + assert dhid == '93652' txt = f'Device "{typ}" placeholder with PHID {phid} and DHID {dhid} ' txt += 'created successfully' @@ -1891,7 +1891,7 @@ def test_edit_laptop(user3: UserClientFlask): assert dev.serial_number == 'aaaab' assert dev.model == 'lc27t55' assert phid == '1' - assert dhid == 'O48N2' + assert dhid == '93652' txt = f'Device "{typ}" placeholder with PHID {phid} and DHID {dhid} ' txt += 'created successfully'