Skip to content

Commit

Permalink
Merge pull request getredash#1256 from ninneko/fork_with_vis
Browse files Browse the repository at this point in the history
Change: when forking a query, copy all visualizations
  • Loading branch information
arikfr authored Nov 23, 2016
2 parents e06740d + 4de6df4 commit ffcee73
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from redash.handlers.dashboards import DashboardListResource, RecentDashboardsResource, DashboardResource, DashboardShareResource
from redash.handlers.data_sources import DataSourceTypeListResource, DataSourceListResource, DataSourceSchemaResource, DataSourceResource, DataSourcePauseResource, DataSourceTestResource
from redash.handlers.events import EventResource
from redash.handlers.queries import QueryRefreshResource, QueryListResource, QueryRecentResource, QuerySearchResource, QueryResource, MyQueriesResource
from redash.handlers.queries import QueryForkResource, QueryRefreshResource, QueryListResource, QueryRecentResource, QuerySearchResource, QueryResource, MyQueriesResource
from redash.handlers.query_results import QueryResultListResource, QueryResultResource, JobResource
from redash.handlers.users import UserResource, UserListResource, UserInviteResource, UserResetPasswordResource
from redash.handlers.visualizations import VisualizationListResource
Expand Down Expand Up @@ -71,6 +71,7 @@ def json_representation(data, code, headers=None):
api.add_org_resource(MyQueriesResource, '/api/queries/my', endpoint='my_queries')
api.add_org_resource(QueryRefreshResource, '/api/queries/<query_id>/refresh', endpoint='query_refresh')
api.add_org_resource(QueryResource, '/api/queries/<query_id>', endpoint='query')
api.add_org_resource(QueryForkResource, '/api/queries/<query_id>/fork', endpoint='query_fork')

api.add_org_resource(ObjectPermissionsListResource, '/api/<object_type>/<object_id>/acl', endpoint='object_permissions')
api.add_org_resource(CheckPermissionResource, '/api/<object_type>/<object_id>/acl/<access_type>', endpoint='check_permissions')
Expand Down
8 changes: 8 additions & 0 deletions handlers/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ def delete(self, query_id):
query.archive(self.current_user)


class QueryForkResource(BaseResource):
@require_permission('edit_query')
def post(self, query_id):
query = get_object_or_404(models.Query.get_by_id_and_org, query_id, self.current_org)
forked_query = query.fork(self.current_user)
return forked_query.to_dict(with_visualizations=True)


class QueryRefreshResource(BaseResource):
def post(self, query_id):
query = get_object_or_404(models.Query.get_by_id_and_org, query_id, self.current_org)
Expand Down
25 changes: 25 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,31 @@ def recent(cls, groups, user_id=None, limit=20):

return query

def fork(self, user):
query = self
forked_query = Query()
forked_query.name = 'Copy of (#{}) {}'.format(query.id, query.name)
forked_query.user = user
forked_list = ['org', 'data_source', 'latest_query_data', 'description', 'query', 'query_hash']
for a in forked_list:
setattr(forked_query, a, getattr(query, a))
forked_query.save()

forked_visualizations = []
for v in query.visualizations:
if v.type == 'TABLE':
continue
forked_v = v.to_dict()
forked_v['options'] = v.options
forked_v['query'] = forked_query
forked_v.pop('id')
forked_visualizations.append(forked_v)

if len(forked_visualizations) > 0:
with db.database.atomic():
Visualization.insert_many(forked_visualizations).execute()
return forked_query

def pre_save(self, created):
super(Query, self).pre_save(created)
self.query_hash = utils.gen_query_hash(self.query)
Expand Down

0 comments on commit ffcee73

Please sign in to comment.