2021-12-28 11:36:02 +00:00
|
|
|
import flask
|
|
|
|
from flask.views import View
|
2021-12-30 20:35:54 +00:00
|
|
|
from flask import Blueprint, url_for
|
2021-12-28 12:55:56 +00:00
|
|
|
from flask_login import login_required, current_user
|
2021-12-28 11:36:02 +00:00
|
|
|
|
2021-12-29 12:44:28 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
2021-12-30 12:53:28 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Device
|
2022-01-03 10:32:12 +00:00
|
|
|
from ereuse_devicehub.inventory.forms import LotDeviceForm, LotForm
|
2021-12-28 11:36:02 +00:00
|
|
|
|
2021-12-28 12:55:56 +00:00
|
|
|
devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory')
|
2021-12-28 11:36:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceListView(View):
|
2021-12-28 12:55:56 +00:00
|
|
|
decorators = [login_required]
|
2021-12-28 11:36:02 +00:00
|
|
|
template_name = 'inventory/device_list.html'
|
|
|
|
|
2021-12-30 20:40:44 +00:00
|
|
|
def dispatch_request(self, id=None):
|
2021-12-29 09:13:34 +00:00
|
|
|
# TODO @cayop adding filter
|
2021-12-28 12:55:56 +00:00
|
|
|
filter_types = ['Desktop', 'Laptop', 'Server']
|
2021-12-30 16:23:34 +00:00
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
2021-12-30 20:40:44 +00:00
|
|
|
lot = None
|
|
|
|
if id:
|
|
|
|
lot = lots.filter(Lot.id == id).one()
|
|
|
|
devices = [dev for dev in lot.devices if dev.type in filter_types]
|
|
|
|
else:
|
|
|
|
devices = Device.query.filter(
|
|
|
|
Device.owner_id == current_user.id).filter(
|
|
|
|
Device.type.in_(filter_types))
|
2021-12-30 16:23:34 +00:00
|
|
|
|
|
|
|
context = {'devices': devices,
|
|
|
|
'lots': lots,
|
2022-01-03 10:32:12 +00:00
|
|
|
'form_lot_device': LotDeviceForm(),
|
2021-12-30 16:23:34 +00:00
|
|
|
'lot': lot}
|
|
|
|
return flask.render_template(self.template_name, **context)
|
|
|
|
|
|
|
|
|
2021-12-30 12:53:28 +00:00
|
|
|
class LotDeviceAddView(View):
|
|
|
|
methods = ['POST']
|
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'inventory/device_list.html'
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
2022-01-03 10:32:12 +00:00
|
|
|
form = LotDeviceForm()
|
2021-12-30 12:53:28 +00:00
|
|
|
if form.validate_on_submit():
|
2021-12-30 20:35:54 +00:00
|
|
|
form.save()
|
|
|
|
|
2022-01-03 10:32:12 +00:00
|
|
|
next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.data)
|
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
|
|
|
|
|
|
|
class LotDeviceDeleteView(View):
|
|
|
|
methods = ['POST']
|
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'inventory/device_list.html'
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
form = LotDeviceForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.remove()
|
|
|
|
|
|
|
|
next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.data)
|
2021-12-30 20:35:54 +00:00
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
|
|
|
|
|
|
|
class LotView(View):
|
|
|
|
methods = ['GET', 'POST']
|
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'inventory/lot.html'
|
|
|
|
title = "Add a new lot"
|
|
|
|
|
|
|
|
def dispatch_request(self, id=None):
|
|
|
|
if id:
|
|
|
|
self.title = "Edit lot"
|
|
|
|
form = LotForm(id=id)
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save()
|
|
|
|
next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.id)
|
2021-12-30 12:53:28 +00:00
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2021-12-30 20:35:54 +00:00
|
|
|
return flask.render_template(self.template_name, form=form, title=self.title)
|
|
|
|
|
2021-12-30 12:53:28 +00:00
|
|
|
|
2022-01-03 11:28:42 +00:00
|
|
|
class LotDeleteView(View):
|
|
|
|
methods = ['GET']
|
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'inventory/device_list.html'
|
|
|
|
|
|
|
|
def dispatch_request(self, id):
|
|
|
|
form = LotForm(id=id)
|
|
|
|
form.remove()
|
|
|
|
next_url = url_for('inventory.devices.devicelist')
|
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-12-29 09:13:34 +00:00
|
|
|
devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist'))
|
2021-12-30 20:40:44 +00:00
|
|
|
devices.add_url_rule('/lot/<string:id>/device/', view_func=DeviceListView.as_view('lotdevicelist'))
|
2021-12-30 20:35:54 +00:00
|
|
|
devices.add_url_rule('/lot/devices/add/', view_func=LotDeviceAddView.as_view('lot_devices_add'))
|
2022-01-03 10:32:12 +00:00
|
|
|
devices.add_url_rule('/lot/devices/del/', view_func=LotDeviceDeleteView.as_view('lot_devices_del'))
|
2022-01-03 11:28:42 +00:00
|
|
|
devices.add_url_rule('/lot/add/', view_func=LotView.as_view('lot_add'))
|
|
|
|
devices.add_url_rule('/lot/<string:id>/del/', view_func=LotDeleteView.as_view('lot_del'))
|
2021-12-30 20:35:54 +00:00
|
|
|
devices.add_url_rule('/lot/<string:id>/', view_func=LotView.as_view('lot_edit'))
|