From f4a5ed45e716d9420a6dcbd5675f7da08c3f9251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Arranz?= Date: Tue, 12 Sep 2017 12:30:54 +0200 Subject: [PATCH] Create a BAE dashboard when creating the BAE marketplace. See #299 --- src/wirecloud/fiware/plugins.py | 4 ++ .../FiWare/BusinessAPIEcosystemView.js | 2 +- src/wirecloud/platform/markets/utils.py | 3 + src/wirecloud/platform/markets/views.py | 16 ++++-- src/wirecloud/platform/workspace/utils.py | 57 ++++++++++++++----- 5 files changed, 60 insertions(+), 22 deletions(-) diff --git a/src/wirecloud/fiware/plugins.py b/src/wirecloud/fiware/plugins.py index 457fb52790..40f9945acd 100644 --- a/src/wirecloud/fiware/plugins.py +++ b/src/wirecloud/fiware/plugins.py @@ -32,6 +32,7 @@ from wirecloud.platform.markets.utils import MarketManager from wirecloud.platform.models import CatalogueResource from wirecloud.platform.plugins import WirecloudPlugin, build_url_template +from wirecloud.platform.workspace.utils import create_workspace import wirecloud.fiware from wirecloud.fiware.marketAdaptor.views import get_market_adaptor, get_market_user_data @@ -72,6 +73,9 @@ def __init__(self, user, name, options): self._name = name self._options = options + def create(self, user, options): + create_workspace(user, mashup="CoNWeT/bae-marketplace/0.1.1", new_name=options['name'], preferences={'server_url': options['url']}) + def download_resource(self, user, url, endpoint): store = endpoint['store'] diff --git a/src/wirecloud/fiware/static/js/wirecloud/FiWare/BusinessAPIEcosystemView.js b/src/wirecloud/fiware/static/js/wirecloud/FiWare/BusinessAPIEcosystemView.js index c12b25ea7c..2dc8188617 100644 --- a/src/wirecloud/fiware/static/js/wirecloud/FiWare/BusinessAPIEcosystemView.js +++ b/src/wirecloud/fiware/static/js/wirecloud/FiWare/BusinessAPIEcosystemView.js @@ -57,7 +57,7 @@ var load = function load() { if (this.status === "unloaded") { this.status = "loading"; - var id = Wirecloud.workspacesByUserAndName.wirecloud.bae; + var id = Wirecloud.workspacesByUserAndName[this.desc.user][this.desc.name]; Wirecloud.loadWorkspace(id).then((workspace) => { this.loadWorkspace(workspace); this.loaded = "loaded"; diff --git a/src/wirecloud/platform/markets/utils.py b/src/wirecloud/platform/markets/utils.py index 5f3032ffda..f0f8c6a971 100644 --- a/src/wirecloud/platform/markets/utils.py +++ b/src/wirecloud/platform/markets/utils.py @@ -31,6 +31,9 @@ class MarketManager: def __init__(self, user, name, options): pass + def create(self, user, options): + pass + def publish_mashup(self, endpoint, published_workspace, user, publish_options): pass diff --git a/src/wirecloud/platform/markets/views.py b/src/wirecloud/platform/markets/views.py index 0e05f9f1bb..9daa62f140 100644 --- a/src/wirecloud/platform/markets/views.py +++ b/src/wirecloud/platform/markets/views.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2012-2016 CoNWeT Lab., Universidad Politécnica de Madrid +# Copyright (c) 2012-2017 CoNWeT Lab., Universidad Politécnica de Madrid # This file is part of Wirecloud. @@ -75,13 +75,14 @@ def create(self, request): if 'user' not in received_data['options'] or received_data['options']['user'] == request.user.username: user_entry = request.user - elif received_data['options'].get('user', None) is not None: - user_entry = User.objects.get(username=received_data['options']['user']) else: - user_entry = None + try: + user_entry = User.objects.get(username=received_data['options']['user']) + except: + return build_error_response(request, 422, _("invalid user option")) - if (user_entry is None or user_entry != request.user) and not request.user.is_superuser: - return build_error_response(request, 403, _("You don't have permissions for adding public marketplaces")) + if user_entry != request.user and not request.user.is_superuser: + return build_error_response(request, 403, _("You don't have permissions for adding marketplaces in name of other user")) if 'user' in received_data['options']: del received_data['options']['user'] @@ -91,6 +92,9 @@ def create(self, request): except IntegrityError: return build_error_response(request, 409, 'Market name already in use') + market_managers = get_market_managers(user_entry) + market_managers[user_entry.username + '/' + received_data['name']].create(user_entry, received_data['options']) + return HttpResponse(status=201) diff --git a/src/wirecloud/platform/workspace/utils.py b/src/wirecloud/platform/workspace/utils.py index 48ee9c4ec4..84847ac0ce 100644 --- a/src/wirecloud/platform/workspace/utils.py +++ b/src/wirecloud/platform/workspace/utils.py @@ -24,6 +24,7 @@ from copy import deepcopy from Crypto.Cipher import AES import json +import os import re from django.conf import settings @@ -33,6 +34,7 @@ import markdown import six +from wirecloud.catalogue import utils as catalogue from wirecloud.catalogue.models import CatalogueResource from wirecloud.commons.utils.cache import CacheableData from wirecloud.commons.utils.db import save_alternative @@ -44,7 +46,7 @@ from wirecloud.platform.context.utils import get_context_values from wirecloud.platform.iwidget.utils import parse_value_from_text from wirecloud.platform.localcatalogue.utils import install_resource_to_user -from wirecloud.platform.preferences.views import get_workspace_preference_values, get_tab_preference_values +from wirecloud.platform.preferences.views import get_workspace_preference_values, get_tab_preference_values, update_workspace_preferences from wirecloud.platform.models import IWidget, Tab, UserWorkspace, Workspace from wirecloud.platform.workspace.managers import get_workspace_managers @@ -592,25 +594,50 @@ def get_iwidget_data(iwidget, workspace, cache_manager=None, user=None): return data_ret -def create_workspace(owner, f): +def create_workspace(owner, f=None, mashup=None, new_name=None, preferences={}): from wirecloud.platform.workspace.mashupTemplateParser import buildWorkspaceFromTemplate - wgt = f if isinstance(f, WgtFile) else WgtFile(f) - template = TemplateParser(wgt.get_template()) - - resource_info = template.get_resource_processed_info(process_urls=False) - if resource_info["type"] != 'mashup': + if mashup is not None and f is not None: raise Exception - for embedded_resource in resource_info['embedded']: - if embedded_resource['src'].startswith('https://'): - resource_file = download_http_content(embedded_resource['src']) - else: - resource_file = BytesIO(wgt.read(embedded_resource['src'])) + if f is not None: + + wgt = f if isinstance(f, WgtFile) else WgtFile(f) + template = TemplateParser(wgt.get_template()) + + resource_info = template.get_resource_processed_info(process_urls=False) + if resource_info["type"] != 'mashup': + raise Exception + + for embedded_resource in resource_info['embedded']: + if embedded_resource['src'].startswith('https://'): + resource_file = download_http_content(embedded_resource['src']) + else: + resource_file = BytesIO(wgt.read(embedded_resource['src'])) + + extra_resource_contents = WgtFile(resource_file) + install_resource_to_user(owner, file_contents=extra_resource_contents) + else: + values = mashup.split('/', 3) + if len(values) != 3: + raise TypeError(_('invalid mashup id')) + + (mashup_vendor, mashup_name, mashup_version) = values + try: + resource = CatalogueResource.objects.get(vendor=mashup_vendor, short_name=mashup_name, version=mashup_version) + if not resource.is_available_for(owner) or resource.resource_type() != 'mashup': + raise CatalogueResource.DoesNotExist + except CatalogueResource.DoesNotExist: + raise Exception(_('Mashup not found: %(mashup)s') % {'mashup': mashup}) + + base_dir = catalogue.wgt_deployer.get_base_dir(mashup_vendor, mashup_name, mashup_version) + wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri)) + template = TemplateParser(wgt_file.get_template()) + + workspace, _foo = buildWorkspaceFromTemplate(template, owner, new_name=new_name) - extra_resource_contents = WgtFile(resource_file) - install_resource_to_user(owner, file_contents=extra_resource_contents) + if len(preferences) > 0: + update_workspace_preferences(workspace, preferences, invalidate_cache=False) - workspace, _ = buildWorkspaceFromTemplate(template, owner) return workspace