diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 0eafb9b0..e7f06a0b 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -424,27 +424,11 @@ class NewDeviceForm(FlaskForm): if self.serial_number.data: self.serial_number.data = self.serial_number.data.lower() - if not self.phid.data: - _hid = Placeholder.query.order_by(Placeholder.id.desc()).first() - if _hid: - _hid = str(_hid.id + 1) - else: - _hid = '1' - self.phid.data = _hid.lower() - self.phid.data = self.phid.data.lower() - - if self.amount.data > 1: - self.phid.data = None - self.id_device_supplier.data = None - self.serial_number.data = None - self.sku.data = None - self.imei.data = None - self.meid.data = None - return True def save(self, commit=True): for n in range(self.amount.data): + self.reset_ids() self.create_device() if commit: @@ -500,6 +484,8 @@ class NewDeviceForm(FlaskForm): snapshot_json['device'].meid = self.meid.data snapshot_json['device'].placeholder = self.get_placeholder() + snapshot_json['device'].hid = self.phid.data + snapshot = upload_form.build(snapshot_json) move_json(self.tmp_snapshots, path_snapshot, g.user.email) @@ -509,6 +495,27 @@ class NewDeviceForm(FlaskForm): return snapshot + def get_phid(self): + _hid = self.phid.data + if not _hid: + _hid = Placeholder.query.order_by(Placeholder.id.desc()).first() + if _hid: + _hid = str(_hid.id + 1) + else: + _hid = '1' + + self.phid.data = _hid.lower() + + def reset_ids(self): + if self.amount.data > 1: + self.phid.data = None + self.id_device_supplier.data = None + self.serial_number.data = None + self.sku.data = None + self.imei.data = None + self.meid.data = None + self.get_phid() + def get_placeholder(self): self.placeholder = Placeholder( **{ diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index 4c4d2182..9982890d 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -627,7 +627,7 @@ def test_add_with_ammount_laptops(user3: UserClientFlask): assert 'Device "Laptop" created successfully!' in body for dev in Device.query.all(): assert dev.type == 'Laptop' - assert dev.placeholder.id_device_supplier == "b2" + assert dev.placeholder.id_device_supplier is None assert dev.hid in [str(x) for x in range(1, num + 1)] assert Device.query.count() == num