upload snapshot from web client

This commit is contained in:
Cayo Puigdefabregas 2023-07-21 18:02:22 +02:00
parent 9ff20740dd
commit 13d36f5650
1 changed files with 14 additions and 57 deletions

View File

@ -1,25 +1,20 @@
"""This command is used for up one snapshot.""" """This command is used for up one snapshot."""
import copy
import json import json
from io import BytesIO
# from uuid import uuid4 # from uuid import uuid4
from io import BytesIO
from pathlib import Path from pathlib import Path
import click import click
from decouple import config from decouple import config
from flask import Session, g from flask.testing import FlaskClient
from flask_wtf.csrf import generate_csrf
from ereuse_devicehub.client import UserClient
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema
from ereuse_devicehub.resources.action.views.snapshot import SnapshotMixin
from ereuse_devicehub.resources.device.models import Computer
from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.user.models import User
class UploadSnapshots(SnapshotMixin): class UploadSnapshots:
""" """
Command. Command.
@ -42,39 +37,19 @@ class UploadSnapshots(SnapshotMixin):
self.file_snapshot = file_snapshot self.file_snapshot = file_snapshot
self.snapshot_json = None self.snapshot_json = None
self.json_wb = None self.json_wb = None
self.schema = SnapshotSchema()
with self.app.app_context(): with self.app.app_context():
self.get_user() self.get_user()
self.open_snapshot() self.open_snapshot()
self.load_schema()
self.build_snapshot() self.build_snapshot()
# db.session.commit()
def get_user(self): def get_user(self):
"""Get datamodel of user.""" """Get datamodel of user."""
self.email = config('EMAIL_DEMO') self.email = config('EMAIL_DEMO')
self.password = config('PASSWORD_DEMO') self.password = config('PASSWORD_DEMO')
self.user = User.query.filter_by(email=self.email).one() self.user = User.query.filter_by(email=self.email).one()
import pdb self.client = FlaskClient(self.app, use_cookies=True)
self.client.get('/login/')
pdb.set_trace()
g.user = self.user
# if 'dpp' in self.app.blueprints.keys():
# client = UserClient(
# self.app,
# self.email,
# self.password,
# response_wrapper=self.app.response_class
# )
# client.login_web(self.email, self.password)
from flask.testing import FlaskClient
from flask_wtf.csrf import generate_csrf
client = FlaskClient(self.app, use_cookies=True)
body, status, headers = client.get('/login/')
data = { data = {
'email': self.email, 'email': self.email,
@ -90,37 +65,19 @@ class UploadSnapshots(SnapshotMixin):
self.file_snapshot self.file_snapshot
).open() as file_snapshot: ).open() as file_snapshot:
self.json_wb = json.loads(file_snapshot.read()) self.json_wb = json.loads(file_snapshot.read())
b_snapshot = bytes(json.dumps(snapshot), 'utf-8') b_snapshot = bytes(json.dumps(self.json_wb), 'utf-8')
self.file_snap = (BytesIO(b_snapshot), self.file_snapshot) self.file_snap = (BytesIO(b_snapshot), self.file_snapshot)
def load_schema(self):
"""Load schema for check snapshot."""
if not self.json_wb:
return
self.snapshot_json = self.schema.load(self.json_wb)
def build_snapshot(self): def build_snapshot(self):
"""Build the devices of snapshot.""" """Build the devices of snapshot."""
import pdb uri = '/inventory/upload-snapshot/'
pdb.set_trace()
if not self.snapshot_json: if not self.snapshot_json:
return return
response = self.build(self.snapshot_json)
if isinstance(response.device, Computer):
response.device.user_trusts = True
db.session.add(response)
uri = '/inventory/upload-snapshot/' self.client.get(uri)
snapshot = conftest.yaml2json(file_name.split(".json")[0]) data = {
b_snapshot = bytes(json.dumps(snapshot), 'utf-8') 'snapshot': self.file_snap,
file_snap = (BytesIO(b_snapshot), file_name) 'csrf_token': generate_csrf(),
user.get(uri) }
self.client.post(uri, data=data, content_type="multipart/form-data")
data = {
'snapshot': file_snap,
'csrf_token': generate_csrf(),
}
user.post(uri, data=data, content_type="multipart/form-data")
return Snapshot.query.one()