Skip to content

Commit

Permalink
Merge 49131ae into af7b6d8
Browse files Browse the repository at this point in the history
  • Loading branch information
aarranz committed Jul 19, 2018
2 parents af7b6d8 + 49131ae commit aaa1d71
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 82 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -4,7 +4,8 @@ python:
- "2.7"
env:
- CKANVERSION=2.6.3 POSTGISVERSION=2
- CKANVERSION=2.7.0 POSTGISVERSION=2
- CKANVERSION=2.7.4 POSTGISVERSION=2
- CKANVERSION=2.8.0 POSTGISVERSION=2
services:
- redis-server
- postgresql
Expand Down
58 changes: 58 additions & 0 deletions ckanext/wirecloudview/controller.py
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.

# This file is part of CKAN WireCloud View Extension.

# CKAN WireCloud View Extension is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# CKAN WireCloud View Extension is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with CKAN WireCloud View Extension. If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

import json
import os
from urlparse import urljoin
from urllib import quote_plus

from ckan.common import request, response
from ckan.lib import base
from ckan.plugins import toolkit, get_plugin
from requests_oauthlib import OAuth2Session
import six


class WireCloudViewController(base.BaseController):

def __init__(self):
self.client_id = six.text_type(os.environ.get('CKAN_OAUTH2_CLIENT_ID', toolkit.config.get('ckan.oauth2.client_id', ''))).strip()

def get_workspaces(self):

q = request.params.get('incomplete', '')
limit = request.params.get('limit', '10')

plugin = get_plugin('wirecloud_view')

# Create a OAuth2 Session
token = toolkit.c.usertoken
oauth = OAuth2Session(self.client_id, token=token)
# Request workspaces
wirecloud_response = oauth.get(urljoin(plugin.wirecloud_url, "api/search") + "?namespace=workspace&q=" + quote_plus(q) + "&maxresults=" + quote_plus(limit))
dashboards = wirecloud_response.json()['results']

response.headers[b'Content-Type'] = b"application/json"
return json.dumps({
"ResultSet": {
"Result": [{"Name": "%s/%s" % (dashboard['owner'], dashboard['name'])} for dashboard in dashboards]
}
}).encode("utf-8")
61 changes: 0 additions & 61 deletions ckanext/wirecloudview/fanstatic/wirecloud_typeahead.js

This file was deleted.

22 changes: 5 additions & 17 deletions ckanext/wirecloudview/plugin.py
Expand Up @@ -68,9 +68,7 @@ def configure(self, config):

def update_config(self, config):
p.toolkit.add_template_directory(config, 'templates')
p.toolkit.add_resource('fanstatic', 'wirecloud_typeahead')
p.toolkit.add_resource('fanstatic', 'wirecloud_option')
p.toolkit.add_resource('fanstatic', 'wirecloud_view')
p.toolkit.add_resource(b'fanstatic', b'wirecloud_view')

def info(self):
return {
Expand All @@ -97,25 +95,15 @@ def form_template(self, context, data_dict):

def get_helpers(self):

def _get_workspaces():

# Create a OAuth2 Session
token = p.toolkit.c.usertoken
oauth = OAuth2Session(client_id, token=token)
# Request workspaces
response = oauth.get(urljoin(self.wirecloud_url, "api/workspaces") + '?access_token=%s' % token['access_token'])
return response.text

return {
'get_workspaces': _get_workspaces,
'get_editor_url': lambda: urljoin(self.wirecloud_url, self.editor_dashboard),
'get_dashboard_url': lambda dashboard, resourceid, ckanserver: urljoin(self.wirecloud_url, dashboard) + '?mode=embedded&resourceid=' + resourceid + '&ckanserver=' + ckanserver
}

def before_map(self, m):
# FIXME: Include all the content in the body of the request
m.connect('/wirecloud_view/resource/{resource_id}/view/{view_id}/workspace/{dashboard_path:.* ?}',
controller='ckanext.wirecloudview.plugin:WirecloudViewController',
action='notify_dashboard_path', conditions=POST)

m.connect('/api/3/wirecloud_view/dashboard_autocomplete',
controller='ckanext.wirecloudview.controller:WireCloudViewController',
action='get_workspaces', conditions=GET)

return m
5 changes: 2 additions & 3 deletions ckanext/wirecloudview/templates/wirecloud_form.html
@@ -1,10 +1,9 @@
{% import 'macros/form.html' as form %}

{% resource 'wirecloud_typeahead/wirecloud_typeahead.js' %}
{% resource 'wirecloud_option/wirecloud_option.js' %}
{% resource 'wirecloud_view/wirecloud_option.js' %}
{% resource 'wirecloud_view/wirecloud_view.css' %}

{{ form.input('dashboard', id='field-dashboard', label=_('Dashboard'), placeholder=_('eg. myuser/dashboard'), value=data.dashboard, attrs={"autocomplete": "off", "data-module": "wirecloud_typeahead", "data-module-workspaces": h.get_workspaces()}, error=errors.dashboard, classes=['control-full', 'control-large']) }}
{{ form.input('dashboard', id='field-dashboard', label=_('Dashboard'), placeholder=_('eg. myuser/dashboard'), value=data.dashboard, error=errors.dashboard, attrs={'data-module': 'autocomplete', 'data-module-source': '/api/3/wirecloud_view/dashboard_autocomplete?incomplete=?'}) }}

<div class="option small" data-module="wirecloud_option">
<span class="wc_sp">Create a new dashboard</span>
Expand Down

0 comments on commit aaa1d71

Please sign in to comment.