2019-02-28 17:21:24 +00:00
|
|
|
import csv
|
|
|
|
import datetime
|
2018-11-21 13:26:56 +00:00
|
|
|
import enum
|
|
|
|
import uuid
|
2020-07-28 14:16:17 +00:00
|
|
|
from collections import OrderedDict
|
2019-02-28 17:21:24 +00:00
|
|
|
from io import StringIO
|
2018-11-21 13:26:56 +00:00
|
|
|
from typing import Callable, Iterable, Tuple
|
|
|
|
|
|
|
|
import boltons
|
|
|
|
import flask
|
|
|
|
import flask_weasyprint
|
|
|
|
import teal.marshmallow
|
|
|
|
from boltons import urlutils
|
2020-12-21 12:40:07 +00:00
|
|
|
from flask import make_response, g, request
|
|
|
|
from flask.json import jsonify
|
2019-02-28 17:21:24 +00:00
|
|
|
from teal.cache import cache
|
2020-12-21 12:40:07 +00:00
|
|
|
from teal.resource import Resource, View
|
2018-11-21 13:26:56 +00:00
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
2019-05-11 14:27:22 +00:00
|
|
|
from ereuse_devicehub.resources.action import models as evs
|
2018-11-21 13:26:56 +00:00
|
|
|
from ereuse_devicehub.resources.device import models as devs
|
|
|
|
from ereuse_devicehub.resources.device.views import DeviceView
|
2021-01-08 16:37:52 +00:00
|
|
|
from ereuse_devicehub.resources.documents.device_row import DeviceRow, StockRow, ActionRow
|
2020-07-28 14:16:17 +00:00
|
|
|
from ereuse_devicehub.resources.lot import LotView
|
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
2020-12-21 12:40:07 +00:00
|
|
|
from ereuse_devicehub.resources.hash_reports import insert_hash, ReportHash
|
2018-11-21 13:26:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Format(enum.Enum):
|
|
|
|
HTML = 'HTML'
|
|
|
|
PDF = 'PDF'
|
|
|
|
|
|
|
|
|
|
|
|
class DocumentView(DeviceView):
|
|
|
|
class FindArgs(DeviceView.FindArgs):
|
|
|
|
format = teal.marshmallow.EnumField(Format, missing=None)
|
|
|
|
|
|
|
|
def get(self, id):
|
|
|
|
"""Get a collection of resources or a specific one.
|
|
|
|
---
|
|
|
|
parameters:
|
|
|
|
- name: id
|
|
|
|
in: path
|
|
|
|
description: The identifier of the resource.
|
|
|
|
type: string
|
|
|
|
required: false
|
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: Return the collection or the specific one.
|
|
|
|
"""
|
|
|
|
args = self.QUERY_PARSER.parse(self.find_args,
|
|
|
|
flask.request,
|
|
|
|
locations=('querystring',))
|
|
|
|
if id:
|
2019-05-11 14:27:22 +00:00
|
|
|
# todo we assume we can pass both device id and action id
|
2018-11-21 13:26:56 +00:00
|
|
|
# for certificates... how is it going to end up being?
|
|
|
|
try:
|
|
|
|
id = uuid.UUID(id)
|
|
|
|
except ValueError:
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
raise teal.marshmallow.ValidationError('Document must be an ID or UUID.')
|
|
|
|
else:
|
|
|
|
query = devs.Device.query.filter_by(id=id)
|
|
|
|
else:
|
2019-05-11 14:27:22 +00:00
|
|
|
query = evs.Action.query.filter_by(id=id)
|
2018-11-21 13:26:56 +00:00
|
|
|
else:
|
|
|
|
flask.current_app.auth.requires_auth(lambda: None)() # todo not nice
|
|
|
|
query = self.query(args)
|
|
|
|
|
|
|
|
type = urlutils.URL(flask.request.url).path_parts[-2]
|
|
|
|
if type == 'erasures':
|
|
|
|
template = self.erasure(query)
|
|
|
|
if args.get('format') == Format.PDF:
|
|
|
|
res = flask_weasyprint.render_pdf(
|
|
|
|
flask_weasyprint.HTML(string=template), download_filename='{}.pdf'.format(type)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
res = flask.make_response(template)
|
|
|
|
return res
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def erasure(query: db.Query):
|
|
|
|
def erasures():
|
|
|
|
for model in query:
|
|
|
|
if isinstance(model, devs.Computer):
|
|
|
|
for erasure in model.privacy:
|
|
|
|
yield erasure
|
|
|
|
elif isinstance(model, devs.DataStorage):
|
|
|
|
erasure = model.privacy
|
|
|
|
if erasure:
|
|
|
|
yield erasure
|
|
|
|
else:
|
|
|
|
assert isinstance(model, evs.EraseBasic)
|
|
|
|
yield model
|
|
|
|
|
|
|
|
url_pdf = boltons.urlutils.URL(flask.request.url)
|
|
|
|
url_pdf.query_params['format'] = 'PDF'
|
|
|
|
url_web = boltons.urlutils.URL(flask.request.url)
|
|
|
|
url_web.query_params['format'] = 'HTML'
|
|
|
|
params = {
|
|
|
|
'title': 'Erasure Certificate',
|
|
|
|
'erasures': tuple(erasures()),
|
|
|
|
'url_pdf': url_pdf.to_text(),
|
|
|
|
'url_web': url_web.to_text()
|
|
|
|
}
|
|
|
|
return flask.render_template('documents/erasure.html', **params)
|
|
|
|
|
|
|
|
|
2019-02-28 17:21:24 +00:00
|
|
|
class DevicesDocumentView(DeviceView):
|
|
|
|
@cache(datetime.timedelta(minutes=1))
|
|
|
|
def find(self, args: dict):
|
2020-08-17 13:57:27 +00:00
|
|
|
query = (x for x in self.query(args) if x.owner_id == g.user.id)
|
2019-02-28 17:21:24 +00:00
|
|
|
return self.generate_post_csv(query)
|
|
|
|
|
|
|
|
def generate_post_csv(self, query):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Get device query and put information in csv format."""
|
2019-02-28 17:21:24 +00:00
|
|
|
data = StringIO()
|
2020-12-21 15:09:30 +00:00
|
|
|
cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"')
|
2019-02-28 17:21:24 +00:00
|
|
|
first = True
|
|
|
|
for device in query:
|
|
|
|
d = DeviceRow(device)
|
|
|
|
if first:
|
2019-05-08 17:12:05 +00:00
|
|
|
cw.writerow(d.keys())
|
2019-02-28 17:21:24 +00:00
|
|
|
first = False
|
2019-05-08 17:12:05 +00:00
|
|
|
cw.writerow(d.values())
|
2020-12-21 10:34:03 +00:00
|
|
|
bfile = data.getvalue().encode('utf-8')
|
|
|
|
output = make_response(bfile)
|
2020-12-21 15:09:30 +00:00
|
|
|
insert_hash(bfile)
|
2019-02-28 17:21:24 +00:00
|
|
|
output.headers['Content-Disposition'] = 'attachment; filename=export.csv'
|
|
|
|
output.headers['Content-type'] = 'text/csv'
|
|
|
|
return output
|
|
|
|
|
2020-08-18 17:19:18 +00:00
|
|
|
|
2021-01-08 16:37:52 +00:00
|
|
|
class ActionsDocumentView(DeviceView):
|
|
|
|
@cache(datetime.timedelta(minutes=1))
|
|
|
|
def find(self, args: dict):
|
|
|
|
query = (x for x in self.query(args) if x.owner_id == g.user.id)
|
|
|
|
return self.generate_post_csv(query)
|
|
|
|
|
|
|
|
def generate_post_csv(self, query):
|
|
|
|
"""Get device query and put information in csv format."""
|
|
|
|
data = StringIO()
|
|
|
|
cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"')
|
|
|
|
first = True
|
|
|
|
for device in query:
|
2021-01-13 17:11:41 +00:00
|
|
|
for allocate in device.get_metrics():
|
|
|
|
d = ActionRow(allocate)
|
2021-01-08 16:37:52 +00:00
|
|
|
if first:
|
|
|
|
cw.writerow(d.keys())
|
|
|
|
first = False
|
|
|
|
cw.writerow(d.values())
|
|
|
|
bfile = data.getvalue().encode('utf-8')
|
|
|
|
output = make_response(bfile)
|
|
|
|
insert_hash(bfile)
|
|
|
|
output.headers['Content-Disposition'] = 'attachment; filename=actions_export.csv'
|
|
|
|
output.headers['Content-type'] = 'text/csv'
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2020-07-28 14:16:17 +00:00
|
|
|
class LotsDocumentView(LotView):
|
|
|
|
def find(self, args: dict):
|
2020-08-18 19:02:54 +00:00
|
|
|
query = (x for x in self.query(args) if x.owner_id == g.user.id)
|
2020-08-03 16:25:55 +00:00
|
|
|
return self.generate_lots_csv(query)
|
2020-07-28 14:16:17 +00:00
|
|
|
|
|
|
|
def generate_lots_csv(self, query):
|
2020-07-28 14:22:17 +00:00
|
|
|
"""Get lot query and put information in csv format."""
|
2020-07-28 14:16:17 +00:00
|
|
|
data = StringIO()
|
|
|
|
cw = csv.writer(data)
|
|
|
|
first = True
|
|
|
|
for lot in query:
|
|
|
|
l = LotRow(lot)
|
|
|
|
if first:
|
|
|
|
cw.writerow(l.keys())
|
|
|
|
first = False
|
|
|
|
cw.writerow(l.values())
|
|
|
|
output = make_response(data.getvalue())
|
|
|
|
output.headers['Content-Disposition'] = 'attachment; filename=lots-info.csv'
|
|
|
|
output.headers['Content-type'] = 'text/csv'
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
class LotRow(OrderedDict):
|
|
|
|
def __init__(self, lot: Lot) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.lot = lot
|
2020-07-28 14:22:17 +00:00
|
|
|
# General information about lot
|
2020-07-28 14:16:17 +00:00
|
|
|
self['Id'] = lot.id.hex
|
|
|
|
self['Name'] = lot.name
|
|
|
|
self['Registered in'] = format(lot.created, '%c')
|
|
|
|
try:
|
|
|
|
self['Description'] = lot.description
|
|
|
|
except:
|
|
|
|
self['Description'] = ''
|
|
|
|
|
|
|
|
|
2020-07-23 13:56:51 +00:00
|
|
|
class StockDocumentView(DeviceView):
|
|
|
|
# @cache(datetime.timedelta(minutes=1))
|
|
|
|
def find(self, args: dict):
|
2020-08-17 09:18:52 +00:00
|
|
|
query = (x for x in self.query(args) if x.owner_id == g.user.id)
|
2020-07-23 13:56:51 +00:00
|
|
|
return self.generate_post_csv(query)
|
|
|
|
|
|
|
|
def generate_post_csv(self, query):
|
|
|
|
"""Get device query and put information in csv format."""
|
|
|
|
data = StringIO()
|
|
|
|
cw = csv.writer(data)
|
|
|
|
first = True
|
|
|
|
for device in query:
|
2020-08-05 09:56:59 +00:00
|
|
|
d = StockRow(device)
|
2020-07-23 13:56:51 +00:00
|
|
|
if first:
|
|
|
|
cw.writerow(d.keys())
|
|
|
|
first = False
|
|
|
|
cw.writerow(d.values())
|
|
|
|
output = make_response(data.getvalue())
|
2020-08-05 09:49:18 +00:00
|
|
|
output.headers['Content-Disposition'] = 'attachment; filename=devices-stock.csv'
|
2020-07-23 13:56:51 +00:00
|
|
|
output.headers['Content-type'] = 'text/csv'
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2020-12-21 12:40:07 +00:00
|
|
|
class CheckView(View):
|
|
|
|
model = ReportHash
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
qry = dict(request.values)
|
2020-12-21 15:09:30 +00:00
|
|
|
hash3 = qry.get('hash')
|
2020-12-21 12:40:07 +00:00
|
|
|
|
|
|
|
result = False
|
2020-12-21 15:09:30 +00:00
|
|
|
if hash3 and ReportHash.query.filter_by(hash3=hash3).count():
|
2020-12-21 12:40:07 +00:00
|
|
|
result = True
|
|
|
|
return jsonify(result)
|
|
|
|
|
|
|
|
|
2021-01-18 11:40:31 +00:00
|
|
|
class StampsView(View):
|
|
|
|
"""
|
|
|
|
This view render one public ans static page for see the links for to do the check
|
|
|
|
of one csv file
|
|
|
|
"""
|
|
|
|
def get(self):
|
|
|
|
return flask.render_template('documents/stamp.html')
|
|
|
|
|
|
|
|
|
2018-11-21 13:26:56 +00:00
|
|
|
class DocumentDef(Resource):
|
|
|
|
__type__ = 'Document'
|
|
|
|
SCHEMA = None
|
|
|
|
VIEW = None # We do not want to create default / documents endpoint
|
|
|
|
AUTH = False
|
2020-08-17 14:45:18 +00:00
|
|
|
|
2018-11-21 13:26:56 +00:00
|
|
|
def __init__(self, app,
|
|
|
|
import_name=__name__,
|
|
|
|
static_folder='static',
|
|
|
|
static_url_path=None,
|
|
|
|
template_folder='templates',
|
|
|
|
url_prefix=None,
|
|
|
|
subdomain=None,
|
|
|
|
url_defaults=None,
|
|
|
|
root_path=None,
|
|
|
|
cli_commands: Iterable[Tuple[Callable, str or None]] = tuple()):
|
|
|
|
super().__init__(app, import_name, static_folder, static_url_path, template_folder,
|
|
|
|
url_prefix, subdomain, url_defaults, root_path, cli_commands)
|
|
|
|
d = {'id': None}
|
|
|
|
get = {'GET'}
|
|
|
|
|
|
|
|
view = DocumentView.as_view('main', definition=self, auth=app.auth)
|
2020-08-05 15:51:13 +00:00
|
|
|
|
2020-08-06 13:55:14 +00:00
|
|
|
# TODO @cayop This two lines never pass
|
2018-11-21 13:26:56 +00:00
|
|
|
if self.AUTH:
|
|
|
|
view = app.auth.requires_auth(view)
|
2020-08-06 13:55:14 +00:00
|
|
|
|
2018-11-21 13:26:56 +00:00
|
|
|
self.add_url_rule('/erasures/', defaults=d, view_func=view, methods=get)
|
|
|
|
self.add_url_rule('/erasures/<{}:{}>'.format(self.ID_CONVERTER.value, self.ID_NAME),
|
|
|
|
view_func=view, methods=get)
|
2020-08-06 13:55:14 +00:00
|
|
|
|
2019-02-28 17:21:24 +00:00
|
|
|
devices_view = DevicesDocumentView.as_view('devicesDocumentView',
|
|
|
|
definition=self,
|
|
|
|
auth=app.auth)
|
2020-08-06 13:55:14 +00:00
|
|
|
devices_view = app.auth.requires_auth(devices_view)
|
2020-07-23 18:55:27 +00:00
|
|
|
|
|
|
|
stock_view = StockDocumentView.as_view('stockDocumentView', definition=self)
|
2020-08-06 14:51:49 +00:00
|
|
|
stock_view = app.auth.requires_auth(stock_view)
|
2020-07-23 18:55:27 +00:00
|
|
|
|
2019-02-28 17:21:24 +00:00
|
|
|
self.add_url_rule('/devices/', defaults=d, view_func=devices_view, methods=get)
|
2020-08-06 13:55:14 +00:00
|
|
|
|
2020-08-18 09:23:50 +00:00
|
|
|
lots_view = LotsDocumentView.as_view('lotsDocumentView', definition=self)
|
|
|
|
lots_view = app.auth.requires_auth(lots_view)
|
2020-07-28 14:16:17 +00:00
|
|
|
self.add_url_rule('/lots/', defaults=d, view_func=lots_view, methods=get)
|
2020-08-18 09:23:50 +00:00
|
|
|
|
2020-08-06 13:55:14 +00:00
|
|
|
stock_view = StockDocumentView.as_view('stockDocumentView', definition=self, auth=app.auth)
|
|
|
|
stock_view = app.auth.requires_auth(stock_view)
|
2020-07-23 13:56:51 +00:00
|
|
|
self.add_url_rule('/stock/', defaults=d, view_func=stock_view, methods=get)
|
2020-12-21 12:40:07 +00:00
|
|
|
|
|
|
|
check_view = CheckView.as_view('CheckView', definition=self, auth=app.auth)
|
|
|
|
self.add_url_rule('/check/', defaults={}, view_func=check_view, methods=get)
|
2021-01-08 16:37:52 +00:00
|
|
|
|
2021-01-18 11:40:31 +00:00
|
|
|
stamps_view = StampsView.as_view('StampsView', definition=self, auth=app.auth)
|
|
|
|
self.add_url_rule('/stamps/', defaults={}, view_func=stamps_view, methods=get)
|
|
|
|
|
2021-01-08 16:37:52 +00:00
|
|
|
actions_view = ActionsDocumentView.as_view('ActionsDocumentView',
|
|
|
|
definition=self,
|
|
|
|
auth=app.auth)
|
|
|
|
actions_view = app.auth.requires_auth(actions_view)
|
|
|
|
self.add_url_rule('/actions/', defaults=d, view_func=actions_view, methods=get)
|