fixing bug when create a new lot

This commit is contained in:
Cayo Puigdefabregas 2022-01-04 12:45:13 +01:00
parent 5b9a9d482d
commit a518e4ebf8
2 changed files with 18 additions and 14 deletions

View File

@ -45,27 +45,31 @@ class LotForm(FlaskForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
id = kwargs.pop('id', None) id = kwargs.pop('id', None)
self.lot = None self.instance = None
if id: 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() Lot.owner_id == g.user.id).one()
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if self.lot and not self.name.data: if self.instance and not self.name.data:
self.name.data = self.lot.name self.name.data = self.instance.name
def save(self): def save(self):
name = self.name.data.strip() name = self.name.data.strip()
if self.lot: if self.instance:
if self.lot.name == name: if self.instance.name == name:
return return self.instance
self.lot.name = name self.instance.name = name
else: else:
self.lot = Lot(name=name) self.instance = Lot(name=name)
db.session.add(self.lot) db.session.add(self.instance)
db.session.commit() db.session.commit()
return self.instance
def remove(self): def remove(self):
if self.lot and not self.lot.devices: if self.instance and not self.instance.devices:
self.lot.delete() self.instance.delete()
db.session.commit() db.session.commit()
return self.instance

View File

@ -73,8 +73,8 @@ 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():
form.save() lot = form.save()
next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.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)