2022-02-24 13:15:58 +00:00
|
|
|
import csv
|
2022-03-04 12:32:37 +00:00
|
|
|
import logging
|
2022-02-22 10:11:53 +00:00
|
|
|
from io import StringIO
|
2022-02-24 13:15:58 +00:00
|
|
|
|
|
|
|
import flask
|
|
|
|
import flask_weasyprint
|
|
|
|
from flask import Blueprint, g, make_response, request, url_for
|
2021-12-28 11:36:02 +00:00
|
|
|
from flask.views import View
|
2022-02-03 09:50:36 +00:00
|
|
|
from flask_login import current_user, login_required
|
2022-03-04 12:32:37 +00:00
|
|
|
from requests.exceptions import ConnectionError
|
2022-03-01 13:29:25 +00:00
|
|
|
from sqlalchemy import or_
|
2022-03-08 09:05:32 +00:00
|
|
|
from werkzeug.exceptions import NotFound
|
2021-12-28 11:36:02 +00:00
|
|
|
|
2022-02-07 13:52:17 +00:00
|
|
|
from ereuse_devicehub import messages
|
2022-03-08 09:05:32 +00:00
|
|
|
from ereuse_devicehub.db import db
|
2022-02-24 13:15:58 +00:00
|
|
|
from ereuse_devicehub.inventory.forms import (
|
2022-02-25 11:27:14 +00:00
|
|
|
AllocateForm,
|
|
|
|
DataWipeForm,
|
2022-03-07 12:07:26 +00:00
|
|
|
FilterForm,
|
2022-02-25 11:27:14 +00:00
|
|
|
LotDeviceForm,
|
|
|
|
LotForm,
|
|
|
|
NewActionForm,
|
|
|
|
NewDeviceForm,
|
|
|
|
TagDeviceForm,
|
|
|
|
TagForm,
|
|
|
|
TagUnnamedForm,
|
2022-02-25 11:34:02 +00:00
|
|
|
TradeDocumentForm,
|
|
|
|
TradeForm,
|
2022-02-25 11:57:13 +00:00
|
|
|
UploadSnapshotForm,
|
2022-02-25 11:56:01 +00:00
|
|
|
)
|
2022-02-24 13:15:58 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import Trade
|
2022-02-25 11:57:13 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Computer, DataStorage, Device
|
|
|
|
from ereuse_devicehub.resources.documents.device_row import ActionRow, DeviceRow
|
2022-02-24 13:15:58 +00:00
|
|
|
from ereuse_devicehub.resources.hash_reports import insert_hash
|
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-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
|
|
|
|
2022-03-04 12:32:37 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-12-28 11:36:02 +00:00
|
|
|
|
2022-02-01 12:40:06 +00:00
|
|
|
class DeviceListMix(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-02-01 12:40:06 +00:00
|
|
|
def get_context(self, lot_id):
|
2022-03-07 12:07:26 +00:00
|
|
|
form_filter = FilterForm()
|
|
|
|
filter_types = form_filter.search()
|
2022-03-08 09:05:32 +00:00
|
|
|
lots = (
|
|
|
|
Lot.query.outerjoin(Trade)
|
|
|
|
.filter(
|
|
|
|
or_(
|
|
|
|
Trade.user_from == g.user,
|
|
|
|
Trade.user_to == g.user,
|
|
|
|
Lot.owner_id == g.user.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.distinct()
|
|
|
|
)
|
2021-12-30 20:40:44 +00:00
|
|
|
lot = None
|
2022-02-24 13:15:58 +00:00
|
|
|
tags = (
|
|
|
|
Tag.query.filter(Tag.owner_id == current_user.id)
|
2022-02-25 11:56:01 +00:00
|
|
|
.filter(Tag.device_id.is_(None))
|
2022-03-02 12:07:04 +00:00
|
|
|
.order_by(Tag.id.asc())
|
2022-02-24 13:15:58 +00:00
|
|
|
)
|
2022-01-25 11:53:36 +00:00
|
|
|
|
2022-01-25 09:30:46 +00:00
|
|
|
if lot_id:
|
|
|
|
lot = lots.filter(Lot.id == lot_id).one()
|
2022-03-08 09:15:53 +00:00
|
|
|
devices = lot.devices
|
2022-03-08 09:30:31 +00:00
|
|
|
if "All" not in filter_types:
|
2022-03-08 09:15:53 +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)
|
2022-01-10 12:07:05 +00:00
|
|
|
form_new_action = NewActionForm(lot=lot.id)
|
2022-02-02 11:52:16 +00:00
|
|
|
form_new_allocate = AllocateForm(lot=lot.id)
|
2022-02-07 13:01:56 +00:00
|
|
|
form_new_datawipe = DataWipeForm(lot=lot.id)
|
2022-02-10 12:22:47 +00:00
|
|
|
form_new_trade = TradeForm(
|
|
|
|
lot=lot.id,
|
2022-02-18 12:37:45 +00:00
|
|
|
user_to=g.user.email,
|
|
|
|
user_from=g.user.email,
|
2022-02-10 12:22:47 +00:00
|
|
|
)
|
2021-12-30 20:40:44 +00:00
|
|
|
else:
|
2022-03-08 09:15:53 +00:00
|
|
|
if "All" in filter_types:
|
|
|
|
devices = (
|
|
|
|
Device.query.filter(Device.owner_id == current_user.id)
|
|
|
|
.filter_by(lots=None)
|
|
|
|
.order_by(Device.updated.desc())
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
devices = (
|
|
|
|
Device.query.filter(Device.owner_id == current_user.id)
|
|
|
|
.filter_by(lots=None)
|
|
|
|
.filter(Device.type.in_(filter_types))
|
|
|
|
.order_by(Device.updated.desc())
|
|
|
|
)
|
|
|
|
|
2022-01-10 12:07:05 +00:00
|
|
|
form_new_action = NewActionForm()
|
2022-02-02 11:52:16 +00:00
|
|
|
form_new_allocate = AllocateForm()
|
2022-02-07 13:01:56 +00:00
|
|
|
form_new_datawipe = DataWipeForm()
|
2022-02-17 12:51:59 +00:00
|
|
|
form_new_trade = ''
|
2022-02-04 12:22:47 +00:00
|
|
|
action_devices = form_new_action.devices.data
|
|
|
|
list_devices = []
|
|
|
|
if action_devices:
|
|
|
|
list_devices.extend([int(x) for x in action_devices.split(",")])
|
|
|
|
|
2022-02-02 12:09:11 +00:00
|
|
|
self.context = {
|
|
|
|
'devices': devices,
|
|
|
|
'lots': lots,
|
|
|
|
'form_lot_device': LotDeviceForm(),
|
|
|
|
'form_tag_device': TagDeviceForm(),
|
|
|
|
'form_new_action': form_new_action,
|
|
|
|
'form_new_allocate': form_new_allocate,
|
2022-02-07 13:01:56 +00:00
|
|
|
'form_new_datawipe': form_new_datawipe,
|
2022-02-09 13:06:28 +00:00
|
|
|
'form_new_trade': form_new_trade,
|
2022-03-07 12:07:26 +00:00
|
|
|
'form_filter': form_filter,
|
2022-02-02 12:09:11 +00:00
|
|
|
'lot': lot,
|
2022-02-04 12:22:47 +00:00
|
|
|
'tags': tags,
|
2022-02-24 13:15:58 +00:00
|
|
|
'list_devices': list_devices,
|
2022-02-02 12:09:11 +00:00
|
|
|
}
|
2022-01-10 14:53:11 +00:00
|
|
|
|
2022-02-01 12:40:06 +00:00
|
|
|
return self.context
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceListView(DeviceListMix):
|
|
|
|
def dispatch_request(self, lot_id=None):
|
|
|
|
self.get_context(lot_id)
|
|
|
|
return flask.render_template(self.template_name, **self.context)
|
2021-12-30 16:23:34 +00:00
|
|
|
|
|
|
|
|
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)
|
2022-02-24 13:15:58 +00:00
|
|
|
device = (
|
|
|
|
Device.query.filter(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():
|
2022-03-07 10:37:32 +00:00
|
|
|
form.save(commit=False)
|
2022-03-02 09:36:26 +00:00
|
|
|
messages.success(
|
|
|
|
'Add devices to lot "{}" successfully!'.format(form._lot.name)
|
|
|
|
)
|
2022-03-07 10:31:38 +00:00
|
|
|
db.session.commit()
|
2022-03-02 09:36:26 +00:00
|
|
|
else:
|
|
|
|
messages.error('Error adding devices to lot!')
|
2021-12-30 20:35:54 +00:00
|
|
|
|
2022-03-02 09:36:26 +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():
|
2022-03-07 10:37:32 +00:00
|
|
|
form.remove(commit=False)
|
2022-03-02 09:36:26 +00:00
|
|
|
messages.success(
|
|
|
|
'Remove devices from lot "{}" successfully!'.format(form._lot.name)
|
|
|
|
)
|
2022-03-07 10:31:38 +00:00
|
|
|
db.session.commit()
|
2022-03-02 09:36:26 +00:00
|
|
|
else:
|
|
|
|
messages.error('Error removing devices from lot!')
|
2022-01-03 10:32:12 +00:00
|
|
|
|
2022-03-02 09:36:26 +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)
|
2022-02-25 11:25:00 +00:00
|
|
|
context = {'form': form, 'title': self.title, 'lots': lots}
|
|
|
|
return flask.render_template(self.template_name, **context)
|
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)
|
2022-02-25 11:25:00 +00:00
|
|
|
context = {'form': form, 'title': self.title, 'lots': lots}
|
|
|
|
return flask.render_template(self.template_name, **context)
|
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)
|
2022-03-07 09:20:18 +00:00
|
|
|
if form.instance.trade:
|
|
|
|
msg = "Sorry, the lot cannot be deleted because have a trade action "
|
2022-03-04 17:30:14 +00:00
|
|
|
messages.error(msg)
|
|
|
|
next_url = url_for('inventory.devices.lotdevicelist', lot_id=id)
|
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2022-01-03 11:28:42 +00:00
|
|
|
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-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()
|
2022-02-25 11:25:00 +00:00
|
|
|
context = {'page_title': 'Upload Snapshot', 'lots': lots, 'form': form}
|
2022-01-17 13:17:52 +00:00
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save()
|
|
|
|
|
2022-02-25 11:25:00 +00:00
|
|
|
return flask.render_template(self.template_name, **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):
|
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id).all()
|
|
|
|
form = NewDeviceForm()
|
2022-02-25 11:25:00 +00:00
|
|
|
context = {'page_title': 'New Device', 'lots': lots, 'form': form}
|
2022-01-19 12:40:40 +00:00
|
|
|
if form.validate_on_submit():
|
|
|
|
form.save()
|
|
|
|
next_url = url_for('inventory.devices.devicelist')
|
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2022-02-25 11:25:00 +00:00
|
|
|
return flask.render_template(self.template_name, **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-25 11:25:00 +00:00
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
2022-03-02 12:07:04 +00:00
|
|
|
tags = Tag.query.filter(Tag.owner_id == current_user.id).order_by(Tag.id)
|
2022-02-02 10:41:29 +00:00
|
|
|
context = {
|
2022-02-25 11:25:00 +00:00
|
|
|
'lots': lots,
|
2022-02-02 10:41:29 +00:00
|
|
|
'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-25 11:25:00 +00:00
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
|
|
|
context = {'page_title': 'New Tag', 'lots': lots}
|
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-25 11:25:00 +00:00
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
|
|
|
context = {'page_title': 'New Unnamed Tag', 'lots': lots}
|
2022-01-26 12:16:18 +00:00
|
|
|
form = TagUnnamedForm()
|
|
|
|
if form.validate_on_submit():
|
2022-03-04 12:32:37 +00:00
|
|
|
try:
|
|
|
|
form.save()
|
|
|
|
except ConnectionError as e:
|
2022-03-08 09:05:32 +00:00
|
|
|
logger.error(
|
|
|
|
"Error while trying to connect to tag server: {}".format(e)
|
|
|
|
)
|
|
|
|
msg = (
|
|
|
|
"Sorry, we cannot create the unnamed tags requested because "
|
|
|
|
"some error happens while connecting to the tag server!"
|
|
|
|
)
|
2022-02-28 10:46:31 +00:00
|
|
|
messages.error(msg)
|
2022-03-04 12:32:37 +00:00
|
|
|
|
2022-01-26 12:16:18 +00:00
|
|
|
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)
|
2022-02-24 13:15:58 +00:00
|
|
|
tag = (
|
|
|
|
Tag.query.filter(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):
|
2022-02-25 11:25:00 +00:00
|
|
|
lots = Lot.query.filter(Lot.owner_id == current_user.id)
|
2022-01-25 13:39:15 +00:00
|
|
|
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-02-24 13:15:58 +00:00
|
|
|
return flask.render_template(
|
2022-02-25 11:27:14 +00:00
|
|
|
self.template_name, form=form, lots=lots, referrer=request.referrer
|
2022-02-24 13:15:58 +00:00
|
|
|
)
|
2022-01-19 12:40:40 +00:00
|
|
|
|
|
|
|
|
2022-01-05 15:00:13 +00:00
|
|
|
class NewActionView(View):
|
|
|
|
methods = ['POST']
|
|
|
|
decorators = [login_required]
|
2022-02-08 10:52:30 +00:00
|
|
|
form_class = NewActionForm
|
2022-01-05 15:00:13 +00:00
|
|
|
|
|
|
|
def dispatch_request(self):
|
2022-02-08 10:52:30 +00:00
|
|
|
self.form = self.form_class()
|
2022-02-04 11:10:27 +00:00
|
|
|
|
2022-02-01 12:40:06 +00:00
|
|
|
if self.form.validate_on_submit():
|
2022-02-24 13:15:58 +00:00
|
|
|
self.form.save()
|
|
|
|
messages.success(
|
|
|
|
'Action "{}" created successfully!'.format(self.form.type.data)
|
|
|
|
)
|
2022-02-08 10:49:13 +00:00
|
|
|
|
|
|
|
next_url = self.get_next_url()
|
2022-01-05 15:00:13 +00:00
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2022-02-08 10:49:13 +00:00
|
|
|
def get_next_url(self):
|
2022-02-04 11:10:27 +00:00
|
|
|
lot_id = self.form.lot.data
|
2022-02-08 10:49:13 +00:00
|
|
|
|
2022-02-04 11:10:27 +00:00
|
|
|
if lot_id:
|
2022-02-08 10:49:13 +00:00
|
|
|
return url_for('inventory.devices.lotdevicelist', lot_id=lot_id)
|
2022-02-04 11:10:27 +00:00
|
|
|
|
2022-02-08 10:49:13 +00:00
|
|
|
return url_for('inventory.devices.devicelist')
|
2022-01-05 15:00:13 +00:00
|
|
|
|
2022-01-03 11:28:42 +00:00
|
|
|
|
2022-02-01 12:40:06 +00:00
|
|
|
class NewAllocateView(NewActionView, DeviceListMix):
|
|
|
|
methods = ['POST']
|
2022-02-08 10:52:30 +00:00
|
|
|
form_class = AllocateForm
|
2022-02-01 12:40:06 +00:00
|
|
|
|
2022-02-07 13:01:56 +00:00
|
|
|
def dispatch_request(self):
|
2022-02-08 10:52:30 +00:00
|
|
|
self.form = self.form_class()
|
2022-02-04 11:10:27 +00:00
|
|
|
|
2022-02-03 09:14:10 +00:00
|
|
|
if self.form.validate_on_submit():
|
2022-02-24 13:15:58 +00:00
|
|
|
self.form.save()
|
|
|
|
messages.success(
|
|
|
|
'Action "{}" created successfully!'.format(self.form.type.data)
|
|
|
|
)
|
2022-02-03 09:14:10 +00:00
|
|
|
|
2022-02-08 10:49:13 +00:00
|
|
|
next_url = self.get_next_url()
|
2022-02-03 09:14:10 +00:00
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2022-02-08 10:49:13 +00:00
|
|
|
lot_id = self.form.lot.data
|
2022-02-03 09:14:10 +00:00
|
|
|
self.get_context(lot_id)
|
2022-02-02 13:17:42 +00:00
|
|
|
self.context['form_new_allocate'] = self.form
|
2022-02-01 12:40:06 +00:00
|
|
|
return flask.render_template(self.template_name, **self.context)
|
|
|
|
|
|
|
|
|
2022-02-07 13:01:56 +00:00
|
|
|
class NewDataWipeView(NewActionView, DeviceListMix):
|
|
|
|
methods = ['POST']
|
2022-02-09 10:27:08 +00:00
|
|
|
form_class = DataWipeForm
|
2022-02-07 13:01:56 +00:00
|
|
|
|
|
|
|
def dispatch_request(self):
|
2022-02-09 10:27:08 +00:00
|
|
|
self.form = self.form_class()
|
|
|
|
|
|
|
|
if self.form.validate_on_submit():
|
2022-02-24 13:15:58 +00:00
|
|
|
self.form.save()
|
|
|
|
messages.success(
|
|
|
|
'Action "{}" created successfully!'.format(self.form.type.data)
|
|
|
|
)
|
2022-02-09 10:27:08 +00:00
|
|
|
|
|
|
|
next_url = self.get_next_url()
|
|
|
|
return flask.redirect(next_url)
|
2022-02-07 13:01:56 +00:00
|
|
|
|
|
|
|
lot_id = self.form.lot.data
|
|
|
|
self.get_context(lot_id)
|
|
|
|
self.context['form_new_datawipe'] = self.form
|
|
|
|
return flask.render_template(self.template_name, **self.context)
|
|
|
|
|
|
|
|
|
2022-02-09 13:06:28 +00:00
|
|
|
class NewTradeView(NewActionView, DeviceListMix):
|
|
|
|
methods = ['POST']
|
|
|
|
form_class = TradeForm
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
self.form = self.form_class()
|
|
|
|
|
|
|
|
if self.form.validate_on_submit():
|
2022-02-24 13:15:58 +00:00
|
|
|
self.form.save()
|
|
|
|
messages.success(
|
|
|
|
'Action "{}" created successfully!'.format(self.form.type.data)
|
|
|
|
)
|
2022-02-09 13:06:28 +00:00
|
|
|
|
|
|
|
next_url = self.get_next_url()
|
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
|
|
|
lot_id = self.form.lot.data
|
|
|
|
self.get_context(lot_id)
|
2022-02-18 12:37:45 +00:00
|
|
|
self.context['form_new_trade'] = self.form
|
2022-02-09 13:06:28 +00:00
|
|
|
return flask.render_template(self.template_name, **self.context)
|
|
|
|
|
|
|
|
|
2022-02-21 12:03:03 +00:00
|
|
|
class NewTradeDocumentView(View):
|
|
|
|
methods = ['POST', 'GET']
|
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'inventory/trade_document.html'
|
|
|
|
form_class = TradeDocumentForm
|
|
|
|
title = "Add new document"
|
|
|
|
|
|
|
|
def dispatch_request(self, lot_id):
|
|
|
|
self.form = self.form_class(lot=lot_id)
|
|
|
|
|
|
|
|
if self.form.validate_on_submit():
|
|
|
|
self.form.save()
|
|
|
|
messages.success('Document created successfully!')
|
|
|
|
next_url = url_for('inventory.devices.lotdevicelist', lot_id=lot_id)
|
|
|
|
return flask.redirect(next_url)
|
|
|
|
|
2022-02-24 13:15:58 +00:00
|
|
|
return flask.render_template(
|
|
|
|
self.template_name, form=self.form, title=self.title
|
|
|
|
)
|
2022-02-21 12:03:03 +00:00
|
|
|
|
|
|
|
|
2022-02-22 10:11:53 +00:00
|
|
|
class ExportsView(View):
|
|
|
|
methods = ['GET']
|
|
|
|
decorators = [login_required]
|
|
|
|
|
2022-02-24 13:15:58 +00:00
|
|
|
def dispatch_request(self, export_id):
|
2022-02-22 10:11:53 +00:00
|
|
|
export_ids = {
|
2022-02-24 13:15:58 +00:00
|
|
|
'metrics': self.metrics,
|
|
|
|
'devices': self.devices_list,
|
|
|
|
'certificates': self.erasure,
|
|
|
|
'links': self.public_links,
|
2022-02-22 10:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if export_id not in export_ids:
|
|
|
|
return NotFound()
|
|
|
|
return export_ids[export_id]()
|
|
|
|
|
2022-02-24 13:15:58 +00:00
|
|
|
def find_devices(self):
|
|
|
|
args = request.args.get('ids')
|
|
|
|
ids = args.split(',') if args else []
|
|
|
|
query = Device.query.filter(Device.owner == g.user)
|
|
|
|
return query.filter(Device.devicehub_id.in_(ids))
|
|
|
|
|
|
|
|
def response_csv(self, data, name):
|
2022-02-22 10:11:53 +00:00
|
|
|
bfile = data.getvalue().encode('utf-8')
|
2022-02-24 13:15:58 +00:00
|
|
|
# insert proof
|
|
|
|
insert_hash(bfile)
|
2022-02-22 10:11:53 +00:00
|
|
|
output = make_response(bfile)
|
2022-02-24 13:15:58 +00:00
|
|
|
output.headers['Content-Disposition'] = 'attachment; filename={}'.format(name)
|
2022-02-22 10:11:53 +00:00
|
|
|
output.headers['Content-type'] = 'text/csv'
|
|
|
|
return output
|
|
|
|
|
2022-02-24 13:15:58 +00:00
|
|
|
def devices_list(self):
|
|
|
|
"""Get device query and put information in csv format."""
|
|
|
|
data = StringIO()
|
|
|
|
cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"')
|
|
|
|
first = True
|
|
|
|
|
|
|
|
for device in self.find_devices():
|
|
|
|
d = DeviceRow(device, {})
|
|
|
|
if first:
|
|
|
|
cw.writerow(d.keys())
|
|
|
|
first = False
|
|
|
|
cw.writerow(d.values())
|
|
|
|
|
|
|
|
return self.response_csv(data, "export.csv")
|
|
|
|
|
|
|
|
def metrics(self):
|
|
|
|
"""Get device query and put information in csv format."""
|
|
|
|
data = StringIO()
|
|
|
|
cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"')
|
|
|
|
first = True
|
|
|
|
devs_id = []
|
|
|
|
# Get the allocate info
|
|
|
|
for device in self.find_devices():
|
|
|
|
devs_id.append(device.id)
|
|
|
|
for allocate in device.get_metrics():
|
|
|
|
d = ActionRow(allocate)
|
|
|
|
if first:
|
|
|
|
cw.writerow(d.keys())
|
|
|
|
first = False
|
|
|
|
cw.writerow(d.values())
|
|
|
|
|
|
|
|
# Get the trade info
|
|
|
|
query_trade = Trade.query.filter(
|
|
|
|
Trade.devices.any(Device.id.in_(devs_id))
|
|
|
|
).all()
|
|
|
|
|
|
|
|
lot_id = request.args.get('lot')
|
|
|
|
if lot_id and not query_trade:
|
|
|
|
lot = Lot.query.filter_by(id=lot_id).one()
|
|
|
|
if hasattr(lot, "trade") and lot.trade:
|
|
|
|
if g.user in [lot.trade.user_from, lot.trade.user_to]:
|
|
|
|
query_trade = [lot.trade]
|
|
|
|
|
|
|
|
for trade in query_trade:
|
|
|
|
data_rows = trade.get_metrics()
|
|
|
|
for row in data_rows:
|
|
|
|
d = ActionRow(row)
|
|
|
|
if first:
|
|
|
|
cw.writerow(d.keys())
|
|
|
|
first = False
|
|
|
|
cw.writerow(d.values())
|
|
|
|
|
|
|
|
return self.response_csv(data, "actions_export.csv")
|
|
|
|
|
|
|
|
def public_links(self):
|
|
|
|
# get a csv with the publink links of this devices
|
|
|
|
data = StringIO()
|
|
|
|
cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"')
|
|
|
|
cw.writerow(['links'])
|
|
|
|
host_url = request.host_url
|
|
|
|
for dev in self.find_devices():
|
|
|
|
code = dev.devicehub_id
|
|
|
|
link = [f"{host_url}devices/{code}"]
|
|
|
|
cw.writerow(link)
|
|
|
|
|
|
|
|
return self.response_csv(data, "links.csv")
|
|
|
|
|
|
|
|
def erasure(self):
|
|
|
|
template = self.build_erasure_certificate()
|
|
|
|
res = flask_weasyprint.render_pdf(
|
|
|
|
flask_weasyprint.HTML(string=template),
|
|
|
|
download_filename='erasure-certificate.pdf',
|
|
|
|
)
|
|
|
|
insert_hash(res.data)
|
|
|
|
return res
|
|
|
|
|
|
|
|
def build_erasure_certificate(self):
|
|
|
|
erasures = []
|
|
|
|
for device in self.find_devices():
|
|
|
|
if isinstance(device, Computer):
|
|
|
|
for privacy in device.privacy:
|
|
|
|
erasures.append(privacy)
|
|
|
|
elif isinstance(device, DataStorage):
|
|
|
|
if device.privacy:
|
|
|
|
erasures.append(device.privacy)
|
|
|
|
|
|
|
|
params = {
|
|
|
|
'title': 'Erasure Certificate',
|
|
|
|
'erasures': tuple(erasures),
|
|
|
|
'url_pdf': '',
|
|
|
|
}
|
|
|
|
return flask.render_template('inventory/erasure.html', **params)
|
|
|
|
|
2022-02-22 10:11:53 +00:00
|
|
|
|
2022-01-05 15:00:13 +00:00
|
|
|
devices.add_url_rule('/action/add/', view_func=NewActionView.as_view('action_add'))
|
2022-02-09 13:06:28 +00:00
|
|
|
devices.add_url_rule('/action/trade/add/', view_func=NewTradeView.as_view('trade_add'))
|
2022-02-24 13:15:58 +00:00
|
|
|
devices.add_url_rule(
|
|
|
|
'/action/allocate/add/', view_func=NewAllocateView.as_view('allocate_add')
|
|
|
|
)
|
|
|
|
devices.add_url_rule(
|
|
|
|
'/action/datawipe/add/', view_func=NewDataWipeView.as_view('datawipe_add')
|
|
|
|
)
|
|
|
|
devices.add_url_rule(
|
|
|
|
'/lot/<string:lot_id>/trade-document/add/',
|
|
|
|
view_func=NewTradeDocumentView.as_view('trade_document_add'),
|
|
|
|
)
|
2021-12-29 09:13:34 +00:00
|
|
|
devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist'))
|
2022-02-24 13:15:58 +00:00
|
|
|
devices.add_url_rule(
|
|
|
|
'/device/<string:id>/', view_func=DeviceDetailView.as_view('device_details')
|
|
|
|
)
|
|
|
|
devices.add_url_rule(
|
|
|
|
'/lot/<string:lot_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/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-02-24 13:15:58 +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-02-24 13:15:58 +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-02-24 13:15:58 +00:00
|
|
|
devices.add_url_rule(
|
|
|
|
'/tag/unnamed/add/', view_func=TagAddUnnamedView.as_view('tag_unnamed_add')
|
|
|
|
)
|
|
|
|
devices.add_url_rule(
|
|
|
|
'/tag/<string:id>/', view_func=TagDetailView.as_view('tag_details')
|
|
|
|
)
|
|
|
|
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'),
|
|
|
|
)
|
|
|
|
devices.add_url_rule(
|
|
|
|
'/export/<string:export_id>/', view_func=ExportsView.as_view('export')
|
|
|
|
)
|