split LotView in LotCreateView and LotUpdateView

This commit is contained in:
Cayo Puigdefabregas 2022-01-12 17:09:20 +01:00
parent e0e4ecfd2d
commit d6dd740142
1 changed files with 21 additions and 8 deletions

View File

@ -62,22 +62,35 @@ class LotDeviceDeleteView(View):
return flask.redirect(next_url) return flask.redirect(next_url)
class LotView(View): class LotCreateView(View):
methods = ['GET', 'POST'] methods = ['GET', 'POST']
decorators = [login_required] decorators = [login_required]
template_name = 'inventory/lot.html' template_name = 'inventory/lot.html'
title = "Add a new lot" title = "Add a new lot"
def dispatch_request(self, id=None): def dispatch_request(self):
if id: if id:
self.title = "Edit lot" self.title = "Edit lot"
form = LotForm()
if form.validate_on_submit():
form.save()
next_url = url_for('inventory.devices.lotdevicelist', id=form.instance.id)
return flask.redirect(next_url)
return flask.render_template(self.template_name, form=form, title=self.title)
class LotUpdateView(View):
methods = ['GET', 'POST']
decorators = [login_required]
template_name = 'inventory/lot.html'
title = "Edit a new lot"
def dispatch_request(self, id):
form = LotForm(id=id) form = LotForm(id=id)
if form.validate_on_submit(): if form.validate_on_submit():
form.save() form.save()
lot_id = id next_url = url_for('inventory.devices.lotdevicelist', 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)
@ -100,6 +113,6 @@ devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist'))
devices.add_url_rule('/lot/<string:id>/device/', view_func=DeviceListView.as_view('lotdevicelist')) devices.add_url_rule('/lot/<string:id>/device/', view_func=DeviceListView.as_view('lotdevicelist'))
devices.add_url_rule('/lot/devices/add/', view_func=LotDeviceAddView.as_view('lot_devices_add')) devices.add_url_rule('/lot/devices/add/', view_func=LotDeviceAddView.as_view('lot_devices_add'))
devices.add_url_rule('/lot/devices/del/', view_func=LotDeviceDeleteView.as_view('lot_devices_del')) devices.add_url_rule('/lot/devices/del/', view_func=LotDeviceDeleteView.as_view('lot_devices_del'))
devices.add_url_rule('/lot/add/', view_func=LotView.as_view('lot_add')) devices.add_url_rule('/lot/add/', view_func=LotCreateView.as_view('lot_add'))
devices.add_url_rule('/lot/<string:id>/del/', view_func=LotDeleteView.as_view('lot_del')) devices.add_url_rule('/lot/<string:id>/del/', view_func=LotDeleteView.as_view('lot_del'))
devices.add_url_rule('/lot/<string:id>/', view_func=LotView.as_view('lot_edit')) devices.add_url_rule('/lot/<string:id>/', view_func=LotUpdateView.as_view('lot_edit'))