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)]) name = StringField(u'Name', [validators.length(min=1)])
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
id = kwargs.pop('id', None) self.id = kwargs.pop('id', None)
self.instance = None self.instance = None
if id: if self.id:
self.instance = Lot.query.filter(Lot.id == id).filter( self.instance = Lot.query.filter(Lot.id == self.id).filter(
Lot.owner_id == g.user.id).one() Lot.owner_id == g.user.id).one()
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if self.instance and not self.name.data: if self.instance and not self.name.data:
@ -62,9 +62,13 @@ class LotForm(FlaskForm):
else: else:
self.instance = Lot(name=name) 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() db.session.commit()
return self.instance return self.id
def remove(self): def remove(self):
if self.instance and not self.instance.devices: if self.instance and not self.instance.devices:

View File

@ -73,8 +73,11 @@ class LotView(View):
self.title = "Edit lot" self.title = "Edit lot"
form = LotForm(id=id) form = LotForm(id=id)
if form.validate_on_submit(): if form.validate_on_submit():
lot = form.save() form.save()
next_url = url_for('inventory.devices.lotdevicelist', id=lot.id) 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.redirect(next_url)
return flask.render_template(self.template_name, form=form, title=self.title) return flask.render_template(self.template_name, form=form, title=self.title)

View File

@ -28,6 +28,17 @@
<div class="col-xl-12"> <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">
<div class="card-body pt-3"> <div class="card-body pt-3">
<!-- Bordered Tabs --> <!-- Bordered Tabs -->