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/dummy/dummy.py

60 lines
2.1 KiB
Python
Raw Normal View History

2018-06-20 21:18:15 +00:00
from pathlib import Path
import click
import click_spinner
import yaml
from ereuse_devicehub.client import UserClient
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.event.models import Snapshot
2018-07-02 10:52:54 +00:00
from ereuse_devicehub.resources.inventory import Inventory
2018-06-20 21:18:15 +00:00
from ereuse_devicehub.resources.tag.model import Tag
from ereuse_devicehub.resources.user import User
class Dummy:
SNAPSHOTS = (
'workbench-server-1',
'computer-monitor'
)
TAGS = (
'tag1',
'tag2',
'tag3'
)
def __init__(self, app) -> None:
super().__init__()
self.app = app
self.app.cli.command('dummy',
short_help='Creates dummy devices and users.')(self.run)
@click.confirmation_option(prompt='This command deletes the DB in the process. '
'Do you want to continue?')
def run(self):
2018-07-02 10:52:54 +00:00
print('Preparing the database...'.ljust(30), end='')
2018-06-20 21:18:15 +00:00
with click_spinner.spinner():
self.app.init_db(erase=True)
user = self.user_client('user@dhub.com', '1234')
user.post(res=Tag, query=[('ids', i) for i in self.TAGS], data={})
2018-07-02 10:52:54 +00:00
files = tuple(Path(__file__).parent.joinpath('files').iterdir())
print('done.')
with click.progressbar(files, label='Creating devices...'.ljust(28)) as bar:
for path in bar:
with path.open() as f:
snapshot = yaml.load(f)
user.post(res=Snapshot, data=snapshot)
inventory, _ = user.get(res=Inventory)
assert len(inventory['devices'])
print('⭐ Done.')
2018-06-20 21:18:15 +00:00
def user_client(self, email: str, password: str):
user = User(email=email, password=password)
db.session.add(user)
db.session.commit()
client = UserClient(application=self.app,
response_wrapper=self.app.response_class,
email=user.email,
password=password)
client.user, _ = client.login(client.email, client.password)
return client