This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/commands/snapshots.py

104 lines
3.0 KiB
Python
Raw Normal View History

2023-07-21 15:20:15 +00:00
"""This command is used for up one snapshot."""
import json
# from uuid import uuid4
2023-07-21 16:02:22 +00:00
from io import BytesIO
2023-07-24 10:33:04 +00:00
from os import listdir
from os import remove as remove_file
from os.path import isfile, join
2023-07-21 15:20:15 +00:00
from pathlib import Path
import click
2023-07-21 16:02:22 +00:00
from flask.testing import FlaskClient
from flask_wtf.csrf import generate_csrf
2023-07-21 15:20:15 +00:00
from ereuse_devicehub.resources.user.models import User
2023-07-21 16:02:22 +00:00
class UploadSnapshots:
2023-07-24 07:29:46 +00:00
"""Command.
2023-07-21 15:20:15 +00:00
This command allow upload all snapshots than exist
in the directory snapshots_upload.
If this snapshot exist replace it.
"""
def __init__(self, app) -> None:
"""Init function."""
super().__init__()
self.app = app
self.schema = app.config.get('DB_SCHEMA')
self.app.cli.command('snapshot', short_help='Upload snapshots.')(self.run)
@click.argument('email')
@click.argument('password')
def run(self, email, password=None):
2023-07-21 15:20:15 +00:00
"""Run command."""
self.email = email
self.password = password
2023-07-21 15:20:15 +00:00
self.json_wb = None
2023-07-24 10:33:04 +00:00
self.onlyfiles = []
2023-07-21 15:20:15 +00:00
2023-07-25 13:54:24 +00:00
self.get_user()
self.get_files()
for f in self.onlyfiles:
self.file_snapshot = f
self.open_snapshot()
self.build_snapshot()
self.remove_files()
2023-07-21 15:20:15 +00:00
def get_user(self):
"""Get datamodel of user."""
self.user = User.query.filter_by(email=self.email).one()
2023-07-21 16:02:22 +00:00
self.client = FlaskClient(self.app, use_cookies=True)
self.client.get('/login/')
2023-07-21 15:20:15 +00:00
data = {
'email': self.email,
'password': self.password,
'remember': False,
'csrf_token': generate_csrf(),
}
self.client.post('/login/', data=data, follow_redirects=True)
2023-07-24 10:33:04 +00:00
def remove_files(self):
"""Open snapshot file."""
for f in self.onlyfiles:
remove_file(Path(__file__).parent.joinpath('snapshot_files').joinpath(f))
2023-07-21 15:20:15 +00:00
def open_snapshot(self):
"""Open snapshot file."""
with Path(__file__).parent.joinpath('snapshot_files').joinpath(
2023-07-24 07:29:46 +00:00
self.file_snapshot,
2023-07-21 15:20:15 +00:00
).open() as file_snapshot:
self.json_wb = json.loads(file_snapshot.read())
2023-07-21 16:02:22 +00:00
b_snapshot = bytes(json.dumps(self.json_wb), 'utf-8')
2023-07-21 15:20:15 +00:00
self.file_snap = (BytesIO(b_snapshot), self.file_snapshot)
def build_snapshot(self):
"""Build the devices of snapshot."""
2023-07-21 16:02:22 +00:00
uri = '/inventory/upload-snapshot/'
2023-07-21 15:20:15 +00:00
2023-07-24 10:33:04 +00:00
if not self.json_wb:
2023-07-21 15:20:15 +00:00
return
2023-07-21 16:02:22 +00:00
self.client.get(uri)
data = {
'snapshot': self.file_snap,
'csrf_token': generate_csrf(),
}
2023-07-24 10:33:04 +00:00
2023-07-21 16:02:22 +00:00
self.client.post(uri, data=data, content_type="multipart/form-data")
2023-07-24 10:33:04 +00:00
def get_files(self):
"""Read snaoshot_files dir."""
mypath = Path(__file__).parent.joinpath('snapshot_files')
for f in listdir(mypath):
if not isfile(join(mypath, f)):
continue
if not f[-5:] == ".json":
continue
self.onlyfiles.append(f)