Fix queries update handler

This commit is contained in:
Arik Fraimovich 2016-12-04 14:03:15 +02:00 committed by Allen Short
parent 7d45812ef7
commit 03b2a416c8
3 changed files with 30 additions and 17 deletions

View File

@ -109,11 +109,8 @@ class QueryResource(BaseResource):
for field in ['id', 'created_at', 'api_key', 'visualizations', 'latest_query_data', 'user', 'last_modified_by', 'org']: for field in ['id', 'created_at', 'api_key', 'visualizations', 'latest_query_data', 'user', 'last_modified_by', 'org']:
query_def.pop(field, None) query_def.pop(field, None)
if 'latest_query_data_id' in query_def: if 'query' in query_def:
query_def['latest_query_data'] = query_def.pop('latest_query_data_id') query_def['query_text'] = query_def.pop('query')
if 'data_source_id' in query_def:
query_def['data_source'] = query_def.pop('data_source_id')
query_def['last_modified_by'] = self.current_user query_def['last_modified_by'] = self.current_user
query_def['changed_by'] = self.current_user query_def['changed_by'] = self.current_user

View File

@ -72,8 +72,10 @@ class GFKBase(object):
class PseudoJSON(TypeDecorator): class PseudoJSON(TypeDecorator):
impl = db.Text impl = db.Text
def process_bind_param(self, value, dialect): def process_bind_param(self, value, dialect):
return json_dumps(value) return json_dumps(value)
def process_result_value(self, value, dialect): def process_result_value(self, value, dialect):
if not value: if not value:
return value return value
@ -121,16 +123,13 @@ class ChangeTrackingMixin(object):
if attr.key not in self.skipped_fields: if attr.key not in self.skipped_fields:
changes[col.name] = {'previous': self._clean_values[col.name], changes[col.name] = {'previous': self._clean_values[col.name],
'current': getattr(self, attr.key)} 'current': getattr(self, attr.key)}
db.session.add(Change(object=self, db.session.add(Change(object=self,
object_version=self.version, object_version=self.version,
user=changed_by, user=changed_by,
change=changes)) change=changes))
class ConflictDetectedError(Exception):
pass
class BelongsToOrgMixin(object): class BelongsToOrgMixin(object):
@classmethod @classmethod
def get_by_id_and_org(cls, object_id, org): def get_by_id_and_org(cls, object_id, org):
@ -645,7 +644,7 @@ class Query(ChangeTrackingMixin, TimestampMixin, BelongsToOrgMixin, db.Model):
def to_dict(self, with_stats=False, with_visualizations=False, with_user=True, with_last_modified_by=True): def to_dict(self, with_stats=False, with_visualizations=False, with_user=True, with_last_modified_by=True):
d = { d = {
'id': self.id, 'id': self.id,
'latest_query_data_id': self.latest_query_data, 'latest_query_data_id': self.latest_query_data_id,
'name': self.name, 'name': self.name,
'description': self.description, 'description': self.description,
'query': self.query_text, 'query': self.query_text,
@ -831,14 +830,17 @@ class Query(ChangeTrackingMixin, TimestampMixin, BelongsToOrgMixin, db.Model):
def __unicode__(self): def __unicode__(self):
return unicode(self.id) return unicode(self.id)
@listens_for(Query.query_text, 'set') @listens_for(Query.query_text, 'set')
def gen_query_hash(target, val, oldval, initiator): def gen_query_hash(target, val, oldval, initiator):
target.query_hash = utils.gen_query_hash(val) target.query_hash = utils.gen_query_hash(val)
@listens_for(Query.user_id, 'set') @listens_for(Query.user_id, 'set')
def query_last_modified_by(target, val, oldval, initiator): def query_last_modified_by(target, val, oldval, initiator):
target.last_modified_by_id = val target.last_modified_by_id = val
# Create default (table) visualization: # Create default (table) visualization:
@listens_for(SignallingSession, 'before_flush') @listens_for(SignallingSession, 'before_flush')
def create_defaults(session, ctx, *a): def create_defaults(session, ctx, *a):
@ -1056,7 +1058,8 @@ class Dashboard(ChangeTrackingMixin, TimestampMixin, BelongsToOrgMixin, db.Model
name = Column(db.String(100)) name = Column(db.String(100))
user_id = Column(db.Integer, db.ForeignKey("users.id")) user_id = Column(db.Integer, db.ForeignKey("users.id"))
user = db.relationship(User) user = db.relationship(User)
# XXX replace with association table # TODO: The layout should dynamically be built from position and size information on each widget.
# Will require update in the frontend code to support this.
layout = Column(db.Text) layout = Column(db.Text)
dashboard_filters_enabled = Column(db.Boolean, default=False) dashboard_filters_enabled = Column(db.Boolean, default=False)
is_archived = Column(db.Boolean, default=False, index=True) is_archived = Column(db.Boolean, default=False, index=True)

View File

@ -55,10 +55,23 @@ class TestQueryResourcePost(BaseTestCase):
admin = self.factory.create_admin() admin = self.factory.create_admin()
query = self.factory.create_query() query = self.factory.create_query()
rv = self.make_request('post', '/api/queries/{0}'.format(query.id), data={'name': 'Testing'}, user=admin) new_ds = self.factory.create_data_source()
new_qr = self.factory.create_query_result()
data = {
'name': 'Testing',
'query': 'select 2',
'latest_query_data_id': new_qr.id,
'data_source_id': new_ds.id
}
rv = self.make_request('post', '/api/queries/{0}'.format(query.id), data=data, user=admin)
self.assertEqual(rv.status_code, 200) self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.json['name'], 'Testing') self.assertEqual(rv.json['name'], data['name'])
self.assertEqual(rv.json['last_modified_by']['id'], admin.id) self.assertEqual(rv.json['last_modified_by']['id'], admin.id)
self.assertEqual(rv.json['query'], data['query'])
self.assertEqual(rv.json['data_source_id'], data['data_source_id'])
self.assertEqual(rv.json['latest_query_data_id'], data['latest_query_data_id'])
def test_raises_error_in_case_of_conflict(self): def test_raises_error_in_case_of_conflict(self):
q = self.factory.create_query() q = self.factory.create_query()