-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
298 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from redash.models import db, QuerySnippet | ||
|
||
if __name__ == '__main__': | ||
with db.database.transaction(): | ||
if not QuerySnippet.table_exists(): | ||
QuerySnippet.create_table() | ||
|
||
db.close_db(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
(function() { | ||
var SnippetsCtrl = function ($scope, $location, growl, Events, QuerySnippet) { | ||
Events.record(currentUser, "view", "page", "query_snippets"); | ||
$scope.$parent.pageTitle = "Query Snippets"; | ||
|
||
$scope.gridConfig = { | ||
isPaginationEnabled: true, | ||
itemsByPage: 20, | ||
maxSize: 8, | ||
}; | ||
|
||
$scope.gridColumns = [ | ||
{ | ||
"label": "Trigger", | ||
"cellTemplate": '<a href="query_snippets/{{dataRow.id}}">{{dataRow.trigger}}</a>' | ||
}, | ||
{ | ||
"label": "Description", | ||
"map": "description" | ||
}, | ||
{ | ||
"label": "Snippet", | ||
"map": "snippet" | ||
}, | ||
{ | ||
'label': 'Created By', | ||
'map': 'user.name' | ||
}, | ||
{ | ||
'label': 'Updated At', | ||
'cellTemplate': '<span am-time-ago="dataRow.created_at"></span>' | ||
} | ||
]; | ||
|
||
$scope.snippets = []; | ||
QuerySnippet.query(function(snippets) { | ||
$scope.snippets = snippets; | ||
}); | ||
}; | ||
|
||
var SnippetCtrl = function ($scope, $routeParams, $http, $location, growl, Events, QuerySnippet) { | ||
$scope.$parent.pageTitle = "Query Snippets"; | ||
$scope.snippetId = $routeParams.snippetId; | ||
Events.record(currentUser, "view", "query_snippet", $scope.snippetId); | ||
|
||
$scope.editorOptions = { | ||
mode: 'snippets', | ||
advanced: { | ||
behavioursEnabled: true, | ||
enableSnippets: false, | ||
autoScrollEditorIntoView: true, | ||
}, | ||
onLoad: function(editor) { | ||
editor.$blockScrolling = Infinity; | ||
editor.getSession().setUseWrapMode(true); | ||
editor.setShowPrintMargin(false); | ||
} | ||
}; | ||
|
||
$scope.saveChanges = function() { | ||
$scope.snippet.$save(function(snippet) { | ||
growl.addSuccessMessage("Saved."); | ||
if ($scope.snippetId === "new") { | ||
$location.path('/query_snippets/' + snippet.id).replace(); | ||
} | ||
}, function() { | ||
growl.addErrorMessage("Failed saving snippet."); | ||
}); | ||
} | ||
|
||
$scope.delete = function() { | ||
$scope.snippet.$delete(function() { | ||
$location.path('/query_snippets'); | ||
growl.addSuccessMessage("Query snippet deleted."); | ||
}, function() { | ||
growl.addErrorMessage("Failed deleting query snippet."); | ||
}); | ||
} | ||
|
||
if ($scope.snippetId == 'new') { | ||
$scope.snippet = new QuerySnippet({description: ""}); | ||
$scope.canEdit = true; | ||
} else { | ||
$scope.snippet = QuerySnippet.get({id: $scope.snippetId}, function(snippet) { | ||
$scope.canEdit = currentUser.canEdit(snippet); | ||
}); | ||
} | ||
}; | ||
|
||
angular.module('redash.controllers') | ||
.controller('SnippetsCtrl', ['$scope', '$location', 'growl', 'Events', 'QuerySnippet', SnippetsCtrl]) | ||
.controller('SnippetCtrl', ['$scope', '$routeParams', '$http', '$location', 'growl', 'Events', 'QuerySnippet', SnippetCtrl]) | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<settings-screen> | ||
<div class="row voffset1"> | ||
<div class="col-md-12"> | ||
<p> | ||
<a href="query_snippets/new" class="btn btn-default"><i class="fa fa-plus"></i> New Snippet</a> | ||
</p> | ||
|
||
<smart-table rows="snippets" columns="gridColumns" | ||
config="gridConfig" | ||
class="table table-condensed table-hover"></smart-table> | ||
</div> | ||
</div> | ||
</settings-screen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<settings-screen> | ||
<!--<h2 class="p-l-5">{{snippet.trigger}}</h2>--> | ||
|
||
<div class=""> | ||
<!--<pre>--> | ||
<!--{{snippet | json}}--> | ||
<!--</pre>--> | ||
|
||
<form name="snippetForm" class="form"> | ||
<div class="form-group"> | ||
<label>Trigger</label> | ||
<input type="string" class="form-control" ng-model="snippet.trigger" ng-disabled="!canEdit" required> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label>Description</label> | ||
<input type="string" class="form-control" ng-model="snippet.description" ng-disabled="!canEdit"> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label>Snippet</label> | ||
<pre ng-if="!canEdit">{{snippet.snippet}}</pre> | ||
<div ui-ace="editorOptions" ng-model="snippet.snippet" style="height:300px" ng-if="canEdit"></div> | ||
</div> | ||
|
||
<div class="form-group" ng-if="canEdit"> | ||
<button class="btn btn-primary" ng-disabled="!snippetForm.$valid" ng-click="saveChanges()">Save</button> | ||
<button class="btn btn-danger" ng-if="snippet.id" ng-click="delete()">Delete</button> | ||
</div> | ||
<small ng-if="snippet.user"> | ||
Created by: {{snippet.user.name}} | ||
</small> | ||
</form> | ||
|
||
</div> | ||
</settings-screen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from flask import request | ||
from funcy import project | ||
|
||
from redash import models | ||
from redash.permissions import require_admin_or_owner | ||
from redash.handlers.base import BaseResource, require_fields, get_object_or_404 | ||
|
||
|
||
class QuerySnippetResource(BaseResource): | ||
def get(self, snippet_id): | ||
snippet = get_object_or_404(models.QuerySnippet.get_by_id_and_org, snippet_id, self.current_org) | ||
return snippet.to_dict() | ||
|
||
def post(self, snippet_id): | ||
req = request.get_json(True) | ||
params = project(req, ('trigger', 'description', 'snippet')) | ||
snippet = get_object_or_404(models.QuerySnippet.get_by_id_and_org, snippet_id, self.current_org) | ||
require_admin_or_owner(snippet.user.id) | ||
|
||
snippet.update_instance(**params) | ||
|
||
self.record_event({ | ||
'action': 'edit', | ||
'object_id': snippet.id, | ||
'object_type': 'query_snippet' | ||
}) | ||
|
||
return snippet.to_dict() | ||
|
||
def delete(self, snippet_id): | ||
snippet = get_object_or_404(models.QuerySnippet.get_by_id_and_org, snippet_id, self.current_org) | ||
require_admin_or_owner(snippet.user.id) | ||
snippet.delete_instance() | ||
|
||
self.record_event({ | ||
'action': 'delete', | ||
'object_id': snippet.id, | ||
'object_type': 'query_snippet' | ||
}) | ||
|
||
|
||
class QuerySnippetListResource(BaseResource): | ||
def post(self): | ||
req = request.get_json(True) | ||
require_fields(req, ('trigger', 'description', 'snippet')) | ||
|
||
snippet = models.QuerySnippet.create( | ||
trigger=req['trigger'], | ||
description=req['description'], | ||
snippet=req['snippet'], | ||
user=self.current_user, | ||
org=self.current_org | ||
) | ||
|
||
self.record_event({ | ||
'action': 'create', | ||
'object_id': snippet.id, | ||
'object_type': 'query_snippet' | ||
}) | ||
|
||
return snippet.to_dict() | ||
|
||
def get(self): | ||
return [snippet.to_dict() for snippet in models.QuerySnippet.all(org=self.current_org)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.