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-25 16:38:13 +00:00
|
|
|
from flask.wrappers import Response
|
|
|
|
from marshmallow.exceptions 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
|
2022-05-18 09:03:58 +00:00
|
|
|
from ereuse_devicehub.parser.models import SnapshotsLog
|
2022-04-06 18:23:08 +00:00
|
|
|
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 (
|
2022-05-16 15:52:31 +00:00
|
|
|
SnapshotMixin,
|
2022-04-07 19:04:05 +00:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2022-05-16 15:52:31 +00:00
|
|
|
class LoginMixin(View):
|
2022-04-06 17:43:35 +00:00
|
|
|
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-05-16 15:52:31 +00:00
|
|
|
class InventoryView(LoginMixin, SnapshotMixin):
|
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-25 16:38:13 +00:00
|
|
|
if type(snapshot_json) == Response:
|
|
|
|
return snapshot_json
|
2022-04-26 08:43:58 +00:00
|
|
|
|
|
|
|
self.snapshot_json = ParseSnapshotLsHw(snapshot_json).get_snapshot()
|
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(
|
|
|
|
{
|
2022-05-04 15:58:14 +00:00
|
|
|
'url': snapshot.device.url.to_text(),
|
2022-04-25 12:05:30 +00:00
|
|
|
'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')
|
2022-04-25 16:38:13 +00:00
|
|
|
sid = snapshot_json.get('sid')
|
2022-05-18 09:03:58 +00:00
|
|
|
error = SnapshotsLog(
|
2022-04-25 16:38:13 +00:00
|
|
|
description=txt, snapshot_uuid=uuid, severity=Severity.Error, sid=sid
|
2022-04-06 18:23:08 +00:00
|
|
|
)
|
|
|
|
error.save(commit=True)
|
2022-04-25 16:38:13 +00:00
|
|
|
# raise err
|
|
|
|
self.response = jsonify(err)
|
|
|
|
self.response.status_code = 400
|
|
|
|
return self.response
|
2022-04-06 18:23:08 +00:00
|
|
|
|
2022-04-06 17:43:35 +00:00
|
|
|
|
|
|
|
api.add_url_rule('/inventory/', view_func=InventoryView.as_view('inventory'))
|