From 483f6749574d8004a018229fa76d06b0638b667a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 5 Jan 2022 11:10:32 +0100 Subject: [PATCH] adding edit lot and fix bug of session.commit --- ereuse_devicehub/inventory/forms.py | 14 +++++++++----- ereuse_devicehub/inventory/views.py | 7 +++++-- .../templates/inventory/device_list.html | 11 +++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index ea7f7c4c..701e73a7 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -44,10 +44,10 @@ class LotForm(FlaskForm): name = StringField(u'Name', [validators.length(min=1)]) def __init__(self, *args, **kwargs): - id = kwargs.pop('id', None) + self.id = kwargs.pop('id', None) self.instance = None - if id: - self.instance = Lot.query.filter(Lot.id == id).filter( + if self.id: + self.instance = Lot.query.filter(Lot.id == self.id).filter( Lot.owner_id == g.user.id).one() super().__init__(*args, **kwargs) if self.instance and not self.name.data: @@ -62,9 +62,13 @@ class LotForm(FlaskForm): else: self.instance = Lot(name=name) - db.session.add(self.instance) + if not self.id: + db.session.add(self.instance) + db.session.commit() + return self.instance + db.session.commit() - return self.instance + return self.id def remove(self): if self.instance and not self.instance.devices: diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 77f1e173..55ed61e8 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -73,8 +73,11 @@ class LotView(View): self.title = "Edit lot" form = LotForm(id=id) if form.validate_on_submit(): - lot = form.save() - next_url = url_for('inventory.devices.lotdevicelist', id=lot.id) + form.save() + lot_id = id + if not id: + lot_id = form.instance.id + 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) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 017be5f5..376afe75 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -28,6 +28,17 @@
+ {% if lot %} +
+
+ + + +
+
+ {% endif %}