diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index d72fec04..2627118c 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -326,9 +326,10 @@ class NewDeviceForm(FlaskForm): pallet = StringField('Identity of pallet', [validators.Optional()]) components = TextAreaField('Components', [validators.Optional()]) info = TextAreaField('Info', [validators.Optional()]) - serial_number = StringField('Serial Number', [validators.Optional()]) model = StringField('Model', [validators.Optional()]) manufacturer = StringField('Manufacturer', [validators.Optional()]) + part_number = StringField('Part Number', [validators.Optional()]) + serial_number = StringField('Serial Number', [validators.Optional()]) appearance = StringField('Appearance', [validators.Optional()]) functionality = StringField('Functionality', [validators.Optional()]) brand = StringField('Brand') @@ -390,6 +391,7 @@ class NewDeviceForm(FlaskForm): self.info.data = self._obj.placeholder.info self.components.data = self._obj.placeholder.components self.serial_number.data = self._obj.serial_number + self.part_number.data = self._obj.part_number self.model.data = self._obj.model self.manufacturer.data = self._obj.manufacturer self.appearance.data = appearance @@ -419,6 +421,7 @@ class NewDeviceForm(FlaskForm): self.info.render_kw = disabled self.components.render_kw = disabled self.serial_number.render_kw = disabled + self.part_number.render_kw = disabled self.model.render_kw = disabled self.manufacturer.render_kw = disabled self.appearance.render_kw = disabled @@ -516,6 +519,8 @@ class NewDeviceForm(FlaskForm): self.model.data = self.model.data.lower() if self.serial_number.data: self.serial_number.data = self.serial_number.data.lower() + if self.part_number.data: + self.part_number.data = self.part_number.data.lower() return True @@ -541,6 +546,7 @@ class NewDeviceForm(FlaskForm): 'model': self.model.data, 'manufacturer': self.manufacturer.data, 'serialNumber': self.serial_number.data, + 'partNumber': self.part_number.data, 'brand': self.brand.data, 'version': self.version.data, 'generation': self.generation.data, @@ -587,6 +593,7 @@ class NewDeviceForm(FlaskForm): self.phid.data = None self.id_device_supplier.data = None self.serial_number.data = None + self.part_number.data = None self.sku.data = None self.imei.data = None self.meid.data = None @@ -616,6 +623,7 @@ class NewDeviceForm(FlaskForm): self._obj.model = self.model.data self._obj.manufacturer = self.manufacturer.data self._obj.serial_number = self.serial_number.data + self._obj.part_number = self.part_number.data self._obj.brand = self.brand.data self._obj.version = self.version.data self._obj.generation = self.generation.data @@ -1538,6 +1546,7 @@ class UploadPlaceholderForm(FlaskForm): 'Model', 'Manufacturer', 'Serial Number', + 'Part Number', 'Id device Supplier', 'Pallet', 'Info', @@ -1565,6 +1574,7 @@ class UploadPlaceholderForm(FlaskForm): 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] ) @@ -1587,6 +1597,7 @@ class UploadPlaceholderForm(FlaskForm): 'model': "{}".format(data['Model'][i]), 'manufacturer': "{}".format(data['Manufacturer'][i]), 'serialNumber': "{}".format(data['Serial Number'][i]), + 'partNumber': "{}".format(data['Part Number'][i]), }, } json_placeholder = { @@ -1626,6 +1637,7 @@ class EditPlaceholderForm(FlaskForm): manufacturer = StringField('Manufacturer', [validators.Optional()]) model = StringField('Model', [validators.Optional()]) 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()]) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 09c5024a..710d0493 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -528,9 +528,14 @@ class DeviceCreateView(GenericMixin): amount = form.amount.data tpy = form.type.data txt = f'{amount} real Device "{tpy}" created successfully.' - if amount == 1: - phid = form.placeholder.phid - dhid = form.placeholder.device.devicehub_id + placeholder = ( + Placeholder.query.filter_by(owner=g.user) + .order_by(Placeholder.id.desc()) + .first() + ) + if amount == 1 and placeholder: + phid = placeholder.phid + dhid = placeholder.device.devicehub_id txt = f'Device "{tpy}" real with PHID {phid} and DHID {dhid} ' txt += 'created successfully' messages.success(txt) diff --git a/ereuse_devicehub/migrations/versions/bcfda54aaf2f_add_part_number_to_device.py b/ereuse_devicehub/migrations/versions/bcfda54aaf2f_add_part_number_to_device.py new file mode 100644 index 00000000..f563d2b6 --- /dev/null +++ b/ereuse_devicehub/migrations/versions/bcfda54aaf2f_add_part_number_to_device.py @@ -0,0 +1,35 @@ +"""add part number to device + +Revision ID: bcfda54aaf2f +Revises: 6b0880832b78 +Create Date: 2022-09-13 16:29:35.403897 + +""" +import citext +import sqlalchemy as sa +from alembic import context, op + +# revision identifiers, used by Alembic. +revision = 'bcfda54aaf2f' +down_revision = '6b0880832b78' +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( + 'device', + sa.Column('part_number', citext.CIText(), nullable=True), + schema=f'{get_inv()}', + ) + + +def downgrade(): + op.drop_column('device', 'part_number', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 7735c817..9d32e796 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -128,6 +128,8 @@ class Device(Thing): """ serial_number = Column(Unicode(), check_lower('serial_number')) serial_number.comment = """The serial number of the device in lower case.""" + part_number = Column(Unicode(), check_lower('part_number')) + part_number.comment = """The part number of the device in lower case.""" brand = db.Column(CIText()) brand.comment = """A naming for consumers. This field can represent several models, so it can be ambiguous, and it is not used to diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index 2b9530db..890dd287 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -52,6 +52,9 @@ class Device(Thing): serial_number = SanitizedStr( lower=True, validate=Length(max=STR_BIG_SIZE), data_key='serialNumber' ) + part_number = SanitizedStr( + lower=True, validate=Length(max=STR_BIG_SIZE), data_key='partNumber' + ) brand = SanitizedStr( validate=Length(max=STR_BIG_SIZE), description=m.Device.brand.comment ) diff --git a/ereuse_devicehub/resources/documents/device_row.py b/ereuse_devicehub/resources/documents/device_row.py index f0a7e8ff..172b9b3b 100644 --- a/ereuse_devicehub/resources/documents/device_row.py +++ b/ereuse_devicehub/resources/documents/device_row.py @@ -259,6 +259,7 @@ class BaseDeviceRow(OrderedDict): self['Real Components'] = '' self['Real Type'] = '' self['Real Serial Number'] = '' + self['Real Part Number'] = '' self['Real Model'] = '' self['Real Manufacturer'] = '' @@ -513,6 +514,7 @@ class DeviceRow(BaseDeviceRow): self['Real Type'] = none2str(self.placeholder.device.type) self['Real Manufacturer'] = none2str(self.placeholder.device.manufacturer) self['Real Model'] = none2str(self.placeholder.device.model) + self['Real Part Number'] = none2str(self.placeholder.device.part_number) self['Real Serial Number'] = none2str(self.placeholder.device.serial_number) diff --git a/ereuse_devicehub/templates/inventory/device_create.html b/ereuse_devicehub/templates/inventory/device_create.html index cdf674a3..78d0bfef 100644 --- a/ereuse_devicehub/templates/inventory/device_create.html +++ b/ereuse_devicehub/templates/inventory/device_create.html @@ -155,6 +155,19 @@ {% endif %} +
+ + {{ form.part_number(class_="form-control") }} + Part number of this device + {% if form.part_number.errors %} +

+ {% for error in form.part_number.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %} +
+
{{ form.serial_number(class_="form-control") }}