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."""
import copy
import json
from io import BytesIO
# from uuid import uuid4
from io import BytesIO
from pathlib import Path
import click
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
class UploadSnapshots(SnapshotMixin):
class UploadSnapshots:
"""
Command.
@ -42,39 +37,19 @@ class UploadSnapshots(SnapshotMixin):
self.file_snapshot = file_snapshot
self.snapshot_json = None
self.json_wb = None
self.schema = SnapshotSchema()
with self.app.app_context():
self.get_user()
self.open_snapshot()
self.load_schema()
self.build_snapshot()
# db.session.commit()
def get_user(self):
"""Get datamodel of user."""
self.email = config('EMAIL_DEMO')
self.password = config('PASSWORD_DEMO')
self.user = User.query.filter_by(email=self.email).one()
import pdb
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/')
self.client = FlaskClient(self.app, use_cookies=True)
self.client.get('/login/')
data = {
'email': self.email,
@ -90,37 +65,19 @@ class UploadSnapshots(SnapshotMixin):
self.file_snapshot
).open() as file_snapshot:
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)
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):
"""Build the devices of snapshot."""
import pdb
uri = '/inventory/upload-snapshot/'
pdb.set_trace()
if not self.snapshot_json:
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/'
snapshot = conftest.yaml2json(file_name.split(".json")[0])
b_snapshot = bytes(json.dumps(snapshot), 'utf-8')
file_snap = (BytesIO(b_snapshot), file_name)
user.get(uri)
self.client.get(uri)
data = {
'snapshot': file_snap,
'snapshot': self.file_snap,
'csrf_token': generate_csrf(),
}
user.post(uri, data=data, content_type="multipart/form-data")
return Snapshot.query.one()
self.client.post(uri, data=data, content_type="multipart/form-data")