diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 1dc9cff2..b1503c6e 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -322,7 +322,7 @@ class NewDeviceForm(FlaskForm): default=1, ) id_device_supplier = StringField('Id Supplier', [validators.Optional()]) - phid = StringField('Placeholder Hardware identity (Phid)', [validators.Optional()]) + id_device_internal = StringField('Id Internal', [validators.Optional()]) pallet = StringField('Identity of pallet', [validators.Optional()]) components = TextAreaField('Components', [validators.Optional()]) info = TextAreaField('Info', [validators.Optional()]) @@ -382,7 +382,7 @@ class NewDeviceForm(FlaskForm): self.type.data = self._obj.type self.amount.render_kw = disabled self.id_device_supplier.data = self._obj.placeholder.id_device_supplier - self.phid.data = self._obj.placeholder.phid + self.id_device_internal.data = self._obj.placeholder.id_device_internal self.pallet.data = self._obj.placeholder.pallet self.info.data = self._obj.placeholder.info self.components.data = self._obj.placeholder.components @@ -411,7 +411,7 @@ class NewDeviceForm(FlaskForm): if self._obj.placeholder.is_abstract: self.type.render_kw = disabled self.amount.render_kw = disabled - self.id_device_supplier.render_kw = disabled + # self.id_device_supplier.render_kw = disabled self.pallet.render_kw = disabled self.info.render_kw = disabled self.components.render_kw = disabled @@ -476,28 +476,6 @@ class NewDeviceForm(FlaskForm): self.meid.errors = error is_valid = False - if self.phid.data and self.amount.data == 1 and not self._obj: - dev = Placeholder.query.filter( - Placeholder.phid == self.phid.data, Placeholder.owner == g.user - ).first() - if dev: - msg = "Error, exist one Placeholder device with this PHID" - self.phid.errors = [msg] - is_valid = False - - if ( - self.phid.data - and self._obj - and self.phid.data != self._obj.placeholder.phid - ): - dev = Placeholder.query.filter( - Placeholder.phid == self.phid.data, Device.owner == g.user - ).first() - if dev: - msg = "Error, exist one Placeholder device with this PHID" - self.phid.errors = [msg] - is_valid = False - if not is_valid: return False @@ -579,8 +557,8 @@ class NewDeviceForm(FlaskForm): def reset_ids(self): if self.amount.data > 1: - self.phid.data = None self.id_device_supplier.data = None + self.id_device_internal.data = None self.serial_number.data = None self.part_number.data = None self.sku.data = None @@ -590,8 +568,8 @@ class NewDeviceForm(FlaskForm): def get_placeholder(self): self.placeholder = Placeholder( **{ - 'phid': self.phid.data or None, 'id_device_supplier': self.id_device_supplier.data, + 'id_device_internal': self.id_device_internal.data, 'info': self.info.data, 'components': self.components.data, 'pallet': self.pallet.data, @@ -601,11 +579,13 @@ class NewDeviceForm(FlaskForm): return self.placeholder def edit_device(self): - self._obj.placeholder.phid = self.phid.data or self._obj.placeholder.phid if not self._obj.placeholder.is_abstract: self._obj.placeholder.id_device_supplier = ( self.id_device_supplier.data or None ) + self._obj.placeholder.id_device_internal = ( + self.id_device_internal.data or None + ) self._obj.placeholder.info = self.info.data or None self._obj.placeholder.components = self.components.data or None self._obj.placeholder.pallet = self.pallet.data or None @@ -1514,9 +1494,7 @@ class UploadPlaceholderForm(FlaskForm): else: self.source = "Excel File: {}".format(_file.filename) try: - data = ( - pd.read_excel(_file, converters={'Phid': str}).fillna('').to_dict() - ) + data = pd.read_excel(_file).fillna('').to_dict() except ValueError: txt = ["File don't have a correct format"] self.placeholder_file.errors = txt @@ -1538,12 +1516,12 @@ class UploadPlaceholderForm(FlaskForm): return False header = [ - 'Phid', 'Model', 'Manufacturer', 'Serial Number', 'Part Number', 'Id device Supplier', + 'Id device Internal', 'Pallet', 'Info', ] @@ -1557,32 +1535,7 @@ class UploadPlaceholderForm(FlaskForm): self.placeholders = [] schema = SnapshotSchema() self.path_snapshots = {} - for i in data['Phid'].keys(): - placeholder = None - data['Phid'][i] = str(data['Phid'][i]) - if data['Phid'][i]: - placeholder = Placeholder.query.filter_by(phid=data['Phid'][i]).first() - - # update one - if placeholder: - self.dev_update += 1 - device = placeholder.device - device.model = "{}".format(data['Model'][i]).lower() - device.manufacturer = "{}".format(data['Manufacturer'][i]).lower() - device.serial_number = "{}".format(data['Serial Number'][i]).lower() - device.part_number = "{}".format(data['Part Number'][i]).lower() - placeholder.id_device_supplier = "{}".format( - data['Id device Supplier'][i] - ) - placeholder.pallet = "{}".format(data['Pallet'][i]) - placeholder.info = "{}".format(data['Info'][i]) - - placeholder_log = PlaceholdersLog( - type="Update", source=self.source, placeholder=device.placeholder - ) - self.placeholders.append((device, placeholder_log)) - continue - + for i in data['Model'].keys(): # create a new one json_snapshot = { 'type': 'Snapshot', @@ -1597,8 +1550,8 @@ class UploadPlaceholderForm(FlaskForm): }, } json_placeholder = { - 'phid': data['Phid'][i] or None, 'id_device_supplier': data['Id device Supplier'][i], + 'id_device_internal': data['Id device Internal'][i], 'pallet': data['Pallet'][i], 'info': data['Info'][i], 'is_abstract': False, @@ -1635,7 +1588,6 @@ class EditPlaceholderForm(FlaskForm): serial_number = StringField('Serial Number', [validators.Optional()]) part_number = StringField('Part Number', [validators.Optional()]) id_device_supplier = StringField('Id Supplier', [validators.Optional()]) - phid = StringField('Phid', [validators.DataRequired()]) pallet = StringField('Pallet', [validators.Optional()]) info = StringField('Info', [validators.Optional()]) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 684be46f..41a3c16b 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -545,7 +545,7 @@ class DeviceCreateView(GenericMixin): tpy = form.type.data txt = f'{amount} placeholders Device "{tpy}" created successfully.' placeholder = ( - Placeholder.query.filter_by(owner=g.user) + Placeholder.query.filter(Placeholder.owner == g.user) .order_by(Placeholder.id.desc()) .first() ) diff --git a/ereuse_devicehub/migrations/versions/626c17026ca7_id_internal_in_placeholder.py b/ereuse_devicehub/migrations/versions/626c17026ca7_id_internal_in_placeholder.py new file mode 100644 index 00000000..8c5f28db --- /dev/null +++ b/ereuse_devicehub/migrations/versions/626c17026ca7_id_internal_in_placeholder.py @@ -0,0 +1,59 @@ +"""id internal in placeholder + +Revision ID: 626c17026ca7 +Revises: e919fe0611ff +Create Date: 2022-10-03 19:25:00.581699 + +""" +import sqlalchemy as sa +from alembic import context, op + +# revision identifiers, used by Alembic. +revision = '626c17026ca7' +down_revision = 'e919fe0611ff' +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_datas(): + con = op.get_bind() + sql = 'select id from common.user where phantom=false and active=true' + users = con.execute(sql) + for user in users: + phid = 1 + user_id = user.id + sql = f""" + select id from {get_inv()}.placeholder where owner_id='{user_id}' + order by id + """ + placeholders = con.execute(sql) + + for p in placeholders: + p_id = p.id + sql = f""" + update {get_inv()}.placeholder set phid='{phid}' + where id='{p_id}' + """ + con.execute(sql) + phid += 1 + + +def upgrade(): + op.add_column( + 'placeholder', + sa.Column('id_device_internal', sa.Unicode(), nullable=True), + schema=f'{get_inv()}', + ) + + upgrade_datas() + + +def downgrade(): + op.drop_column('placeholder', 'id_device_internal', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 5a40cfc6..67f4bb80 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -10,7 +10,6 @@ from boltons import urlutils from citext import CIText from ereuse_utils.naming import HID_CONVERSION_DOC, Naming from flask import g, request -from flask_sqlalchemy import event from more_itertools import unique_everseen from sqlalchemy import BigInteger, Boolean, Column from sqlalchemy import Enum as DBEnum @@ -75,11 +74,15 @@ def create_code(context): return hashcode.encode(_id) -def create_phid(context): - _hid = Placeholder.query.order_by(Placeholder.id.desc()).first() - if _hid: - return str(_hid.id + 1) - return '1' +def create_phid(context, count=1): + phid = str(Placeholder.query.filter(Placeholder.owner == g.user).count() + count) + if ( + Placeholder.query.filter(Placeholder.owner == g.user) + .filter(Placeholder.phid == phid) + .count() + ): + return create_phid(context, count=count + 1) + return phid class Device(Thing): @@ -901,8 +904,8 @@ class DisplayMixin: class Placeholder(Thing): id = Column(BigInteger, Sequence('placeholder_seq'), primary_key=True) - pallet = Column(Unicode(), nullable=True) phid = Column(Unicode(), nullable=False, default=create_phid) + pallet = Column(Unicode(), nullable=True) pallet.comment = "used for identification where from where is this placeholders" info = db.Column(CIText()) components = Column(CIText()) @@ -912,6 +915,8 @@ class Placeholder(Thing): id_device_supplier.comment = ( "Identification used for one supplier of one placeholders" ) + id_device_internal = db.Column(CIText()) + id_device_internal.comment = "Identification used internaly for the user" device_id = db.Column( BigInteger, @@ -1593,4 +1598,5 @@ def create_code_tag(mapper, connection, device): db.session.add(tag) +# from flask_sqlalchemy import event # event.listen(Device, 'after_insert', create_code_tag, propagate=True) diff --git a/ereuse_devicehub/static/js/create_device.js b/ereuse_devicehub/static/js/create_device.js index 04533411..4acece0f 100644 --- a/ereuse_devicehub/static/js/create_device.js +++ b/ereuse_devicehub/static/js/create_device.js @@ -29,14 +29,18 @@ function amountInputs() { if ($("#amount").val() > 1) { $("#Phid").hide(); $("#Id_device_supplier").hide(); + $("#Id_device_internal").hide(); $("#Serial_number").hide(); + $("#Part_number").hide(); $("#Sku").hide(); $("#imei").hide(); $("#meid").hide(); } else { $("#Phid").show(); $("#Id_device_supplier").show(); + $("#Id_device_internal").show(); $("#Serial_number").show(); + $("#Part_number").show(); $("#Sku").show(); deviceInputs(); }; diff --git a/ereuse_devicehub/templates/inventory/device_create.html b/ereuse_devicehub/templates/inventory/device_create.html index fa7bd591..6acf564f 100644 --- a/ereuse_devicehub/templates/inventory/device_create.html +++ b/ereuse_devicehub/templates/inventory/device_create.html @@ -90,19 +90,6 @@ {% endif %} -
- {% for error in form.phid.errors %}
- {{ error }}
- {% endfor %}
-
+ {% for error in form.id_device_internal.errors %}
+ {{ error }}
+ {% endfor %}
+