From 8c979d774144f94ce78b4c94ab89b1eabe69059d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 1 Dec 2022 13:03:24 +0100 Subject: [PATCH] fix user register --- .../4b7f77f121bf_add_api_keys_dlt_to_user.py | 35 +++++++++++++++++++ ereuse_devicehub/resources/device/models.py | 8 +++-- ereuse_devicehub/resources/lot/models.py | 1 + ereuse_devicehub/resources/user/models.py | 6 +++- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 ereuse_devicehub/migrations/versions/4b7f77f121bf_add_api_keys_dlt_to_user.py diff --git a/ereuse_devicehub/migrations/versions/4b7f77f121bf_add_api_keys_dlt_to_user.py b/ereuse_devicehub/migrations/versions/4b7f77f121bf_add_api_keys_dlt_to_user.py new file mode 100644 index 00000000..fcbb456e --- /dev/null +++ b/ereuse_devicehub/migrations/versions/4b7f77f121bf_add_api_keys_dlt_to_user.py @@ -0,0 +1,35 @@ +"""add api_keys_dlt to user + +Revision ID: 4b7f77f121bf +Revises: af038a8a388c +Create Date: 2022-12-01 10:35:36.795035 + +""" +import citext +import sqlalchemy as sa +from alembic import context, op + +# revision identifiers, used by Alembic. +revision = '4b7f77f121bf' +down_revision = 'af038a8a388c' +branch_labels = None +depends_on = None + + +def get_inv(): + INV = context.get_x_argument(as_dictionary=True).get('inventory') + if not INV: + raise ValueError("Inventory value is not specified") + return INV + + +def upgrade(): + op.add_column( + 'user', + sa.Column('api_keys_dlt', type_=citext.CIText(), nullable=True), + schema='common', + ) + + +def downgrade(): + op.drop_column('user', 'api_keys_dlt', schema='common') diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index fe68d3ef..1c98377c 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -831,15 +831,19 @@ class Device(Thing): if 'trublo' not in app.blueprints.keys() or not self.hid: return + if not session.get('token_dlt'): + return + chid = hashlib.sha3_256(self.hid.encode('utf-8')).hexdigest() - token_dlt = session.get('token_dlt', ".").split(".")[1] + token_dlt = session.get('token_dlt').split(".")[1] api_dlt = app.config.get('API_DLT') if not token_dlt or not api_dlt: return api = API(api_dlt, token_dlt, "ethereum") - result = api.register_device(chid) + api.register_device(chid) + # result = api.register_device(chid) def __lt__(self, other): return self.id < other.id diff --git a/ereuse_devicehub/resources/lot/models.py b/ereuse_devicehub/resources/lot/models.py index 461e7c69..68eec828 100644 --- a/ereuse_devicehub/resources/lot/models.py +++ b/ereuse_devicehub/resources/lot/models.py @@ -51,6 +51,7 @@ class Lot(Thing): primaryjoin=lambda: Lot.id == LotParent.child_id, secondaryjoin=lambda: LotParent.parent_id == Lot.id, cascade='refresh-expire', # propagate changes outside ORM + sync_backref=False, backref=db.backref( 'children', viewonly=True, diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index 33c77043..e72ca075 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -105,11 +105,13 @@ class User(UserMixin, Thing): from modules.trublo.utils import encrypt + # import pdb; pdb.set_trace() api_dlt = app.config.get('API_DLT') data = register_user(api_dlt) + api_token = data.get('data', {}).get('api_token') data = json.dumps(data) self.api_keys_dlt = encrypt(password, data) - return data.get('data', {}).get('api_token') + return api_token def get_dlt_keys(self, password): if 'trublo' not in app.blueprints.keys(): @@ -117,6 +119,8 @@ class User(UserMixin, Thing): from modules.trublo.utils import decrypt + # import pdb; pdb.set_trace() + if not self.api_keys_dlt: return {}