Skip to content

Commit

Permalink
Replace client_id with actual query id
Browse files Browse the repository at this point in the history
  • Loading branch information
vera-liu committed Mar 17, 2017
1 parent 2042c72 commit 585f72a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 23 deletions.
10 changes: 5 additions & 5 deletions superset/assets/javascripts/SqlLab/actions.js
Expand Up @@ -186,16 +186,16 @@ export function removeQuery(query) {
}

export const FAVE_QUERY_SUCCESS = 'FAVE_QUERY_SUCCESS';
export function faveQuerySuccess(query) {
return { type: FAVE_QUERY_SUCCESS, query };
export function faveQuerySuccess(query, fave) {
return { type: FAVE_QUERY_SUCCESS, query, fave };
}

const FAVEQUERY_BASE_URL = '/superset/favstar/query';
export function favouriteQuery(query) {
export function favouriteQuery(query, fave) {
return function (dispatch) {
const favAction = query.faved ? 'unselect' : 'select';
const favAction = fave ? 'select' : 'unselect';
$.get(`${FAVEQUERY_BASE_URL}/${query.id}/${favAction}/`);
dispatch(faveQuerySuccess(query));
dispatch(faveQuerySuccess(query, fave));
};
}

Expand Down
9 changes: 2 additions & 7 deletions superset/assets/javascripts/SqlLab/components/FaveQueries.jsx
Expand Up @@ -16,13 +16,8 @@ class FaveQueries extends React.PureComponent {
}

deleteQuery(query) {
$.ajax({
type: 'GET',
url: `/superset/favstar/query/${query.id}/unselect/`,
success: () => {
this.fetchFaveQueries();
},
});
this.props.actions.favouriteQuery(query, false);
this.fetchFaveQueries();
}
fetchFaveQueries() {
this.setState({ fetching: true });
Expand Down
Expand Up @@ -75,7 +75,7 @@ class QueryTable extends React.PureComponent {
this.props.actions.removeQuery(query);
}
favQuery(query) {
this.props.actions.favouriteQuery(query);
this.props.actions.favouriteQuery(query, !query.faved);
}
render() {
const data = this.props.queries.map((query) => {
Expand Down
6 changes: 1 addition & 5 deletions superset/assets/javascripts/SqlLab/reducers.js
Expand Up @@ -72,11 +72,7 @@ export const sqlLabReducer = function (state, action) {
return Object.assign({}, state, { queries: newQueries });
},
[actions.FAVE_QUERY_SUCCESS]() {
let faved = false;
if (!state.queries[action.query.id].faved) {
faved = true;
}
return alterInObject(state, 'queries', action.query, { faved });
return alterInObject(state, 'queries', action.query, { faved: action.fave });
},
[actions.RESET_STATE]() {
return Object.assign({}, getInitialState());
Expand Down
@@ -1,12 +1,9 @@
import React from 'react';
import Select from 'react-select';
import FaveQueries from '../../../javascripts/SqlLab/components/FaveQueries';
import { shallow } from 'enzyme';
import { describe, it } from 'mocha';
import { expect } from 'chai';
import sinon from 'sinon';

describe('QuerySearch', () => {
describe('FaveQueries', () => {
const mockedProps = {
actions: {},
};
Expand Down
9 changes: 8 additions & 1 deletion superset/views/core.py
Expand Up @@ -1526,7 +1526,7 @@ def fave_queries(self):
sqla.and_(
models.FavStar.user_id == int(g.user.id),
models.FavStar.class_name == 'query',
models.Query.client_id == models.FavStar.obj_id,
models.Query.id == models.FavStar.obj_id,
)
)
.order_by(
Expand Down Expand Up @@ -1696,6 +1696,13 @@ def favstar(self, class_name, obj_id, action):
session = db.session()
FavStar = models.FavStar # noqa
count = 0
if class_name == 'query':
query = session.query(models.Query).filter_by(
client_id=obj_id).first()
if not query:
return json_error_response(
json.dumps({'error': 'No query was found!'}))
obj_id = query.id
favs = session.query(FavStar).filter_by(
class_name=class_name, obj_id=obj_id,
user_id=g.user.get_id()).all()
Expand Down

0 comments on commit 585f72a

Please sign in to comment.