From a518e4ebf8351569b8b429f1b6bb39a729bf9679 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 4 Jan 2022 12:45:13 +0100 Subject: [PATCH] fixing bug when create a new lot --- ereuse_devicehub/inventory/forms.py | 28 ++++++++++++++++------------ ereuse_devicehub/inventory/views.py | 4 ++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 8654e76e..8fc94671 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -45,27 +45,31 @@ class LotForm(FlaskForm): def __init__(self, *args, **kwargs): id = kwargs.pop('id', None) - self.lot = None + self.instance = None if id: - self.lot = Lot.query.filter(Lot.id == id).filter( + self.instance = Lot.query.filter(Lot.id == id).filter( Lot.owner_id == g.user.id).one() super().__init__(*args, **kwargs) - if self.lot and not self.name.data: - self.name.data = self.lot.name + if self.instance and not self.name.data: + self.name.data = self.instance.name def save(self): name = self.name.data.strip() - if self.lot: - if self.lot.name == name: - return - self.lot.name = name + if self.instance: + if self.instance.name == name: + return self.instance + self.instance.name = name else: - self.lot = Lot(name=name) + self.instance = Lot(name=name) - db.session.add(self.lot) + db.session.add(self.instance) db.session.commit() + return self.instance def remove(self): - if self.lot and not self.lot.devices: - self.lot.delete() + if self.instance and not self.instance.devices: + self.instance.delete() db.session.commit() + return self.instance + + diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 4be3b992..77f1e173 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -73,8 +73,8 @@ class LotView(View): self.title = "Edit lot" form = LotForm(id=id) if form.validate_on_submit(): - form.save() - next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.id) + lot = form.save() + next_url = url_for('inventory.devices.lotdevicelist', id=lot.id) return flask.redirect(next_url) return flask.render_template(self.template_name, form=form, title=self.title)