refactoring DeviceListView

This commit is contained in:
Cayo Puigdefabregas 2021-12-30 21:40:44 +01:00
parent d3dc5c8986
commit 0f5ab17a3f

View file

@ -14,29 +14,18 @@ class DeviceListView(View):
decorators = [login_required] decorators = [login_required]
template_name = 'inventory/device_list.html' template_name = 'inventory/device_list.html'
def dispatch_request(self): def dispatch_request(self, id=None):
# TODO @cayop adding filter # TODO @cayop adding filter
filter_types = ['Desktop', 'Laptop', 'Server'] filter_types = ['Desktop', 'Laptop', 'Server']
devices = Device.query.filter(
Device.owner_id == current_user.id).filter(
Device.type.in_(filter_types))
lots = Lot.query.filter(Lot.owner_id == current_user.id) lots = Lot.query.filter(Lot.owner_id == current_user.id)
lot = None
context = {'devices': devices, 'lots': lots, 'form_lot_device': LotDeviceAddForm()} if id:
return flask.render_template(self.template_name, **context) lot = lots.filter(Lot.id == id).one()
devices = [dev for dev in lot.devices if dev.type in filter_types]
else:
class LotDeviceListView(View): devices = Device.query.filter(
decorators = [login_required] Device.owner_id == current_user.id).filter(
template_name = 'inventory/device_list.html' Device.type.in_(filter_types))
def dispatch_request(self, id):
# TODO @cayop adding filter
lots = Lot.query.filter(Lot.owner_id == current_user.id)
lot = lots.filter(Lot.id == id).one()
filter_types = ['Desktop', 'Laptop', 'Server']
devices = [dev for dev in lot.devices if dev.type in filter_types]
context = {'devices': devices, context = {'devices': devices,
'lots': lots, 'lots': lots,
@ -78,7 +67,7 @@ class LotView(View):
devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist')) devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist'))
devices.add_url_rule('/lot/<string:id>/device/', view_func=LotDeviceListView.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/add', view_func=LotView.as_view('lot_add')) devices.add_url_rule('/lot/add', view_func=LotView.as_view('lot_add'))
devices.add_url_rule('/lot/<string:id>/', view_func=LotView.as_view('lot_edit')) devices.add_url_rule('/lot/<string:id>/', view_func=LotView.as_view('lot_edit'))