From e15e45748134d439b6a769236b1342ddf13dadef Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Tue, 8 Feb 2022 11:49:13 +0100 Subject: [PATCH] Refactor: create method `get_next_url()` --- ereuse_devicehub/inventory/views.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 2ae1c9c4..e4e43ba5 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -295,34 +295,37 @@ class NewActionView(View): def dispatch_request(self): self.form = self._form() - next_url = url_for('inventory.devices.devicelist') - lot_id = self.form.lot.data - if lot_id: - next_url = url_for('inventory.devices.lotdevicelist', lot_id=lot_id) - if self.form.validate_on_submit(): instance = self.form.save() messages.success('Action "{}" created successfully!'.format(instance.type)) + + next_url = self.get_next_url() return flask.redirect(next_url) + def get_next_url(self): + lot_id = self.form.lot.data + + if lot_id: + return url_for('inventory.devices.lotdevicelist', lot_id=lot_id) + + return url_for('inventory.devices.devicelist') + class NewAllocateView(NewActionView, DeviceListMix): methods = ['POST'] _form = AllocateForm - def dispatch_request(self, lot_id=None): + def dispatch_request(self): self.form = self._form() - next_url = url_for('inventory.devices.devicelist') - lot_id = self.form.lot.data - if lot_id: - next_url = url_for('inventory.devices.lotdevicelist', lot_id=lot_id) - if self.form.validate_on_submit(): instance = self.form.save() messages.success('Action "{}" created successfully!'.format(instance.type)) + + next_url = self.get_next_url() return flask.redirect(next_url) + lot_id = self.form.lot.data self.get_context(lot_id) self.context['form_new_allocate'] = self.form return flask.render_template(self.template_name, **self.context)