2021-12-28 11:36:02 +00:00
|
|
|
import flask
|
|
|
|
from flask.views import View
|
2022-01-12 20:02:47 +00:00
|
|
|
from flask import Blueprint, url_for, request
|
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
|
2022-01-11 12:13:57 +00:00
|
|
|
from ereuse_devicehub.resources.tag.model import Tag
|
2021-12-30 12:53:28 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Device
|
2022-01-25 09:37:30 +00:00
|
|
|
from ereuse_devicehub.inventory.forms import LotDeviceForm, LotForm, UploadSnapshotForm, \
|
2022-02-02 10:41:29 +00:00
|
|
|
NewDeviceForm, TagForm, TagUnnamedForm, TagDeviceForm
|
2021-12-28 11:36:02 +00:00
|
|
|
|
2022-02-02 10:41:29 +00:00
|
|
|
# TODO(@slamora): rename base 'inventory.devices' --> 'inventory'
|
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'
|
|
|
|
|
2022-01-25 09:30:46 +00:00
|
|
|
def dispatch_request(self, lot_id=None):
|
2021-12-29 09:13:34 +00:00
|
|
|
# TODO @cayop adding filter
|
2022-01-25 09:30:46 +00:00
|
|
|
# https://github.com/eReuse/devicehub-teal/blob/testing/ereuse_devicehub/resources/device/views.py#L56
|
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
|
2022-01-25 11:53:36 +00:00
|
|
|
tags = Tag.query.filter(Tag.owner_id == current_user.id).filter(
|
|
|
|
Tag.device_id == None).order_by(Tag.created.desc())
|
|
|
|
|
2022-01-25 09:30:46 +00:00
|
|
|
if lot_id:
|
|
|
|
lot = lots.filter(Lot.id == lot_id).one()
|
2021-12-30 20:40:44 +00:00
|
|
|
devices = [dev for dev in lot.devices if dev.type in filter_types]
|
2022-01-12 19:40:57 +00:00
|
|
|
devices = sorted(devices, key=lambda x: x.updated, reverse=True)
|
2021-12-30 20:40:44 +00:00
|
|
|
else:
|
|
|
|
devices = Device.query.filter(
|
|
|
|
Device.owner_id == current_user.id).filter(
|
2022-01-18 10:24:27 +00:00
|
|
|
Device.type.in_(filter_types)).filter(Device.lots == None).order_by(
|
|
|
|
Device.updated.desc())
|
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(),
|
2022-01-25 11:53:36 +00:00
|
|
|
'form_tag_device': TagDeviceForm(),
|
|
|
|
'lot': lot,
|
|
|
|
'tags': tags}
|
2021-12-30 16:23:34 +00:00
|
|
|
return flask.render_template(self.template_name, **context)
|
|
|
|
|
|
|
|
|
2022-02-02 10:41:29 +00:00
|
|
|
class DeviceDetailView(View):
|
2022-01-13 15:08:55 +00:00
|
|
|
decorators = [login_required]
|
2022-02-02 11:21:30 +00:00
|
|
|
template_name = 'inventory/device_detail.html'
|
2022-01-13 15:08:55 +00:00
|
|
|
|
|
|
|
def dispatch_request(self, id):
|
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
|
|
|
device = Device.query.filter(
|
2022-02-02 10:41:29 +00:00
|
|
|
Device.owner_id == current_user.id).filter(Device.devicehub_id == id).one()
|
2022-01-13 15:08:55 +00:00
|
|
|
|
2022-02-02 11:21:30 +00:00
|
|
|
context = {
|
|
|
|
'device': device,
|
|
|
|
'lots': lots,
|
|
|
|
'page_title': 'Device {}'.format(device.devicehub_id),
|
|
|
|
}
|
2022-01-13 15:08:55 +00:00
|
|
|
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-26 10:48:37 +00:00
|
|
|
next_url = request.referrer or url_for('inventory.devices.devicelist')
|
|
|
|
return flask.redirect(next_url)
|
2022-01-03 10:32:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2022-01-26 10:48:37 +00:00
|
|
|
next_url = request.referrer or url_for('inventory.devices.devicelist')
|
|
|
|
return flask.redirect(next_url)
|
2021-12-30 20:35:54 +00:00
|
|
|
|
|
|
|
|
2022-01-12 16:09:20 +00:00
|
|
|
class LotCreateView(View):
|
2021-12-30 20:35:54 +00:00
|
|
|
methods = ['GET', 'POST']
|
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'inventory/lot.html'
|
|
|
|
title = "Add a new lot"
|
|
|
|
|
2022-01-12 16:09:20 +00:00
|
|
|
def dispatch_request(self):
|
|
|
|
form = LotForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save()
|
2022-01-25 09:30:46 +00:00
|
|
|
next_url = url_for('inventory.devices.lotdevicelist', lot_id=form.id)
|
2022-01-12 16:09:20 +00:00
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2022-01-12 18:44:10 +00:00
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
|
|
|
return flask.render_template(self.template_name, form=form, title=self.title, lots=lots)
|
2022-01-12 16:09:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LotUpdateView(View):
|
|
|
|
methods = ['GET', 'POST']
|
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'inventory/lot.html'
|
|
|
|
title = "Edit a new lot"
|
|
|
|
|
|
|
|
def dispatch_request(self, id):
|
2021-12-30 20:35:54 +00:00
|
|
|
form = LotForm(id=id)
|
|
|
|
if form.validate_on_submit():
|
2022-01-05 10:10:32 +00:00
|
|
|
form.save()
|
2022-01-25 09:30:46 +00:00
|
|
|
next_url = url_for('inventory.devices.lotdevicelist', lot_id=id)
|
2021-12-30 12:53:28 +00:00
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2022-01-12 18:44:10 +00:00
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
|
|
|
return flask.render_template(self.template_name, form=form, title=self.title, lots=lots)
|
2021-12-30 20:35:54 +00:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2022-01-17 13:17:52 +00:00
|
|
|
class UploadSnapshotView(View):
|
|
|
|
methods = ['GET', 'POST']
|
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'inventory/upload_snapshot.html'
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
2022-02-02 11:21:30 +00:00
|
|
|
context = {'page_title': 'Upload Snapshot'}
|
2022-01-18 10:24:27 +00:00
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id).all()
|
2022-01-17 13:17:52 +00:00
|
|
|
form = UploadSnapshotForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save()
|
|
|
|
|
2022-02-02 11:21:30 +00:00
|
|
|
return flask.render_template(self.template_name, form=form, lots=lots, **context)
|
2022-01-17 13:17:52 +00:00
|
|
|
|
|
|
|
|
2022-02-02 11:21:30 +00:00
|
|
|
class DeviceCreateView(View):
|
2022-01-19 12:40:40 +00:00
|
|
|
methods = ['GET', 'POST']
|
|
|
|
decorators = [login_required]
|
2022-02-02 11:21:30 +00:00
|
|
|
template_name = 'inventory/device_create.html'
|
2022-01-19 12:40:40 +00:00
|
|
|
|
|
|
|
def dispatch_request(self):
|
2022-02-02 11:21:30 +00:00
|
|
|
context = {'page_title': 'New Device'}
|
2022-01-19 12:40:40 +00:00
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id).all()
|
|
|
|
form = NewDeviceForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save()
|
|
|
|
next_url = url_for('inventory.devices.devicelist')
|
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2022-02-02 11:21:30 +00:00
|
|
|
return flask.render_template(self.template_name, form=form, lots=lots, **context)
|
2022-01-19 12:40:40 +00:00
|
|
|
|
|
|
|
|
2022-01-11 12:13:57 +00:00
|
|
|
class TagListView(View):
|
|
|
|
methods = ['GET']
|
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'inventory/tag_list.html'
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
2022-02-02 10:41:29 +00:00
|
|
|
tags = Tag.query.filter(Tag.owner_id == current_user.id)
|
|
|
|
context = {
|
|
|
|
'lots': [],
|
|
|
|
'tags': tags,
|
|
|
|
'page_title': 'Tags Management',
|
|
|
|
}
|
2022-01-11 12:13:57 +00:00
|
|
|
return flask.render_template(self.template_name, **context)
|
|
|
|
|
2022-01-03 11:28:42 +00:00
|
|
|
|
2022-01-11 12:42:01 +00:00
|
|
|
class TagAddView(View):
|
|
|
|
methods = ['GET', 'POST']
|
|
|
|
decorators = [login_required]
|
2022-02-02 10:41:29 +00:00
|
|
|
template_name = 'inventory/tag_create.html'
|
2022-01-11 12:42:01 +00:00
|
|
|
|
|
|
|
def dispatch_request(self):
|
2022-02-02 10:41:29 +00:00
|
|
|
context = {'page_title': 'New Tag'}
|
2022-01-11 12:42:01 +00:00
|
|
|
form = TagForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save()
|
|
|
|
next_url = url_for('inventory.devices.taglist')
|
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2022-02-02 10:41:29 +00:00
|
|
|
return flask.render_template(self.template_name, form=form, **context)
|
2022-01-11 12:42:01 +00:00
|
|
|
|
|
|
|
|
2022-01-26 12:16:18 +00:00
|
|
|
class TagAddUnnamedView(View):
|
|
|
|
methods = ['GET', 'POST']
|
|
|
|
decorators = [login_required]
|
2022-02-02 10:41:29 +00:00
|
|
|
template_name = 'inventory/tag_create_unnamed.html'
|
2022-01-26 12:16:18 +00:00
|
|
|
|
|
|
|
def dispatch_request(self):
|
2022-02-02 10:41:29 +00:00
|
|
|
context = {'page_title': 'New Unnamed Tag'}
|
2022-01-26 12:16:18 +00:00
|
|
|
form = TagUnnamedForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save()
|
|
|
|
next_url = url_for('inventory.devices.taglist')
|
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2022-02-02 10:41:29 +00:00
|
|
|
return flask.render_template(self.template_name, form=form, **context)
|
2022-01-26 12:16:18 +00:00
|
|
|
|
|
|
|
|
2022-02-02 10:41:29 +00:00
|
|
|
class TagDetailView(View):
|
2022-01-26 12:59:56 +00:00
|
|
|
decorators = [login_required]
|
2022-02-02 10:41:29 +00:00
|
|
|
template_name = 'inventory/tag_detail.html'
|
2022-01-26 12:59:56 +00:00
|
|
|
|
|
|
|
def dispatch_request(self, id):
|
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
|
|
|
tag = Tag.query.filter(
|
2022-02-02 10:41:29 +00:00
|
|
|
Tag.owner_id == current_user.id).filter(Tag.id == id).one()
|
2022-01-26 12:59:56 +00:00
|
|
|
|
2022-02-02 10:41:29 +00:00
|
|
|
context = {
|
|
|
|
'lots': lots,
|
|
|
|
'tag': tag,
|
|
|
|
'page_title': '{} Tag'.format(tag.code),
|
|
|
|
}
|
2022-01-26 12:59:56 +00:00
|
|
|
return flask.render_template(self.template_name, **context)
|
|
|
|
|
|
|
|
|
2022-02-02 11:21:30 +00:00
|
|
|
class TagLinkDeviceView(View):
|
2022-01-25 11:53:36 +00:00
|
|
|
methods = ['POST']
|
|
|
|
decorators = [login_required]
|
2022-02-02 11:21:30 +00:00
|
|
|
# template_name = 'inventory/device_list.html'
|
2022-01-25 11:53:36 +00:00
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
form = TagDeviceForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save()
|
|
|
|
|
|
|
|
return flask.redirect(request.referrer)
|
|
|
|
|
|
|
|
|
2022-02-02 11:21:30 +00:00
|
|
|
class TagUnlinkDeviceView(View):
|
2022-01-25 13:39:15 +00:00
|
|
|
methods = ['POST', 'GET']
|
2022-01-25 11:53:36 +00:00
|
|
|
decorators = [login_required]
|
2022-02-02 11:21:30 +00:00
|
|
|
template_name = 'inventory/tag_unlink_device.html'
|
2022-01-25 11:53:36 +00:00
|
|
|
|
2022-01-25 13:39:15 +00:00
|
|
|
def dispatch_request(self, id):
|
|
|
|
form = TagDeviceForm(delete=True, device=id)
|
2022-01-25 11:53:36 +00:00
|
|
|
if form.validate_on_submit():
|
|
|
|
form.remove()
|
|
|
|
|
2022-01-26 11:29:03 +00:00
|
|
|
next_url = url_for('inventory.devices.devicelist')
|
|
|
|
return flask.redirect(next_url)
|
2022-01-25 11:53:36 +00:00
|
|
|
|
2022-01-25 13:39:15 +00:00
|
|
|
return flask.render_template(self.template_name, form=form, referrer=request.referrer)
|
|
|
|
|
2022-01-25 11:53:36 +00:00
|
|
|
|
2021-12-29 09:13:34 +00:00
|
|
|
devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist'))
|
2022-02-02 10:41:29 +00:00
|
|
|
devices.add_url_rule('/device/<string:id>/', view_func=DeviceDetailView.as_view('device_details'))
|
2022-01-25 09:30:46 +00:00
|
|
|
devices.add_url_rule('/lot/<string:lot_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-12 16:09:20 +00:00
|
|
|
devices.add_url_rule('/lot/add/', view_func=LotCreateView.as_view('lot_add'))
|
2022-01-03 11:28:42 +00:00
|
|
|
devices.add_url_rule('/lot/<string:id>/del/', view_func=LotDeleteView.as_view('lot_del'))
|
2022-01-12 16:09:20 +00:00
|
|
|
devices.add_url_rule('/lot/<string:id>/', view_func=LotUpdateView.as_view('lot_edit'))
|
2022-01-17 13:17:52 +00:00
|
|
|
devices.add_url_rule('/upload-snapshot/', view_func=UploadSnapshotView.as_view('upload_snapshot'))
|
2022-02-02 11:21:30 +00:00
|
|
|
devices.add_url_rule('/device/add/', view_func=DeviceCreateView.as_view('device_add'))
|
2022-01-11 12:13:57 +00:00
|
|
|
devices.add_url_rule('/tag/', view_func=TagListView.as_view('taglist'))
|
2022-01-11 12:42:01 +00:00
|
|
|
devices.add_url_rule('/tag/add/', view_func=TagAddView.as_view('tag_add'))
|
2022-01-26 12:59:56 +00:00
|
|
|
devices.add_url_rule('/tag/unnamed/add/', view_func=TagAddUnnamedView.as_view('tag_unnamed_add'))
|
2022-02-02 10:41:29 +00:00
|
|
|
devices.add_url_rule('/tag/<string:id>/', view_func=TagDetailView.as_view('tag_details'))
|
2022-02-02 11:21:30 +00:00
|
|
|
devices.add_url_rule('/tag/devices/add/', view_func=TagLinkDeviceView.as_view('tag_devices_add'))
|
|
|
|
devices.add_url_rule('/tag/devices/<int:id>/del/', view_func=TagUnlinkDeviceView.as_view('tag_devices_del'))
|