adding edit lot and fix bug of session.commit

This commit is contained in:
Cayo Puigdefabregas 2022-01-05 11:10:32 +01:00
parent 18fead559c
commit 483f674957
3 changed files with 25 additions and 7 deletions

View File

@ -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,10 +62,14 @@ class LotForm(FlaskForm):
else:
self.instance = Lot(name=name)
if not self.id:
db.session.add(self.instance)
db.session.commit()
return self.instance
db.session.commit()
return self.id
def remove(self):
if self.instance and not self.instance.devices:
self.instance.delete()

View File

@ -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)

View File

@ -28,6 +28,17 @@
<div class="col-xl-12">
{% if lot %}
<div class="card">
<div class="card-body pt-3">
<!-- Bordered Tabs -->
<div class="col-12">
<h3><a href="{{ url_for('inventory.devices.lot_edit', id=lot.id) }}">{{ lot.name }}</a></h3>
</div>
</div>
</div>
{% endif %}
<div class="card">
<div class="card-body pt-3">
<!-- Bordered Tabs -->