2022-04-06 18:23:08 +00:00
|
|
|
import json
|
2023-07-03 16:49:15 +00:00
|
|
|
import logging
|
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
|
2023-07-11 12:06:16 +00:00
|
|
|
from ereuse_devicehub.parser.parser import ParseSnapshot
|
2022-04-06 18:23:08 +00:00
|
|
|
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
|
|
|
|
2023-07-03 16:49:15 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
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
|
|
|
|
2023-07-03 16:49:15 +00:00
|
|
|
try:
|
2023-07-11 12:06:16 +00:00
|
|
|
self.snapshot_json = ParseSnapshot(snapshot_json).get_snapshot()
|
2023-07-03 16:49:15 +00:00
|
|
|
raise 1 == 2
|
|
|
|
except Exception as err:
|
|
|
|
logger.error("Error: {} \n{}\n".format(err, self.snapshot_json))
|
|
|
|
|
|
|
|
self.response = jsonify(
|
|
|
|
{
|
|
|
|
'error': err,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self.response.status_code = 500
|
|
|
|
return self.response
|
2022-04-08 09:12:17 +00:00
|
|
|
|
|
|
|
snapshot = self.build()
|
2022-11-16 16:40:35 +00:00
|
|
|
snapshot.device.set_hid()
|
|
|
|
snapshot.device.binding.device.set_hid()
|
2022-04-08 09:12:17 +00:00
|
|
|
db.session.add(snapshot)
|
2022-05-18 10:47:23 +00:00
|
|
|
|
|
|
|
snap_log = SnapshotsLog(
|
|
|
|
description='Ok',
|
|
|
|
snapshot_uuid=snapshot.uuid,
|
|
|
|
severity=Severity.Info,
|
|
|
|
sid=snapshot.sid,
|
2022-05-20 16:31:47 +00:00
|
|
|
version=str(snapshot.version),
|
2022-05-18 16:02:08 +00:00
|
|
|
snapshot=snapshot,
|
2022-05-18 10:47:23 +00:00
|
|
|
)
|
|
|
|
snap_log.save()
|
|
|
|
|
2022-04-08 09:12:17 +00:00
|
|
|
db.session().final_flush()
|
|
|
|
db.session.commit()
|
2022-10-28 12:08:51 +00:00
|
|
|
url = "https://{}/".format(app.config['HOST'])
|
|
|
|
public_url = "{}{}".format(url.strip("/"), snapshot.device.url.to_text())
|
2022-04-25 12:05:30 +00:00
|
|
|
self.response = jsonify(
|
|
|
|
{
|
2022-08-31 15:27:17 +00:00
|
|
|
'dhid': snapshot.device.dhid,
|
2022-10-28 12:08:51 +00:00
|
|
|
'url': url,
|
|
|
|
'public_url': public_url,
|
2022-04-25 12:05:30 +00:00
|
|
|
}
|
|
|
|
)
|
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 10:47:23 +00:00
|
|
|
version = snapshot_json.get('version')
|
2022-05-18 09:03:58 +00:00
|
|
|
error = SnapshotsLog(
|
2022-05-18 10:47:23 +00:00
|
|
|
description=txt,
|
|
|
|
snapshot_uuid=uuid,
|
|
|
|
severity=Severity.Error,
|
|
|
|
sid=sid,
|
2022-05-20 16:31:47 +00:00
|
|
|
version=str(version),
|
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'))
|