2022-04-06 18:23:08 +00:00
|
|
|
import json
|
2022-04-06 17:43:35 +00:00
|
|
|
from binascii import Error as asciiError
|
|
|
|
|
2022-04-06 18:23:08 +00:00
|
|
|
from flask import Blueprint
|
|
|
|
from flask import current_app as app
|
2022-04-11 09:24:25 +00:00
|
|
|
from flask import g, jsonify, request
|
2022-04-06 17:43:35 +00:00
|
|
|
from flask.views import View
|
2022-04-06 18:23:08 +00:00
|
|
|
from marshmallow import ValidationError
|
2022-04-06 17:43:35 +00:00
|
|
|
from werkzeug.exceptions import Unauthorized
|
|
|
|
|
|
|
|
from ereuse_devicehub.auth import Auth
|
2022-04-06 18:23:08 +00:00
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.parser.models import SnapshotErrors
|
|
|
|
from ereuse_devicehub.parser.parser import ParseSnapshotLsHw
|
|
|
|
from ereuse_devicehub.parser.schemas import Snapshot_lite
|
2022-04-07 19:04:05 +00:00
|
|
|
from ereuse_devicehub.resources.action.views.snapshot import (
|
|
|
|
SnapshotMix,
|
|
|
|
move_json,
|
|
|
|
save_json,
|
|
|
|
)
|
|
|
|
from ereuse_devicehub.resources.enums import Severity
|
2022-04-06 17:43:35 +00:00
|
|
|
|
|
|
|
api = Blueprint('api', __name__, url_prefix='/api')
|
|
|
|
|
|
|
|
|
|
|
|
class LoginMix(View):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.authenticate()
|
|
|
|
|
|
|
|
def authenticate(self):
|
|
|
|
unauthorized = Unauthorized('Provide a suitable token.')
|
|
|
|
basic_token = request.headers.get('Authorization', " ").split(" ")
|
|
|
|
if not len(basic_token) == 2:
|
|
|
|
raise unauthorized
|
|
|
|
|
|
|
|
token = basic_token[1]
|
|
|
|
try:
|
|
|
|
token = Auth.decode(token)
|
|
|
|
except asciiError:
|
|
|
|
raise unauthorized
|
|
|
|
self.user = Auth().authenticate(token)
|
2022-04-06 18:23:08 +00:00
|
|
|
g.user = self.user
|
2022-04-06 17:43:35 +00:00
|
|
|
|
|
|
|
|
2022-04-07 19:04:05 +00:00
|
|
|
class InventoryView(LoginMix, SnapshotMix):
|
2022-04-06 17:43:35 +00:00
|
|
|
methods = ['POST']
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
2022-04-06 18:23:08 +00:00
|
|
|
snapshot_json = json.loads(request.data)
|
|
|
|
self.tmp_snapshots = app.config['TMP_SNAPSHOTS']
|
|
|
|
self.path_snapshot = save_json(snapshot_json, self.tmp_snapshots, g.user.email)
|
2022-04-08 09:12:17 +00:00
|
|
|
snapshot_json = self.validate(snapshot_json)
|
2022-04-11 09:24:25 +00:00
|
|
|
try:
|
|
|
|
self.snapshot_json = ParseSnapshotLsHw(snapshot_json).get_snapshot()
|
2022-04-21 16:55:00 +00:00
|
|
|
except Exception as err:
|
|
|
|
txt = "{}, {}".format(err.__class__, err)
|
|
|
|
uuid = snapshot_json.get('uuid')
|
2022-04-25 09:45:25 +00:00
|
|
|
sid = snapshot_json.get('sid')
|
2022-04-21 16:55:00 +00:00
|
|
|
error = SnapshotErrors(
|
2022-04-25 09:45:25 +00:00
|
|
|
description=txt, snapshot_uuid=uuid, severity=Severity.Error, sid=sid
|
2022-04-21 16:55:00 +00:00
|
|
|
)
|
|
|
|
error.save(commit=True)
|
2022-04-11 09:24:25 +00:00
|
|
|
self.response = jsonify('')
|
|
|
|
self.response.status_code = 201
|
|
|
|
return self.response
|
2022-04-08 09:12:17 +00:00
|
|
|
|
|
|
|
snapshot = self.build()
|
|
|
|
db.session.add(snapshot)
|
|
|
|
db.session().final_flush()
|
|
|
|
db.session.commit()
|
2022-04-25 12:05:30 +00:00
|
|
|
self.response = jsonify(
|
|
|
|
{
|
|
|
|
'url': snapshot.device.url,
|
|
|
|
'dhid': snapshot.device.devicehub_id,
|
|
|
|
'sid': snapshot.sid,
|
|
|
|
}
|
|
|
|
)
|
2022-04-08 09:12:17 +00:00
|
|
|
self.response.status_code = 201
|
|
|
|
move_json(self.tmp_snapshots, self.path_snapshot, g.user.email)
|
|
|
|
return self.response
|
|
|
|
|
|
|
|
def validate(self, snapshot_json):
|
|
|
|
self.schema = Snapshot_lite()
|
2022-04-06 18:23:08 +00:00
|
|
|
try:
|
2022-04-08 09:12:17 +00:00
|
|
|
return self.schema.load(snapshot_json)
|
2022-04-06 18:23:08 +00:00
|
|
|
except ValidationError as err:
|
|
|
|
txt = "{}".format(err)
|
|
|
|
uuid = snapshot_json.get('uuid')
|
|
|
|
error = SnapshotErrors(
|
|
|
|
description=txt, snapshot_uuid=uuid, severity=Severity.Error
|
|
|
|
)
|
|
|
|
error.save(commit=True)
|
|
|
|
raise err
|
|
|
|
|
2022-04-06 17:43:35 +00:00
|
|
|
|
|
|
|
api.add_url_rule('/inventory/', view_func=InventoryView.as_view('inventory'))
|