Fix importer to use data source

This commit is contained in:
Arik Fraimovich 2014-03-19 12:53:30 +02:00
parent 679921dc8e
commit 579ca28d6d
2 changed files with 18 additions and 7 deletions

View File

@ -5,10 +5,11 @@ from flask.ext.script import Manager
class Importer(object):
def __init__(self, object_mapping=None):
def __init__(self, object_mapping=None, data_source=None):
if object_mapping is None:
object_mapping = {}
self.object_mapping = object_mapping
self.data_source = data_source
def import_query_result(self, query_result):
query_result = self._get_or_create(models.QueryResult, query_result['id'],
@ -30,7 +31,8 @@ class Importer(object):
query=query['query'],
query_hash=query['query_hash'],
description=query['description'],
latest_query_data=query_result)
latest_query_data=query_result,
data_source=self.data_source)
return new_query
@ -115,12 +117,21 @@ def importer_with_mapping_file(mapping_filename):
with open(mapping_filename) as f:
mapping = json.loads(f.read())
importer = Importer(object_mapping=mapping)
importer = Importer(object_mapping=mapping, data_source=get_data_source())
yield importer
with open(mapping_filename, 'w') as f:
f.write(json.dumps(importer.object_mapping, indent=2))
def get_data_source():
try:
data_source = models.DataSource.get(models.DataSource.name=="Import")
except models.DataSource.DoestNotExist:
data_source = models.DataSource.create(name="Import", type="import", options='{}')
return data_source
@import_manager.command
def query(mapping_filename, query_filename, user_id):
user = models.User.get_by_id(user_id)

View File

@ -3,7 +3,7 @@ import os.path
from tests import BaseTestCase
from redash import models
from redash import import_export
from factories import user_factory, dashboard_factory
from factories import user_factory, dashboard_factory, data_source_factory
class ImportTest(BaseTestCase):
@ -15,7 +15,7 @@ class ImportTest(BaseTestCase):
self.user = user_factory.create()
def test_imports_dashboard_correctly(self):
importer = import_export.Importer()
importer = import_export.Importer(data_source=data_source_factory.create())
dashboard = importer.import_dashboard(self.user, self.dashboard)
self.assertIsNotNone(dashboard)
@ -31,7 +31,7 @@ class ImportTest(BaseTestCase):
self.assertEqual(models.QueryResult.select().count(), dashboard.widgets.count()-1)
def test_imports_updates_existing_models(self):
importer = import_export.Importer()
importer = import_export.Importer(data_source=data_source_factory.create())
importer.import_dashboard(self.user, self.dashboard)
self.dashboard['name'] = 'Testing #2'
@ -47,7 +47,7 @@ class ImportTest(BaseTestCase):
}
}
importer = import_export.Importer(object_mapping=mapping)
importer = import_export.Importer(object_mapping=mapping, data_source=data_source_factory.create())
imported_dashboard = importer.import_dashboard(self.user, self.dashboard)
self.assertEqual(imported_dashboard, dashboard)