mirror of
https://github.com/valitydev/redash.git
synced 2024-11-07 01:25:16 +00:00
51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
|
import redash.models
|
||
|
|
||
|
|
||
|
class ModelFactory(object):
|
||
|
def __init__(self, model, **kwargs):
|
||
|
self.model = model
|
||
|
self.kwargs = kwargs
|
||
|
|
||
|
def _get_kwargs(self, override_kwargs):
|
||
|
kwargs = self.kwargs.copy()
|
||
|
kwargs.update(override_kwargs)
|
||
|
|
||
|
for key, arg in kwargs.items():
|
||
|
if callable(arg):
|
||
|
kwargs[key] = arg()
|
||
|
|
||
|
return kwargs
|
||
|
|
||
|
def instance(self, **override_kwargs):
|
||
|
kwargs = self._get_kwargs(override_kwargs)
|
||
|
|
||
|
return self.model(**kwargs)
|
||
|
|
||
|
def create(self, **override_kwargs):
|
||
|
kwargs = self._get_kwargs(override_kwargs)
|
||
|
return self.model.create(**kwargs)
|
||
|
|
||
|
dashboard_factory = ModelFactory(redash.models.Dashboard,
|
||
|
name='test', user='test@everything.me', layout='[]')
|
||
|
|
||
|
query_factory = ModelFactory(redash.models.Query,
|
||
|
name='New Query',
|
||
|
description='',
|
||
|
query='SELECT 1',
|
||
|
ttl=-1,
|
||
|
user='test@everything.me')
|
||
|
|
||
|
visualization_factory = ModelFactory(redash.models.Visualization,
|
||
|
type='CHART',
|
||
|
query=query_factory.create,
|
||
|
name='Chart',
|
||
|
description='',
|
||
|
options='{}')
|
||
|
|
||
|
widget_factory = ModelFactory(redash.models.Widget,
|
||
|
type='chart',
|
||
|
width=1,
|
||
|
options='{}',
|
||
|
dashboard=dashboard_factory.create,
|
||
|
visualization=visualization_factory.create)
|