adding edit lot and fix bug of session.commit
This commit is contained in:
parent
18fead559c
commit
483f674957
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 -->
|
||||
|
|
Reference in New Issue