def wrapper(self, *args, **kwargs): owner_user = User.objects.get(username=owner) if shared: base = self.shared_test_data_dir else: base = self.test_data_dir with open(os.path.join(base, file_name), 'rb') as f: wgt = 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_user, file_contents=extra_resource_contents) buildWorkspaceFromTemplate(template, owner_user) return test_func(self, *args, **kwargs)
def deploy_tenant_ac(request): result = _parse_ac_request(request) if isinstance(result, HttpResponse): return result id_4CaaSt, wgt_file, fileURL = result # Process 4CaaSt Id username = parse_username(id_4CaaSt) user = get_object_or_404(User, username=username) # Install uploaded MAC resource try: resource = install_resource_to_user(user, file_contents=wgt_file) except TemplateParseException as e: return build_error_response(request, 400, e.msg) # Create a workspace if the resource is a mashup if resource.resource_type() == 'mashup' and not Workspace.objects.filter(creator=user, name=resource.short_name).exists(): buildWorkspaceFromTemplate(resource.get_template(), user, True) return HttpResponse(status=204)
def deploy_tenant_ac(request): result = _parse_ac_request(request) if isinstance(result, HttpResponse): return result id_4CaaSt, wgt_file, fileURL = result # Process 4CaaSt Id username = parse_username(id_4CaaSt) user = get_object_or_404(User, username=username) # Install uploaded MAC resource try: resource = install_resource_to_user(user, file_contents=wgt_file) except TemplateParseException as e: return build_error_response(request, 400, e.msg) # Create a workspace if the resource is a mashup if resource.resource_type() == 'mashup' and not Workspace.objects.filter( creator=user, name=resource.short_name).exists(): buildWorkspaceFromTemplate(resource.get_template(), user, True) return HttpResponse(status=204)
def wrapper(self, *args, **kwargs): owner_user = User.objects.get(username=owner) if shared: base = self.shared_test_data_dir else: base = self.test_data_dir with open(os.path.join(base, file_name), 'rb') as f: wgt = 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_user, file_contents=extra_resource_contents) buildWorkspaceFromTemplate(template, owner_user) return test_func(self, *args, **kwargs)
def test_bloqued_connections_rdf(self): template = self.read_template('wt2.rdf') workspace, _junk = buildWorkspaceFromTemplate(template, self.user) wiring_status = json.loads(workspace.wiringStatus) self.assertEqual(len(wiring_status['connections']), 1) self.assertEqual(wiring_status['connections'][0]['readOnly'], True)
def test_build_workspace_from_rdf_template(self): workspace, _junk = buildWorkspaceFromTemplate(self.rdfTemplate1, self.user) get_global_workspace_data(self.workspace, self.user) wiring_status = json.loads(workspace.wiringStatus) self.assertEqual(len(wiring_status['connections']), 1) self.assertEqual(wiring_status['connections'][0]['readOnly'], False)
def test_build_workspace_from_rdf_template_utf8_char(self): template = self.read_template('wt4.rdf') workspace, _junk = buildWorkspaceFromTemplate(template, self.user) data = json.loads(get_global_workspace_data(workspace, self.user).get_data()) for t in data['tabs']: self.assertEqual(t['name'][0:7], u'Pestaña')
def test_read_only_widgets(self): template = self.read_template('wt6.xml') workspace, _junk = buildWorkspaceFromTemplate(template, self.user) data = json.loads(get_global_workspace_data(workspace, self.user).get_data()) self.assertEqual(len(data['tabs'][0]['iwidgets']), 3) self.assertEqual(data['tabs'][0]['iwidgets'][0]['readOnly'], True)
def wrapper(self, *args, **kwargs): owner_user = User.objects.get(username=owner) if shared: base = self.shared_test_data_dir else: base = self.test_data_dir wgt_file = open(os.path.join(base, file_name), "rb") wgt = WgtFile(wgt_file) template = wgt.get_template() buildWorkspaceFromTemplate(template, owner_user) wgt_file.close() return test_func(self, *args, **kwargs)
def start_tenant_ac(request): id_4CaaSt, wgt_file, fileURL = _parse_ac_request(request) # Process 4CaaSt Id username = parse_username(id_4CaaSt) user = get_object_or_404(User, username=username) try: if user.tenantprofile_4CaaSt.id_4CaaSt != id_4CaaSt: raise Http404 except TenantProfile.DoesNotExist: raise Http404 # Create a workspace if the resource is a mashup template = TemplateParser(wgt_file.get_template()) if template.get_resource_type() == 'mashup' and not Workspace.objects.filter(creator=user, name=template.get_resource_info()['display_name']).exists(): buildWorkspaceFromTemplate(template, user, True) return HttpResponse(status=204)
def sync_base_workspaces(user): from wirecloud.platform.workspace.mashupTemplateParser import buildWorkspaceFromTemplate packageLinker = PackageLinker() reload_showcase = False managers = get_workspace_managers() workspaces_by_manager = {} workspaces_by_ref = {} for manager in managers: workspaces_by_manager[manager.get_id()] = [] workspaces_by_ref[manager.get_id()] = {} workspaces = UserWorkspace.objects.filter(user=user) for workspace in workspaces: if workspace.manager != '': workspaces_by_manager[workspace.manager].append(workspace.reason_ref) workspaces_by_ref[workspace.manager][workspace.reason_ref] = workspace for manager in managers: current_workspaces = workspaces_by_manager[manager.get_id()] result = manager.update_base_workspaces(user, current_workspaces) for workspace_to_remove in result[0]: user_workspace = workspaces_by_ref[manager.get_id()][workspace_to_remove] workspace = user_workspace.workspace user_workspace.delete() if workspace.userworkspace_set.count() == 0: workspace.delete() for workspace_to_add in result[1]: from_workspace = workspace_to_add[1] if isinstance(from_workspace, Workspace): user_workspace = packageLinker.link_workspace(from_workspace, user, from_workspace.creator) elif isinstance(from_workspace, PublishedWorkspace): _junk, user_workspace = buildWorkspaceFromTemplate(from_workspace.template, user) else: # TODO warning continue user_workspace.manager = manager.get_id() user_workspace.reason_ref = workspace_to_add[0] user_workspace.save() reload_showcase = True return reload_showcase
def create_workspace(owner, f=None, mashup=None, new_name=None, new_title=None, preferences={}, searchable=True, public=False): from wirecloud.platform.workspace.mashupTemplateParser import buildWorkspaceFromTemplate if mashup is not None and f is not None: raise Exception 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, new_title=new_title, searchable=searchable, public=public) if len(preferences) > 0: update_workspace_preferences(workspace, preferences, invalidate_cache=False) return workspace
def test_complex_workspaces_rdf(self): workspace, _junk = buildWorkspaceFromTemplate(self.rdfTemplate3, self.user) data = get_global_workspace_data(workspace, self.user).get_data() self.assertEqual(len(data['tabs']), 4) self.assertEqual(data['tabs'][0]['name'], u'Tab') self.assertEqual(len(data['tabs'][0]['iwidgets']), 1) self.assertEqual(data['tabs'][1]['name'], u'Tab 2') self.assertEqual(len(data['tabs'][1]['iwidgets']), 1) self.assertEqual(data['tabs'][2]['name'], u'Tab 3') self.assertEqual(len(data['tabs'][2]['iwidgets']), 0) self.assertEqual(data['tabs'][3]['name'], u'Tab 4') self.assertEqual(len(data['tabs'][3]['iwidgets']), 0) wiring = data['wiring'] self.assertEqual(len(wiring['connections']), 1) self.assertEqual(wiring['connections'][0]['source']['type'], 'iwidget') self.assertEqual(wiring['connections'][0]['source']['endpoint'], 'event') self.assertEqual(wiring['connections'][0]['target']['type'], 'iwidget') self.assertEqual(wiring['connections'][0]['target']['endpoint'], 'slot')
def create_workspace(owner, f): 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': 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) workspace, _ = buildWorkspaceFromTemplate(template, owner) return workspace
def create_workspace(owner, f): 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': 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) workspace, _ = buildWorkspaceFromTemplate(template, owner) return workspace
def test_complex_workspaces(self): template3 = self.read_template('wt3.xml') workspace, _junk = buildWorkspaceFromTemplate(template3, self.user) data = json.loads(get_global_workspace_data(workspace, self.user).get_data()) self.assertEqual(len(data['tabs']), 4) self.assertEqual(data['tabs'][0]['name'], 'Tab') self.assertEqual(len(data['tabs'][0]['iwidgets']), 1) self.assertEqual(data['tabs'][1]['name'], 'Tab 2') self.assertEqual(len(data['tabs'][1]['iwidgets']), 1) self.assertEqual(data['tabs'][2]['name'], 'Tab 3') self.assertEqual(len(data['tabs'][2]['iwidgets']), 0) self.assertEqual(data['tabs'][3]['name'], 'Tab 4') self.assertEqual(len(data['tabs'][3]['iwidgets']), 0) wiring_status = data['wiring'] self.assertEqual(len(wiring_status['operators']), 1) self.assertEqual(len(wiring_status['connections']), 1) self.assertEqual(wiring_status['connections'][0]['source']['type'], 'iwidget') self.assertEqual(wiring_status['connections'][0]['source']['endpoint'], 'event') self.assertEqual(wiring_status['connections'][0]['target']['type'], 'iwidget') self.assertEqual(wiring_status['connections'][0]['target']['endpoint'], 'slot')
def test_build_workspace_from_rdf_template(self): template = self.read_template('wt1.rdf') workspace, _junk = buildWorkspaceFromTemplate(template, self.user) self.check_basic_workspace_structure(workspace)
username = parse_username(id_4CaaSt) user = get_object_or_404(User, username=username) # Install uploaded MAC resource try: resource = install_resource_to_user(user, file_contents=wgt_file, templateURL=fileURL, packaged=True) except TemplateParseException, e: return build_error_response(request, 400, unicode(e.msg)) # Create a workspace if the resource is a mashup if resource.resource_type() == 'mashup' and not Workspace.objects.filter(creator=user, name=resource.display_name).exists(): buildWorkspaceFromTemplate(resource.get_template(), user, True) return HttpResponse(status=204) @require_POST @commit_on_http_success def undeploy_tenant_ac(request): result = _parse_ac_request(request) if isinstance(result, HttpResponse): return result id_4CaaSt, wgt_file, fileURL = result # Process 4CaaSt Id username = parse_username(id_4CaaSt)
def create_workspace(owner, f=None, mashup=None, new_name=None, new_title=None, preferences={}, searchable=True, public=False): from wirecloud.platform.workspace.mashupTemplateParser import buildWorkspaceFromTemplate if mashup is not None and f is not None: raise Exception 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, new_title=new_title, searchable=searchable, public=public) if len(preferences) > 0: update_workspace_preferences(workspace, preferences, invalidate_cache=False) return workspace
def create(self, request): data = parse_json_request(request) workspace_name = data.get('name', '').strip() workspace_title = data.get('title', '').strip() workspace_id = data.get('workspace', '') mashup_id = data.get('mashup', '') initial_pref_values = data.get('preferences', {}) allow_renaming = normalize_boolean_param( request, 'allow_renaming', data.get('allow_renaming', False)) dry_run = normalize_boolean_param(request, 'dry_run', data.get('dry_run', False)) if mashup_id == '' and workspace_id == '' and (workspace_name == '' and workspace_title == ''): return build_error_response(request, 422, _('Missing name or title parameter')) elif mashup_id != '' and workspace_id != '': return build_error_response( request, 422, _('Workspace and mashup parameters cannot be used at the same time' )) if mashup_id == '' and workspace_id == '': if workspace_title == '': workspace_title = workspace_name if workspace_name != '' and not is_valid_name(workspace_name): return build_error_response(request, 422, _('invalid workspace name')) if dry_run: return HttpResponse(status=204) try: workspace = createEmptyWorkspace(workspace_title, request.user, name=workspace_name, allow_renaming=allow_renaming) except IntegrityError: msg = _('A workspace with the given name already exists') return build_error_response(request, 409, msg) else: if mashup_id != '': values = mashup_id.split('/', 3) if len(values) != 3: return build_error_response(request, 422, _('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( request.user ) or resource.resource_type() != 'mashup': raise CatalogueResource.DoesNotExist except CatalogueResource.DoesNotExist: return build_error_response( request, 422, _('Mashup not found: %(mashup_id)s') % {'mashup_id': mashup_id}) 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()) else: from_ws = get_object_or_404(Workspace, id=workspace_id) if from_ws.public is False and not request.user.is_superuser and from_ws.creator != request.user: return build_error_response( request, 403, _('You are not allowed to read from workspace %s') % workspace_id) options = { 'vendor': 'api', 'name': from_ws.name, 'version': '1.0', 'title': from_ws.title if from_ws.title is not None and from_ws.title.strip() != "" else from_ws.name, 'description': 'Temporal mashup for the workspace copy operation', 'email': '*****@*****.**', } template = TemplateParser( build_json_template_from_workspace(options, from_ws, from_ws.creator)) try: check_mashup_dependencies(template, request.user) except MissingDependencies as e: details = { 'missingDependencies': e.missing_dependencies, } return build_error_response(request, 422, e, details=details) if dry_run: return HttpResponse(status=204) try: workspace, _junk = buildWorkspaceFromTemplate( template, request.user, allow_renaming=allow_renaming, new_name=workspace_name, new_title=workspace_title) except IntegrityError: msg = _('A workspace with the given name already exists') return build_error_response(request, 409, msg) if len(initial_pref_values) > 0: update_workspace_preferences(workspace, initial_pref_values, invalidate_cache=False) workspace_data = get_global_workspace_data(workspace, request.user) return workspace_data.get_response(status_code=201, cacheable=False)
def create(self, request): data = parse_json_request(request) workspace_name = data.get('name', '').strip() workspace_id = data.get('workspace', '') mashup_id = data.get('mashup', '') initial_pref_values = data.get('preferences', {}) allow_renaming = normalize_boolean_param(request, 'allow_renaming', data.get('allow_renaming', False)) dry_run = normalize_boolean_param(request, 'dry_run', data.get('dry_run', False)) if mashup_id == '' and workspace_id == '' and workspace_name == '': return build_error_response(request, 422, _('Missing name parameter')) elif mashup_id != '' and workspace_id != '': return build_error_response(request, 422, _('Workspace and mashup parameters cannot be used at the same time')) if mashup_id == '' and workspace_id == '': if not is_valid_name(workspace_name): return build_error_response(request, 422, _('invalid workspace name')) if dry_run: return HttpResponse(status=204) try: workspace = createEmptyWorkspace(workspace_name, request.user, allow_renaming=allow_renaming) except IntegrityError: msg = _('A workspace with the given name already exists') return build_error_response(request, 409, msg) else: if mashup_id != '': values = mashup_id.split('/', 3) if len(values) != 3: return build_error_response(request, 422, _('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(request.user) or resource.resource_type() != 'mashup': raise CatalogueResource.DoesNotExist except CatalogueResource.DoesNotExist: return build_error_response(request, 422, _('Mashup not found: %(mashup_id)s') % {'mashup_id': mashup_id}) 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()) else: from_ws = get_object_or_404(Workspace, id=workspace_id) if from_ws.public is False and not request.user.is_superuser and from_ws.creator != request.user: return build_error_response(request, 403, _('You are not allowed to read from workspace %s') % workspace_id) options = { 'vendor': 'api', 'name': from_ws.name, 'version': '1.0', 'title': '', 'description': 'Temporal mashup for the workspace copy operation', 'email': '*****@*****.**', } template = TemplateParser(build_json_template_from_workspace(options, from_ws, from_ws.creator)) try: check_mashup_dependencies(template, request.user) except MissingDependencies as e: details = { 'missingDependencies': e.missing_dependencies, } return build_error_response(request, 422, e, details=details) if dry_run: return HttpResponse(status=204) if workspace_name == '': workspace_name = None try: workspace, _junk = buildWorkspaceFromTemplate(template, request.user, allow_renaming=allow_renaming, new_name=workspace_name) except IntegrityError: msg = _('A workspace with the given name already exists') return build_error_response(request, 409, msg) if len(initial_pref_values) > 0: update_workspace_preferences(workspace, initial_pref_values, invalidate_cache=False) workspace_data = get_global_workspace_data(workspace, request.user) return workspace_data.get_response(status_code=201, cacheable=False)
def test_build_workspace_from_rdf_template_utf8_char(self): workspace, _junk = buildWorkspaceFromTemplate(self.rdfTemplate4, self.user) data = get_global_workspace_data(workspace, self.user).get_data() for t in data['tabs']: self.assertEqual(t['name'][0:7], u'Pestaña')
def test_workspace_with_params_rdf(self): template = self.read_template('wt5.rdf') workspace, _junk = buildWorkspaceFromTemplate(template, self.user) self.check_workspace_with_params(workspace)
def test_blocked_connections(self): workspace, _junk = buildWorkspaceFromTemplate(self.template2, self.user) wiring_status = json.loads(workspace.wiringStatus) self.assertEqual(len(wiring_status['connections']), 1) self.assertEqual(wiring_status['connections'][0]['readOnly'], True)
username = parse_username(id_4CaaSt) user = get_object_or_404(User, username=username) # Install uploaded MAC resource try: resource = install_resource_to_user(user, file_contents=wgt_file, templateURL=fileURL, packaged=True) except TemplateParseException, e: return build_error_response(request, 400, unicode(e.msg)) # Create a workspace if the resource is a mashup if resource.resource_type() == 'mashup' and not Workspace.objects.filter(creator=user, name=resource.display_name).exists(): buildWorkspaceFromTemplate(template, user, True) return HttpResponse(status=204) @require_POST def undeploy_tenant_ac(request): id_4CaaSt, wgt_file, fileURL = _parse_ac_request(request) # Process 4CaaSt Id username = parse_username(id_4CaaSt) user = get_object_or_404(User, username=username) try: if user.tenantprofile_4CaaSt.id_4CaaSt != id_4CaaSt: raise Http404
template = TemplateParser(template) except: build_error_response(request, 424, _('Downloaded invalid resource description from: %(url)s') % {'url': resource.template_uri}) try: check_mashup_dependencies(template, request.user) except MissingDependencies, e: details = { 'missingDependencies': e.missing_dependencies, } return build_error_response(request, 422, unicode(e), details=details) if dry_run: return HttpResponse(status_code=204) workspace, _junk = buildWorkspaceFromTemplate(template, request.user, True) workspace_data = get_global_workspace_data(workspace, request.user) return workspace_data.get_response(status_code=201, cacheable=False) class WorkspaceEntry(Resource): @authentication_required def read(self, request, workspace_id): workspace = get_object_or_404(Workspace, users__id=request.user.id, pk=workspace_id) workspace_data = get_global_workspace_data(workspace, request.user) return workspace_data.get_response()