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()
|
|
|
|
except ValidationError:
|
|
|
|
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()
|
|
|
|
self.response = self.schema.jsonify(snapshot)
|
|
|
|
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'))
|