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 new file mode 100644 index 00000000..4e199fa4 --- /dev/null +++ b/ereuse_devicehub/commands/initdatas.py @@ -0,0 +1,40 @@ +from uuid import uuid4 + +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..a6c6b7c9 100644 --- a/ereuse_devicehub/devicehub.py +++ b/ereuse_devicehub/devicehub.py @@ -5,20 +5,22 @@ 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.adduser import AddUser +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 +75,8 @@ class Devicehub(Teal): self.dummy = Dummy(self) # self.report = Report(self) self.get_token = GetToken(self) + self.initdata = InitDatas(self) + self.adduser = AddUser(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( diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 593ce4e3..1d9c1e79 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) 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'