Skip to content

Commit

Permalink
create data source with default group specific method
Browse files Browse the repository at this point in the history
  • Loading branch information
arikfr committed Jan 12, 2016
1 parent 901cf6f commit eed3d50
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion redash/handlers/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ def post(self):
if not validate_configuration(req['type'], req['options']):
abort(400)

datasource = models.DataSource.create(org=self.current_org, name=req['name'], type=req['type'], options=json.dumps(req['options']))
datasource = models.DataSource.create_with_group(org=self.current_org,
name=req['name'],
type=req['type'], options=json.dumps(req['options']))

return datasource.to_dict(all=True)

Expand Down
8 changes: 5 additions & 3 deletions redash/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,11 @@ def to_dict(self, all=False, with_permissions=False):
def __unicode__(self):
return self.name

def post_save(self, created):
if created:
DataSourceGroup.create(data_source=self, group=self.org.default_group)
@classmethod
def create_with_group(cls, *args, **kwargs):
data_source = cls.create(*args, **kwargs)
DataSourceGroup.create(data_source=data_source, group=data_source.org.default_group)
return data_source

@property
def configuration(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class TestDataSourceCreate(BaseTestCase):
def test_adds_data_source_to_default_group(self):
data_source = DataSource.create(org=self.factory.org, name='test', options='{}', type='pg')
data_source = DataSource.create_with_group(org=self.factory.org, name='test', options='{}', type='pg')
self.assertIn(self.factory.org.default_group.id, data_source.groups)

0 comments on commit eed3d50

Please sign in to comment.