コード例 #1
0
ファイル: utils.py プロジェクト: Mognom/wirecloud
def check_packaged_resource(wgt_file, resource_info=None):

    if resource_info is None:
        template_contents = wgt_file.get_template()
        template = TemplateParser(template_contents)
        resource_info = template.get_resource_info()

    if resource_info['type'] == 'widget':
        code_url = resource_info['contents']['src']
        if not code_url.startswith(('http://', 'https://')):

            try:
                code = wgt_file.read(code_url)
            except KeyError:
                msg = _('Missing contents file: %(file_name)s.')
                raise InvalidContents(msg % {'file_name': code_url})

            try:
                code.decode(resource_info['contents']['charset'])
            except UnicodeDecodeError:
                msg = _('%(file_name)s was not encoded using the specified charset (%(charset)s according to the widget descriptor file).')
                raise InvalidContents(msg % {'file_name': code_url, 'charset': resource_info['contents']['charset']})

    check_invalid_doc_content(wgt_file, resource_info, 'longdescription')
    check_invalid_doc_content(wgt_file, resource_info, 'doc')
    check_invalid_doc_content(wgt_file, resource_info, 'changelog')
    check_invalid_embedded_resources(wgt_file, resource_info)
コード例 #2
0
ファイル: tests.py プロジェクト: ciniguez/FIREWA
    def test_basic_widget_creation_from_rdf(self):
        template_uri = "http://example.com/path/widget.rdf"
        template = self.read_template('template1.rdf')

        parser = TemplateParser(template)
        data = parser.get_resource_info()
        self.assertIn('requirements', data)
        self.assertItemsEqual(data['requirements'], ({'type': 'feature', 'name': 'Wirecloud'},))

        downloader.download_http_content.set_response(template_uri, template)
        downloader.download_http_content.set_response('http://example.com/path/test.html', BASIC_HTML_GADGET_CODE)
        widget = install_resource(template, template_uri, self.user, False).widget

        self.changeLanguage('en')
        data = get_widget_data(widget)
        self.assertEqual(data['uri'], 'Wirecloud/test/0.1')
        self.assertEqual(data['vendor'], 'Wirecloud')
        self.assertEqual(data['name'], 'test')
        self.assertEqual(data['version'], '0.1')

        self.assertEqual(data['variables']['prop']['label'], u'Property Label')
        self.assertEqual(data['variables']['prop']['aspect'], 'PROP')
        self.assertEqual(data['variables']['pref']['label'], u'Preference Label')
        self.assertEqual(data['variables']['pref']['value_options'], [[u'1', u'Option Name']])
        self.assertEqual(data['variables']['pref']['aspect'], 'PREF')
        self.assertEqual(data['variables']['event']['label'], u'Event Label')
        self.assertEqual(data['variables']['event']['aspect'], 'EVEN')
        self.assertEqual(data['variables']['slot']['label'], u'Slot Label')
        self.assertEqual(data['variables']['slot']['aspect'], 'SLOT')
コード例 #3
0
ファイル: views.py プロジェクト: fispace/wirecloud
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
    except TenantProfile.DoesNotExist:
        raise Http404

    # If the resource is a mashup, remove the assigned workspace
    template = TemplateParser(wgt_file.get_template())
    if template.get_resource_type() == 'mashup':
        Workspace.objects.filter(creator=user, name=template.get_resource_info()['display_name']).delete()

    # Uninstall de resource
    template = TemplateParser(wgt_file.get_template())
    resource = CatalogueResource.objects.get(vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version())
    resource.users.remove(user)

    return HttpResponse(status=204)
コード例 #4
0
ファイル: views.py プロジェクト: yasuhi526/wirecloud
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)

    user = get_object_or_404(User, username=username)
    try:
        if user.tenantprofile_4CaaSt.id_4CaaSt != id_4CaaSt:
            raise Http404
    except TenantProfile.DoesNotExist:
        raise Http404

    # If the resource is a mashup, remove the assigned workspace
    template = TemplateParser(wgt_file.get_template())
    if template.get_resource_type() == 'mashup':
        Workspace.objects.filter(
            creator=user, name=template.get_resource_info()['title']).delete()

    # Uninstall de resource
    template = TemplateParser(wgt_file.get_template())
    resource = CatalogueResource.objects.get(
        vendor=template.get_resource_vendor(),
        short_name=template.get_resource_name(),
        version=template.get_resource_version())
    resource.users.remove(user)

    return HttpResponse(status=204)
コード例 #5
0
ファイル: tests.py プロジェクト: katsikas/wirecloud
    def test_basic_operator_creation_from_rdf(self):
        template = self.read_template("operatorTemplate1.rdf")
        parser = TemplateParser(template)
        data = parser.get_resource_info()

        self.assertEqual(data["vendor"], "Wirecloud")
        self.assertEqual(data["name"], "test operator")
        self.assertEqual(data["type"], "operator")
        self.assertEqual(data["version"], "0.1")
        self.assertEqual(data["email"], "*****@*****.**")
        self.assertEqual(data["wiring"]["inputs"][0]["label"], "slot")
        self.assertEqual(data["wiring"]["inputs"][0]["type"], "text")
        self.assertEqual(data["wiring"]["inputs"][0]["friendcode"], "test_friend_code")
        self.assertEqual(data["wiring"]["outputs"][0]["label"], "event")
        self.assertEqual(data["wiring"]["outputs"][0]["type"], "text")
        self.assertEqual(data["wiring"]["outputs"][0]["friendcode"], "test_friend_code")
        self.assertEqual(data["preferences"][0]["label"], "Preference label")
        self.assertEqual(data["preferences"][0]["description"], "Preference description")
        self.assertEqual(data["preferences"][0]["default_value"], "value")
        self.assertEqual(len(data["js_files"]), 5)

        self.assertEqual(data["js_files"][0], "/examplecode1.js")
        self.assertEqual(data["js_files"][1], "/examplecode2.js")
        self.assertEqual(data["js_files"][2], "/examplecode3.js")
        self.assertEqual(data["js_files"][3], "/examplecode4.js")
        self.assertEqual(data["js_files"][4], "/examplecode5.js")
コード例 #6
0
ファイル: tests.py プロジェクト: rachmadagitam/apps.Wirecloud
    def test_basic_operator_creation_from_rdf(self):
        template = self.read_file('operatorTemplate1.rdf')
        parser = TemplateParser(template)
        data = parser.get_resource_info()

        self.assertEqual(data['vendor'], 'Wirecloud')
        self.assertEqual(data['name'], 'test operator')
        self.assertEqual(data['type'], 'operator')
        self.assertEqual(data['version'], '0.1')
        self.assertEqual(data['email'], '*****@*****.**')
        self.assertEqual(data['wiring']['inputs'][0]['label'], 'slot')
        self.assertEqual(data['wiring']['inputs'][0]['type'], 'text')
        self.assertEqual(data['wiring']['inputs'][0]['friendcode'], 'test_friend_code')
        self.assertEqual(data['wiring']['outputs'][0]['label'], 'event')
        self.assertEqual(data['wiring']['outputs'][0]['type'], 'text')
        self.assertEqual(data['wiring']['outputs'][0]['friendcode'], 'test_friend_code')
        self.assertEqual(data['preferences'][0]['label'], 'Preference label')
        self.assertEqual(data['preferences'][0]['description'], 'Preference description')
        self.assertEqual(data['preferences'][0]['default'], 'value')
        self.assertEqual(len(data['js_files']), 5)

        self.assertEqual(data['js_files'][0], '/examplecode1.js')
        self.assertEqual(data['js_files'][1], '/examplecode2.js')
        self.assertEqual(data['js_files'][2], '/examplecode3.js')
        self.assertEqual(data['js_files'][3], '/examplecode4.js')
        self.assertEqual(data['js_files'][4], '/examplecode5.js')
コード例 #7
0
    def test_basic_operator_creation_from_rdf(self):
        template = self.read_file('operatorTemplate1.rdf')
        parser = TemplateParser(template)
        data = parser.get_resource_info()

        self.assertEqual(data['vendor'], 'Wirecloud')
        self.assertEqual(data['name'], 'test operator')
        self.assertEqual(data['type'], 'operator')
        self.assertEqual(data['version'], '0.1')
        self.assertEqual(data['email'], '*****@*****.**')
        self.assertEqual(data['wiring']['inputs'][0]['label'], 'slot')
        self.assertEqual(data['wiring']['inputs'][0]['type'], 'text')
        self.assertEqual(data['wiring']['inputs'][0]['friendcode'],
                         'test_friend_code')
        self.assertEqual(data['wiring']['outputs'][0]['label'], 'event')
        self.assertEqual(data['wiring']['outputs'][0]['type'], 'text')
        self.assertEqual(data['wiring']['outputs'][0]['friendcode'],
                         'test_friend_code')
        self.assertEqual(data['preferences'][0]['label'], 'Preference label')
        self.assertEqual(data['preferences'][0]['description'],
                         'Preference description')
        self.assertEqual(data['preferences'][0]['default'], 'value')
        self.assertEqual(len(data['js_files']), 5)

        self.assertEqual(data['js_files'][0], '/examplecode1.js')
        self.assertEqual(data['js_files'][1], '/examplecode2.js')
        self.assertEqual(data['js_files'][2], '/examplecode3.js')
        self.assertEqual(data['js_files'][3], '/examplecode4.js')
        self.assertEqual(data['js_files'][4], '/examplecode5.js')
コード例 #8
0
ファイル: plugins.py プロジェクト: caernel/wirecloud
    def publish(self, endpoint, wgt_file, user, request=None, template=None):

        if template is None:
            template = TemplateParser(wgt_file.get_template())

        resource_info = template.get_resource_info()

        mimetypes = {
            'widget': 'application/x-widget+mashable-application-component',
            'operator': 'application/x-operator+mashable-application-component',
            'mashup': 'application/x-mashup+mashable-application-component',
        }

        store = endpoint['store']
        adaptor = get_market_adaptor(self._user, self._name)
        user_data = get_market_user_data(user, self._user, self._name)
        storeclient = adaptor.get_store(store)

        store_token_key = store + '/token'
        if store_token_key in user_data:
            token = user_data[store_token_key]
        else:
            token = user_data['idm_token']

        storeclient.upload_resource(
            resource_info['title'],
            resource_info['version'],
            "_".join((resource_info['vendor'], resource_info['name'], resource_info['version'])) + '.wgt',
            resource_info['description'],
            mimetypes[resource_info['type']],
            wgt_file.get_underlying_file(),
            token
        )
コード例 #9
0
def check_mashup_dependencies(template, user):

    if not isinstance(template, TemplateParser):
        template = TemplateParser(template)

    missing_dependencies = []
    workspace_info = template.get_resource_info()

    for tab_entry in workspace_info['tabs']:
        for resource in tab_entry['resources']:
            try:
                catalogue_resource = CatalogueResource.objects.get(vendor=resource.get('vendor'), short_name=resource.get('name'), version=resource.get('version'))
                if not catalogue_resource.is_available_for(user):
                    raise CatalogueResource.DoesNotExist
            except CatalogueResource.DoesNotExist:
                missing_dependencies.append('/'.join((resource.get('vendor'), resource.get('name'), resource.get('version'))))

    for id_, op in workspace_info['wiring']['operators'].iteritems():
        (vendor, name, version) = op['name'].split('/')
        try:
            resource = CatalogueResource.objects.get(vendor=vendor, short_name=name, version=version)
            if not resource.is_available_for(user):
                raise CatalogueResource.DoesNotExist
        except CatalogueResource.DoesNotExist:
            missing_dependencies.append('/'.join((vendor, name, version)))

    if len(missing_dependencies) > 0:
        raise MissingDependencies(missing_dependencies)
コード例 #10
0
ファイル: views.py プロジェクト: yasuhi526/wirecloud
def _parse_ac_request(request):

    fileURL = None
    file_contents = None
    content_type = get_content_type(request)[0]

    data = parse_json_request(request)

    if 'url' not in data:
        return build_error_response(request, 400, _('Missing widget URL'))

    fileURL = data.get('url')
    id_4CaaSt = data.get('4CaaStID')

    if id_4CaaSt is None:
        return build_error_response(request, 400, _('Missing 4CaaStID'))

    if not isinstance(id_4CaaSt, string_types) or id_4CaaSt.strip() == '':
        return build_error_response(request, 400, _('Invalid 4CaaStID'))

    try:
        downloaded_file = download_http_content(fileURL)
    except:
        return build_error_response(
            request, 409,
            _('Mashable application component could not be downloaded'))

    downloaded_file = StringIO(downloaded_file)
    file_contents = WgtFile(downloaded_file)

    # Create a custom version of the resource
    template = TemplateParser(file_contents.get_template())
    template_info = template.get_resource_info()
    template_info['name'] += '@' + id_4CaaSt

    for pref_name, pref_value in six.iteritems(data.get('preferences', {})):
        for widget_pref_index, widget_pref in enumerate(
                template_info['preferences']):
            if widget_pref['name'] == pref_name:
                template_info['preferences'][widget_pref_index][
                    'readonly'] = True
                template_info['preferences'][widget_pref_index][
                    'value'] = pref_value
                break

    # Write a new Wgt file
    new_file = StringIO()
    zin = zipfile.ZipFile(downloaded_file, 'r')
    zout = zipfile.ZipFile(new_file, 'w')
    zout.writestr('config.xml', write_rdf_description(template_info))
    for item in zin.infolist():
        if item.filename == 'config.xml':
            continue
        zout.writestr(item, zin.read(item.filename))
    zin.close()
    zout.close()

    file_contents = WgtFile(new_file)

    return id_4CaaSt, file_contents, fileURL
コード例 #11
0
def check_packaged_resource(wgt_file, resource_info=None):

    if resource_info is None:
        template_contents = wgt_file.get_template()
        template = TemplateParser(template_contents)
        resource_info = template.get_resource_info()

    if resource_info['type'] == 'widget':
        code_url = resource_info['contents']['src']
        if not code_url.startswith(('http://', 'https://')):

            try:
                code = wgt_file.read(code_url)
            except KeyError:
                msg = _('Missing contents file: %(file_name)s.')
                raise InvalidContents(msg % {'file_name': code_url})

            try:
                code.decode(resource_info['contents']['charset'])
            except UnicodeDecodeError:
                msg = _(
                    '%(file_name)s was not encoded using the specified charset (%(charset)s according to the widget descriptor file).'
                )
                raise InvalidContents(
                    msg % {
                        'file_name': code_url,
                        'charset': resource_info['contents']['charset']
                    })

    check_invalid_doc_content(wgt_file, resource_info, 'longdescription')
    check_invalid_doc_content(wgt_file, resource_info, 'doc')
    check_invalid_doc_content(wgt_file, resource_info, 'changelog')
    check_invalid_embedded_resources(wgt_file, resource_info)
コード例 #12
0
ファイル: views.py プロジェクト: GreenIDer-Donati/wirecloud
def _parse_ac_request(request):

    fileURL = None
    file_contents = None
    content_type = get_content_type(request)[0]

    try:
        data = json.loads(request.body)
    except Exception as e:
        msg = _("malformed json data: %s") % unicode(e)
        return build_error_response(request, 400, msg)

    if 'url' not in data:
        return build_error_response(request, 400, _('Missing widget URL'))

    fileURL = data.get('url')
    id_4CaaSt = data.get('4CaaStID')

    if id_4CaaSt is None:
        return build_error_response(request, 400, _('Missing 4CaaStID'))

    if not isinstance(id_4CaaSt, string_types) or id_4CaaSt.strip() == '':
        return build_error_response(request, 400, _('Invalid 4CaaStID'))

    try:
        downloaded_file = download_http_content(fileURL)
    except:
        return build_error_response(request, 409, _('Mashable application component could not be downloaded'))

    downloaded_file = StringIO(downloaded_file)
    file_contents = WgtFile(downloaded_file)

    # Create a custom version of the resource
    template = TemplateParser(file_contents.get_template())
    template_info = template.get_resource_info()
    template_info['name'] += '@' + id_4CaaSt

    for pref_name, pref_value in six.iteritems(data.get('preferences', {})):
        for widget_pref_index, widget_pref in enumerate(template_info['preferences']):
            if widget_pref['name'] == pref_name:
                template_info['preferences'][widget_pref_index]['readonly'] = True
                template_info['preferences'][widget_pref_index]['value'] = pref_value
                break

    # Write a new Wgt file
    new_file = StringIO()
    zin = zipfile.ZipFile(downloaded_file, 'r')
    zout = zipfile.ZipFile(new_file, 'w')
    zout.writestr('config.xml', write_rdf_description(template_info))
    for item in zin.infolist():
        if item.filename == 'config.xml':
            continue
        zout.writestr(item, zin.read(item.filename))
    zin.close()
    zout.close()

    file_contents = WgtFile(new_file)

    return id_4CaaSt, file_contents, fileURL
コード例 #13
0
def add_packaged_resource(file,
                          user,
                          wgt_file=None,
                          template=None,
                          deploy_only=False):

    close_wgt = False
    if wgt_file is None:
        wgt_file = WgtFile(file)
        close_wgt = True

    if template is None:
        template_contents = wgt_file.get_template()
        template = TemplateParser(template_contents)

    resource_info = template.get_resource_info()

    resource_id = (
        resource_info['vendor'],
        resource_info['name'],
        resource_info['version'],
    )
    file_name = '_'.join(resource_id) + '.wgt'

    check_packaged_resource(wgt_file, resource_info)

    local_dir = wgt_deployer.get_base_dir(*resource_id)
    local_wgt = os.path.join(local_dir, file_name)

    if not os.path.exists(local_dir):
        os.makedirs(local_dir)

    overrides = extract_resource_media_from_package(template, wgt_file,
                                                    local_dir)
    if close_wgt:
        wgt_file.close()

    f = open(local_wgt, "wb")
    file.seek(0)
    f.write(file.read())
    f.close()

    if not deploy_only:
        resource_info.update(overrides)

        resource = CatalogueResource.objects.create(
            short_name=resource_info['name'],
            vendor=resource_info['vendor'],
            version=resource_info['version'],
            type=CatalogueResource.RESOURCE_TYPES.index(resource_info['type']),
            creator=user,
            template_uri=file_name,
            creation_date=now(),
            popularity='0.0',
            json_description=json.dumps(resource_info))

        return resource
コード例 #14
0
ファイル: utils.py プロジェクト: GreenIDer-Donati/wirecloud
def create_widget_from_template(template, user, request=None, base=None):

    """Creates a widget from a template"""

    if isinstance(template, TemplateParser):
        parser = template
    else:
        template_content = download_http_content(template, user=user)
        if base is None:
            base = template
        parser = TemplateParser(template_content, base=base)

    if parser.get_resource_type() != 'widget':
        raise Exception()

    widget_info = parser.get_resource_info()
    check_requirements(widget_info)

    widget = Widget()
    widget.resource = CatalogueResource.objects.get(vendor=parser.get_resource_vendor(), short_name=parser.get_resource_name(), version=parser.get_resource_version())
    widget_code = parser.get_absolute_url(widget_info['contents']['src'], base)
    widget.xhtml = XHTML.objects.create(
        uri=widget.uri + "/xhtml",
        url=widget_code,
        content_type=widget_info['contents']['contenttype'],
        use_platform_style=widget_info['contents']['useplatformstyle'],
        cacheable=widget_info['contents']['cacheable']
    )

    widget.width = widget_info['widget_width']
    widget.height = widget_info['widget_height']

    widget.save()

    for preference in widget_info['preferences']:
        vDef = VariableDef.objects.create(
            name=preference['name'],
            type=parser.typeText2typeCode(preference['type']),
            aspect='PREF',
            readonly=preference['readonly'],
            default_value=preference['default'],
            value=preference['value'],
            widget=widget,
            secure=preference['secure']
        )

    for prop in widget_info['properties']:
        vDef = VariableDef.objects.create(
            name=prop['name'],
            type=parser.typeText2typeCode(prop['type']),
            aspect='PROP',
            default_value=prop['default'],
            widget=widget,
            secure=prop['secure'],
        )

    return widget
コード例 #15
0
ファイル: utils.py プロジェクト: Fiware/apps.Wirecloud
def add_packaged_resource(file, user, wgt_file=None, template=None, deploy_only=False):

    close_wgt = False
    if wgt_file is None:
        wgt_file = WgtFile(file)
        close_wgt = True

    if template is None:
        template_contents = wgt_file.get_template()
        template = TemplateParser(template_contents)

    resource_info = template.get_resource_info()

    resource_id = (
        resource_info['vendor'],
        resource_info['name'],
        resource_info['version'],
    )
    file_name = '_'.join(resource_id) + '.wgt'

    check_packaged_resource(wgt_file, resource_info)

    local_dir = wgt_deployer.get_base_dir(*resource_id)
    local_wgt = os.path.join(local_dir, file_name)

    if not os.path.exists(local_dir):
        os.makedirs(local_dir)

    overrides = extract_resource_media_from_package(template, wgt_file, local_dir)
    if close_wgt:
        wgt_file.close()

    f = open(local_wgt, "wb")
    file.seek(0)
    f.write(file.read())
    f.close()

    if not deploy_only:
        resource_info.update(overrides)

        resource = CatalogueResource.objects.create(
            short_name=resource_info['name'],
            vendor=resource_info['vendor'],
            version=resource_info['version'],
            type=CatalogueResource.RESOURCE_TYPES.index(resource_info['type']),
            creator=user,
            template_uri=file_name,
            creation_date=now(),
            popularity='0.0',
            json_description=json.dumps(resource_info)
        )

        return resource
コード例 #16
0
ファイル: tests.py プロジェクト: aarranz/wirecloud
    def test_widget_with_unmet_requirements_rdf(self):

        template_uri = "http://example.com/path/widget.xml"
        template = self.read_template('template8.rdf')

        parser = TemplateParser(template)
        data = parser.get_resource_info()
        self.assertIn('requirements', data)
        self.assertItemsEqual(data['requirements'], ({'type': 'feature', 'name': 'nonexistent-feature'}, {'type': 'feature', 'name': 'Wirecloud'},))

        self.network._servers['http']['example.com'].add_response('GET', '/path/widget.xml', {'content': template})
        self.assertRaises(Exception, install_resource, template, template_uri, self.user, False)
        self.assertRaises(Widget.DoesNotExist, Widget.objects.get, resource__vendor='Example', resource__short_name='test', resource__version='0.1')
コード例 #17
0
ファイル: tests.py プロジェクト: aarranz/wirecloud
    def test_basic_widget_creation_from_rdf(self):
        template_uri = "http://example.com/path/widget.rdf"
        template = self.read_template('template1.rdf')

        parser = TemplateParser(template)
        data = parser.get_resource_info()
        self.assertIn('requirements', data)
        self.assertItemsEqual(data['requirements'], ({'type': 'feature', 'name': 'Wirecloud'},))

        self.network._servers['http']['example.com'].add_response('GET', '/path/widget.rdf', {'content': template})
        self.network._servers['http']['example.com'].add_response('GET', '/path/test.html', {'content': BASIC_HTML_GADGET_CODE})
        resource = install_resource(template, template_uri, self.user, False)

        self.check_basic_widget_info(resource)
コード例 #18
0
ファイル: utils.py プロジェクト: ngpJason/wirecloud
def update_resource_catalogue_cache(orm=None):

    if orm is not None:
        resources = orm.CatalogueResource.objects.all()
    else:
        resources = CatalogueResource.objects.all()

    resources_to_remove = []
    for resource in resources:

        try:

            if getattr(resource, 'fromWGT', True):
                base_dir = wgt_deployer.get_base_dir(resource.vendor,
                                                     resource.short_name,
                                                     resource.version)
                wgt_file = WgtFile(
                    os.path.join(base_dir, resource.template_uri))
                template = wgt_file.get_template()
                wgt_file.close()
            else:
                # fromWGT attribute support was removed from Wirecloud in version 0.7.0
                template = download_http_content(resource.template_uri)

            template_parser = TemplateParser(template)
            resource.json_description = json.dumps(
                template_parser.get_resource_info())
            resource.save()

        except (IOError, TemplateParseException) as e:

            if isinstance(e, IOError) and e.errno != errno.ENOENT:
                raise e

            resources_to_remove.append(resource)

    if len(resources_to_remove) > 0 and getattr(
            settings, 'WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION',
            False) is False:
        raise Exception(
            'There are some mashable application components that are not supported anymore (use WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION for removing automatically them in the migration process'
        )

    for resource in resources_to_remove:
        print('    Removing %s' %
              (resource.vendor + '/' + resource.short_name + '/' +
               resource.version))
        resource.delete()
コード例 #19
0
def fix_dev_version(wgt_file, user):

    template_contents = wgt_file.get_template()
    template = TemplateParser(template_contents)

    resource_info = template.get_resource_info()

    # Add user name to the version if the component is in development
    if '-dev' in resource_info['version']:

        # User name added this way to prevent users to upload a version
        # *.*-devAnotherUser that would be accepted but might collide with
        # AnotherUser's development version
        resource_info['version'] = re.sub('-dev.*$', '-dev' + user.username, resource_info['version'])
        template_string = write_json_description(resource_info)
        wgt_file.update_config(template_string)
コード例 #20
0
ファイル: tests.py プロジェクト: katsikas/wirecloud
    def test_basic_widget_creation_from_rdf(self):
        template_uri = "http://example.com/path/widget.rdf"
        template = self.read_template("template1.rdf")

        parser = TemplateParser(template)
        data = parser.get_resource_info()
        self.assertIn("requirements", data)
        self.assertItemsEqual(data["requirements"], ({"type": "feature", "name": "Wirecloud"},))

        self.network._servers["http"]["example.com"].add_response("GET", "/path/widget.rdf", {"content": template})
        self.network._servers["http"]["example.com"].add_response(
            "GET", "/path/test.html", {"content": BASIC_HTML_GADGET_CODE}
        )
        resource = install_resource(template, template_uri, self.user, False)

        self.check_basic_widget_info(resource)
コード例 #21
0
ファイル: utils.py プロジェクト: Fiware/apps.Wirecloud
def fix_dev_version(wgt_file, user):

    template_contents = wgt_file.get_template()
    template = TemplateParser(template_contents)

    resource_info = template.get_resource_info()

    # Add user name to the version if the component is in development
    if '-dev' in resource_info['version']:

        # User name added this way to prevent users to upload a version
        # *.*-devAnotherUser that would be accepted but might collide with
        # AnotherUser's development version
        resource_info['version'] = re.sub('-dev.*$', '-dev' + user.username, resource_info['version'])
        template_string = write_json_description(resource_info)
        wgt_file.update_config(template_string)
コード例 #22
0
ファイル: utils.py プロジェクト: Robinlovelace/wirecloud
def add_widget_from_wgt(file, user, wgt_file=None, template=None, deploy_only=False):

    close_wgt = False
    if wgt_file is None:
        wgt_file = WgtFile(file)
        close_wgt = True

    if template is None:
        template_contents = wgt_file.get_template()
        template = TemplateParser(template_contents)

    if template.get_resource_type() == 'widget':
        resource_info = template.get_resource_info()
        code_url = resource_info['code_url']
        if not code_url.startswith(('http://', 'https://')):
            code = wgt_file.read(code_url)
            try:
                unicode(code, resource_info['code_charset'])
            except UnicodeDecodeError:
                msg = _('%(file_name)s was not encoded using the specified charset (%(charset)s according to the widget descriptor file).')
                raise InvalidContents(msg % {'file_name': code_url, 'charset': resource_info['code_charset']})

    resource_id = (
        template.get_resource_vendor(),
        template.get_resource_name(),
        template.get_resource_version(),
    )
    file_name = '_'.join(resource_id) + '.wgt'
    local_dir = wgt_deployer.get_base_dir(*resource_id)
    local_wgt = os.path.join(local_dir, file_name)

    if not os.path.exists(local_dir):
        os.makedirs(local_dir)

    overrides = extract_resource_media_from_package(template, wgt_file, local_dir)
    if close_wgt:
        wgt_file.close()

    f = open(local_wgt, "wb")
    file.seek(0)
    f.write(file.read())
    f.close()

    if not deploy_only:
        return add_resource_from_template(file_name, template, user, fromWGT=True, overrides=overrides)
コード例 #23
0
ファイル: utils.py プロジェクト: GreenIDer-Donati/wirecloud
def update_resource_catalogue_cache(orm=None):

    if orm is not None:
        resources = orm.CatalogueResource.objects.all()
    else:
        resources = CatalogueResource.objects.all()

    resources_to_remove = []
    for resource in resources:

        try:

            if getattr(resource, "fromWGT", True):
                base_dir = wgt_deployer.get_base_dir(resource.vendor, resource.short_name, resource.version)
                wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
                template = wgt_file.get_template()
                wgt_file.close()
            else:
                # fromWGT attribute support was removed from Wirecloud in version 0.7.0
                template = download_http_content(resource.template_uri)

            template_parser = TemplateParser(template)
            resource.json_description = json.dumps(template_parser.get_resource_info())
            resource.save()

        except (IOError, TemplateParseException) as e:

            if isinstance(e, IOError) and e.errno != errno.ENOENT:
                raise e

            resources_to_remove.append(resource)

    if (
        len(resources_to_remove) > 0
        and getattr(settings, "WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION", False) is False
    ):
        raise Exception(
            "There are some mashable application components that are not supported anymore (use WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION for removing automatically them in the migration process"
        )

    for resource in resources_to_remove:
        print("    Removing %s" % (resource.vendor + "/" + resource.short_name + "/" + resource.version))
        resource.delete()
コード例 #24
0
ファイル: views.py プロジェクト: ciniguez/FIREWA
def stop_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

    # Remove assigned workspace if the resource is a mashup
    template = TemplateParser(wgt_file.get_template())
    if template.get_resource_type() == 'mashup':
        Workspace.objects.filter(creator=user, name=template.get_resource_info()['display_name']).delete()

    return HttpResponse(status=204)
コード例 #25
0
ファイル: tests.py プロジェクト: katsikas/wirecloud
    def test_widget_with_unmet_requirements_rdf(self):

        template_uri = "http://example.com/path/widget.xml"
        template = self.read_template("template8.rdf")

        parser = TemplateParser(template)
        data = parser.get_resource_info()
        self.assertIn("requirements", data)
        self.assertItemsEqual(
            data["requirements"],
            ({"type": "feature", "name": "nonexistent-feature"}, {"type": "feature", "name": "Wirecloud"}),
        )

        self.network._servers["http"]["example.com"].add_response("GET", "/path/widget.xml", {"content": template})
        self.assertRaises(Exception, install_resource, template, template_uri, self.user, False)
        self.assertRaises(
            Widget.DoesNotExist,
            Widget.objects.get,
            resource__vendor="Example",
            resource__short_name="test",
            resource__version="0.1",
        )
コード例 #26
0
ファイル: views.py プロジェクト: fispace/wirecloud
        return build_error_response(request, 400, _('Missing widget URL'))

    fileURL = data.get('url')
    id_4CaaSt = data.get('4CaaStID')

    try:
        downloaded_file = downloader.download_http_content(fileURL)
    except:
        return build_error_response(request, 409, _('Widget content could not be downloaded'))

    downloaded_file = StringIO(downloaded_file)
    file_contents = WgtFile(downloaded_file)

    # Create a custom version of the resource
    template = TemplateParser(file_contents.get_template())
    template_info = template.get_resource_info()
    template_info['name'] += '@' + id_4CaaSt

    for pref_name, pref_value in data.get('preferences', {}).iteritems():
        for widget_pref_index, widget_pref in enumerate(template_info['preferences']):
            if widget_pref['name'] == pref_name:
                template_info['preferences'][widget_pref_index]['readonly'] = True
                template_info['preferences'][widget_pref_index]['value'] = pref_value
                break

    # Write a new Wgt file
    new_file = StringIO()
    zin = zipfile.ZipFile(downloaded_file, 'r')
    zout = zipfile.ZipFile(new_file, 'w')
    zout.writestr('config.xml', write_rdf_description(template_info))
    for item in zin.infolist():
コード例 #27
0
def fillWorkspaceUsingTemplate(workspace, template):

    if not isinstance(template, TemplateParser):
        template = TemplateParser(template)

    if template.get_resource_type() != 'mashup':
        raise TypeError('Unsupported resource type: %s' % template.get_resource_type())

    user = workspace.creator

    context_values = get_context_values(workspace, workspace.creator)
    processor = TemplateValueProcessor({'user': user, 'context': context_values})

    mashup_description = template.get_resource_info()

    new_values = {}
    id_mapping = {
        'operator': {},
        'widget': {},
    }
    for preference_name in mashup_description['preferences']:
        # Filter public and sharelist preferences
        if preference_name in ("public", "sharelist"):
            continue

        new_values[preference_name] = {
            'inherit': False,
            'value': mashup_description['preferences'][preference_name],
        }

    if len(new_values) > 0:
        update_workspace_preferences(workspace, new_values)

    new_forced_values = {
        'extra_prefs': [],
        'iwidget': {},
        'ioperator': {},
    }
    for param in mashup_description['params']:
        new_forced_values['extra_prefs'].append({
            'name': param['name'],
            'inheritable': False,
            'label': param.get('label'),
            'type': param.get('type'),
            'description': param.get('description'),
            'required': param.get('required'),
        })

    for tab_entry in mashup_description['tabs']:
        tab = createTab(tab_entry.get('title'), workspace, name=tab_entry['name'], allow_renaming=True)

        new_values = {}
        for preference_name in tab_entry['preferences']:
            new_values[preference_name] = {
                'inherit': False,
                'value': tab_entry['preferences'][preference_name],
            }

        if len(new_values) > 0:
            update_tab_preferences(tab, new_values)

        for resource in tab_entry['resources']:

            position = resource['position']
            rendering = resource['rendering']

            widget = get_or_add_widget_from_catalogue(resource.get('vendor'), resource.get('name'), resource.get('version'), user)

            iwidget_data = {
                "widget": widget.uri,
                "title": resource.get('title'),
                "left": float(position.get('x')),
                "top": float(position.get('y')),
                "icon_left": 0,
                "icon_top": 0,
                "zIndex": int(position.get('z')),
                "width": float(rendering.get('width')),
                "height": float(rendering.get('height')),
                "layout": int(rendering.get('layout')),
                "minimized": rendering['minimized'],
                "fulldragboard": rendering['fulldragboard'],
                "titlevisible": rendering['titlevisible'],
            }

            iwidget = SaveIWidget(iwidget_data, user, tab, commit=False)
            if resource.get('readonly'):
                iwidget.readOnly = True

            initial_variable_values = {}
            iwidget_forced_values = {}
            iwidget_info = widget.resource.get_processed_info(process_variables=True)
            for prop_name in resource['properties']:
                prop = resource['properties'][prop_name]
                read_only = prop.get('readonly')
                if prop.get('value', None) is not None:
                    value = prop['value']
                else:
                    value = iwidget_info['variables']['properties'][prop_name]['default']
                if read_only:
                    iwidget_forced_values[prop_name] = {'value': value}
                else:
                    initial_variable_values[prop_name] = processor.process(value)

            for pref_name in resource['preferences']:
                pref = resource['preferences'][pref_name]
                read_only = pref.get('readonly')
                if pref.get('value', None) is not None:
                    value = pref['value']
                    if isinstance(value, dict):
                        value = value["users"].get("%s" % workspace.creator.id, iwidget_info['variables']['preferences'][pref_name]['default'])
                else:
                    value = iwidget_info['variables']['preferences'][pref_name]['default']

                # Build multiuser structure
                if read_only:
                    iwidget_forced_values[pref_name] = {'value': value, 'hidden': pref.get('hidden', False)}
                else:
                    initial_variable_values[pref_name] = processor.process(value)
            set_initial_values(iwidget, initial_variable_values, iwidget_info, workspace.creator)
            iwidget.save()

            if len(iwidget_forced_values) > 0:
                new_forced_values['iwidget'][str(iwidget.id)] = iwidget_forced_values

            id_mapping['widget'][resource.get('id')] = {
                'id': iwidget.id,
                'name': resource.get('vendor') + "/" + resource.get('name') + "/" + resource.get('version')
            }

    # wiring
    if len(workspace.wiringStatus) == 0:
        workspace.wiringStatus = get_wiring_skeleton()

    max_id = 0

    for id_ in workspace.wiringStatus['operators'].keys():
        if int(id_) > max_id:
            max_id = int(id_)

    # Process operators info
    for operator_id, operator in mashup_description['wiring']['operators'].items():
        max_id += 1
        new_id = "%s" % max_id
        id_mapping['operator'][operator_id] = {
            'id': new_id
        }
        workspace.wiringStatus['operators'][new_id] = {
            'id': new_id,
            'name': operator['name'],
            'preferences': operator['preferences'],
            'properties': {}
        }

        ioperator_forced_values = {}
        for pref_id, pref in operator['preferences'].items():
            if pref.get('readonly', False):
                ioperator_forced_values[pref_id] = {'value': pref.get('value'), 'hidden': pref.get('hidden', False)}

            workspace.wiringStatus['operators'][new_id]["preferences"][pref_id]["value"] = {'users': {"%s" % workspace.creator.id: pref["value"]}}

        if len(ioperator_forced_values) > 0:
            new_forced_values['ioperator'][new_id] = ioperator_forced_values

    # Remap connection ids
    source_mapping = {}
    target_mapping = {}

    for connection in mashup_description['wiring']['connections']:

        if not is_valid_connection(connection, id_mapping):
            continue

        old_source_name = get_endpoint_name(connection['source'])
        old_target_name = get_endpoint_name(connection['target'])

        connection['source']['id'] = map_id(connection['source'], id_mapping)
        connection['target']['id'] = map_id(connection['target'], id_mapping)

        source_mapping[old_source_name] = get_endpoint_name(connection['source'])
        target_mapping[old_target_name] = get_endpoint_name(connection['target'])

        # Add new connection
        workspace.wiringStatus['connections'].append(connection)

    # Merging visual description...
    _remap_component_ids(id_mapping, mashup_description['wiring']['visualdescription']['components'], isGlobal=True)
    _remap_connection_endpoints(source_mapping, target_mapping, mashup_description['wiring']['visualdescription'])

    # Remap mashup description behaviours' ids
    if len(mashup_description['wiring']['visualdescription']['behaviours']) != 0:
        for behaviour in mashup_description['wiring']['visualdescription']['behaviours']:
            _remap_component_ids(id_mapping, behaviour['components'])
            _remap_connection_endpoints(source_mapping, target_mapping, behaviour)

    if len(workspace.wiringStatus['visualdescription']['behaviours']) != 0 or len(mashup_description['wiring']['visualdescription']['behaviours']) != 0:
        if len(workspace.wiringStatus['visualdescription']['behaviours']) == 0 and not is_empty_wiring(workspace.wiringStatus['visualdescription']):
            # *TODO* flag to check if the user really want to merge both workspaces.
            _create_new_behaviour(workspace.wiringStatus['visualdescription'], _("Original wiring"), _("This is the wiring description of the original workspace"))

        if len(mashup_description['wiring']['visualdescription']['behaviours']) == 0:
            _create_new_behaviour(mashup_description['wiring']['visualdescription'], _("Merged wiring"), _("This is the wiring description of the merged mashup."))

        workspace.wiringStatus['visualdescription']['behaviours'] += mashup_description['wiring']['visualdescription']['behaviours']

    # Merge global behaviour components and connections
    workspace.wiringStatus['visualdescription']['components']['operator'].update(mashup_description['wiring']['visualdescription']['components']['operator'])
    workspace.wiringStatus['visualdescription']['components']['widget'].update(mashup_description['wiring']['visualdescription']['components']['widget'])
    workspace.wiringStatus['visualdescription']['connections'] += mashup_description['wiring']['visualdescription']['connections']

    # Forced values
    normalize_forced_values(workspace)

    workspace.forcedValues['extra_prefs'] += new_forced_values['extra_prefs']
    workspace.forcedValues['iwidget'].update(new_forced_values['iwidget'])
    workspace.forcedValues['ioperator'].update(new_forced_values['ioperator'])

    workspace.save()
コード例 #28
0
def fillWorkspaceUsingTemplate(workspace, template):

    if not isinstance(template, TemplateParser):
        template = TemplateParser(template)

    if template.get_resource_type() != 'mashup':
        raise Exception()

    user = workspace.creator

    user_workspace = UserWorkspace.objects.get(user=workspace.creator, workspace=workspace)
    context_values = get_context_values(user_workspace)
    processor = TemplateValueProcessor({'user': user, 'context': context_values})

    workspace_info = template.get_resource_info()

    new_values = {}
    iwidget_id_mapping = {}
    for preference_name in workspace_info['preferences']:
        new_values[preference_name] = {
            'inherit': False,
            'value': workspace_info['preferences'][preference_name],
        }

    if len(new_values) > 0:
        update_workspace_preferences(workspace, new_values)

    new_forced_values = {
        'extra_prefs': {},
        'iwidget': {},
    }
    for param_name in workspace_info['params']:
        param = workspace_info['params'][param_name]
        new_forced_values['extra_prefs'][param_name] = {
            'inheritable': False,
            'label': param.get('label'),
            'type': param.get('type'),
        }

    for tab_entry in workspace_info['tabs']:
        tab = createTab(tab_entry.get('name'), workspace, allow_renaming=True)

        new_values = {}
        for preference_name in tab_entry['preferences']:
            new_values[preference_name] = {
                'inherit': False,
                'value': tab_entry['preferences'][preference_name],
            }

        if len(new_values) > 0:
            update_tab_preferences(tab, new_values)

        for resource in tab_entry['resources']:

            position = resource['position']
            rendering = resource['rendering']

            initial_variable_values = {}
            iwidget_forced_values = {}
            for prop_name in resource['properties']:
                prop = resource['properties'][prop_name]
                read_only = prop.get('readonly')
                if read_only:
                    iwidget_forced_values[prop_name] = {'value': prop.get('value')}
                else:
                    initial_variable_values[prop_name] = processor.process(prop.get('value'))

            for pref_name in resource['preferences']:
                pref = resource['preferences'][pref_name]
                read_only = pref.get('readonly')
                if read_only:
                    iwidget_forced_values[pref_name] = {'value': pref.get('value'), 'hidden': pref.get('hidden', False)}
                else:
                    initial_variable_values[pref_name] = processor.process(pref.get('value'))

            widget = get_or_add_widget_from_catalogue(resource.get('vendor'), resource.get('name'), resource.get('version'), user, None)

            iwidget_data = {
                "left": int(position.get('x')),
                "top": int(position.get('y')),
                "icon_left": -1,
                "icon_top": -1,
                "zIndex": int(position.get('z')),
                "width": int(rendering.get('width')),
                "height": int(rendering.get('height')),
                "name": resource.get('title'),
                "layout": int(rendering.get('layout')),
                "widget": widget.uri,
            }

            iwidget = SaveIWidget(iwidget_data, user, tab, initial_variable_values)
            if resource.get('readonly'):
                iwidget.readOnly = True
                iwidget.save()

            new_forced_values['iwidget'][str(iwidget.id)] = iwidget_forced_values
            iwidget_id_mapping[resource.get('id')] = iwidget

    if workspace.forcedValues is not None and workspace.forcedValues != '':
        forced_values = json.loads(workspace.forcedValues)
    else:
        forced_values = {
            'extra_prefs': {},
            'iwidget': {},
        }

    forced_values['extra_prefs'].update(new_forced_values['extra_prefs'])
    forced_values['iwidget'].update(new_forced_values['iwidget'])
    workspace.forcedValues = json.dumps(forced_values, ensure_ascii=False)

    # wiring
    wiring_status = {
        'operators': {},
        'connections': [],
    }

    if workspace.wiringStatus != '':
        workspace_wiring_status = json.loads(workspace.wiringStatus)
    else:
        workspace_wiring_status = {
            'operators': {},
            'connections': [],
            'views': []
        }

    if 'views' not in workspace_wiring_status:
        workspace_wiring_status['views'] = []

    max_id = 0
    operators = {}

    for id_ in workspace_wiring_status['operators'].keys():
        if int(id_) > max_id:
            max_id = int(id_)

    # Change string ids by integer ids and install unavailable operators
    for id_, op in workspace_info['wiring']['operators'].iteritems():
        max_id += 1
        #mapping between string ids and integer id
        operators[id_] = max_id
        wiring_status['operators'][max_id] = {
            'id': max_id,
            'name': op['name']
        }
        op_id_args = op['name'].split('/')
        op_id_args.append(user)
        get_or_add_resource_from_available_marketplaces(*op_id_args)

    wiring_status['operators'].update(workspace_wiring_status['operators'])

    if workspace_wiring_status['connections']:
        wiring_status['connections'] = workspace_wiring_status['connections']

    for connection in workspace_info['wiring']['connections']:
        source_id = connection['source']['id']
        target_id = connection['target']['id']

        if connection['source']['type'] == 'iwidget':
            source_id = iwidget_id_mapping[source_id].id
        elif connection['source']['type'] == 'ioperator':
            source_id = operators[source_id]

        if connection['target']['type'] == 'iwidget':
            target_id = iwidget_id_mapping[target_id].id
        elif connection['target']['type'] == 'ioperator':
            target_id = operators[target_id]

        wiring_status['connections'].append({
            'readOnly': connection['readonly'],
            'source': {
                'id': source_id,
                'type': connection['source']['type'],
                'endpoint': connection['source']['endpoint'],
            },
            'target': {
                'id': target_id,
                'type': connection['target']['type'],
                'endpoint': connection['target']['endpoint'],
            },
        })

    wiring_status['views'] = workspace_wiring_status['views']

    if 'views' in workspace_info['wiring']:
        for wiring_view in workspace_info['wiring']['views']:
            iwidgets_views = {}
            for key, widget in wiring_view['iwidgets'].iteritems():
                iwidgets_views[iwidget_id_mapping[key].id] = widget

            operators_views = {}
            for key, operator in wiring_view['operators'].iteritems():
                operators_views[operators[key]] = operator

            wiring_status['views'].append({
                'iwidgets': iwidgets_views,
                'operators': operators_views,
                'label': wiring_view['label'],
                'multiconnectors': {},
                'connections': []
            })

    workspace.wiringStatus = json.dumps(wiring_status)

    workspace.save()
コード例 #29
0
ファイル: utils.py プロジェクト: rockneurotiko/wirecloud
def add_packaged_resource(file, user, wgt_file=None, template=None, deploy_only=False):

    close_wgt = False
    if wgt_file is None:
        wgt_file = WgtFile(file)
        close_wgt = True

    if template is None:
        template_contents = wgt_file.get_template()
        template = TemplateParser(template_contents)

    resource_info = template.get_resource_info()
    if resource_info['type'] == 'widget':
        code_url = resource_info['contents']['src']
        if not code_url.startswith(('http://', 'https://')):

            try:
                code = wgt_file.read(code_url)
            except KeyError:
                msg = _('Missing contents file: %(file_name)s.')
                raise InvalidContents(msg % {'file_name': code_url})

            try:
                unicode(code, resource_info['contents']['charset'])
            except UnicodeDecodeError:
                msg = _('%(file_name)s was not encoded using the specified charset (%(charset)s according to the widget descriptor file).')
                raise InvalidContents(msg % {'file_name': code_url, 'charset': resource_info['contents']['charset']})

    resource_id = (
        template.get_resource_vendor(),
        template.get_resource_name(),
        template.get_resource_version(),
    )
    file_name = '_'.join(resource_id) + '.wgt'
    local_dir = wgt_deployer.get_base_dir(*resource_id)
    local_wgt = os.path.join(local_dir, file_name)

    if not os.path.exists(local_dir):
        os.makedirs(local_dir)

    overrides = extract_resource_media_from_package(template, wgt_file, local_dir)
    if close_wgt:
        wgt_file.close()

    f = open(local_wgt, "wb")
    file.seek(0)
    f.write(file.read())
    f.close()

    if not deploy_only:
        resource_info.update(overrides)

        resource = CatalogueResource.objects.create(
            short_name=resource_info['name'],
            vendor=resource_info['vendor'],
            version=resource_info['version'],
            type=CatalogueResource.RESOURCE_TYPES.index(resource_info['type']),
            creator=user,
            template_uri=file_name,
            creation_date=now(),
            popularity='0.0',
            json_description=json.dumps(resource_info)
        )

        return resource
コード例 #30
0
ファイル: utils.py プロジェクト: fispace/wirecloud
def create_widget_from_template(template, user, request=None, base=None):

    """Creates a widget from a template"""

    if isinstance(template, TemplateParser):
        parser = template
    else:
        template_content = downloader.download_http_content(template, user=user)
        if base is None:
            base = template
        parser = TemplateParser(template_content, base=base)

    if parser.get_resource_type() != 'widget':
        raise Exception()

    widget_info = parser.get_resource_info()
    check_requirements(widget_info)

    widget = Widget()
    widget.resource = CatalogueResource.objects.get(vendor=parser.get_resource_vendor(), short_name=parser.get_resource_name(), version=parser.get_resource_version())
    widget_code = parser.get_absolute_url(widget_info['code_url'], base)
    widget.xhtml = XHTML.objects.create(
        uri=widget.uri + "/xhtml",
        url=widget_code,
        content_type=widget_info['code_content_type'],
        use_platform_style=widget_info['code_uses_platform_style'],
        cacheable=widget_info['code_cacheable']
    )

    widget.width = widget_info['widget_width']
    widget.height = widget_info['widget_height']

    widget.save()

    variable_definitions = {}
    user_options = {}

    order = 0
    for preference in widget_info['preferences']:
        vDef = VariableDef.objects.create(
            name=preference['name'],
            order=order,
            description=preference['description'],
            type=parser.typeText2typeCode(preference['type']),
            aspect='PREF',
            friend_code=None,
            readonly=preference['readonly'],
            label=preference['label'],
            default_value=preference['default_value'],
            value=preference['value'],
            widget=widget,
            secure=preference['secure']
        )
        variable_definitions[vDef.name] = vDef
        user_options[vDef.name] = {}
        for option in preference.get('options', ()):
            upo = UserPrefOption.objects.create(
                value=option['value'],
                name=option['label'],
                variableDef=vDef
            )
            user_options[vDef.name][upo.name] = upo

        order += 1

    order = 0
    for prop in widget_info['properties']:
        vDef = VariableDef.objects.create(
            name=prop['name'],
            order=order,
            description=prop['description'],
            type=parser.typeText2typeCode(prop['type']),
            aspect='PROP',
            friend_code=None,
            label=prop['label'],
            default_value=prop['default_value'],
            widget=widget,
            secure=prop['secure'],
        )
        variable_definitions[vDef.name] = vDef
        order += 1

    order = 0
    for input_endpoint in widget_info['wiring']['inputs']:
        vDef = VariableDef.objects.create(
            name=input_endpoint['name'],
            order=order,
            description=input_endpoint['description'],
            type=parser.typeText2typeCode(input_endpoint['type']),
            aspect='SLOT',
            friend_code=input_endpoint['friendcode'],
            label=input_endpoint['label'],
            action_label=input_endpoint['actionlabel'],
            widget=widget,
        )
        variable_definitions[vDef.name] = vDef
        order += 1

    order = 0
    for output_endpoint in widget_info['wiring']['outputs']:
        vDef = VariableDef.objects.create(
            name=output_endpoint['name'],
            order=order,
            description=output_endpoint['description'],
            type=parser.typeText2typeCode(output_endpoint['type']),
            aspect='EVEN',
            friend_code=output_endpoint['friendcode'],
            label=output_endpoint['label'],
            widget=widget,
        )
        variable_definitions[vDef.name] = vDef
        order += 1

    for context in widget_info['context']:
        vDef = VariableDef.objects.create(
            name=context['name'],
            type=parser.typeText2typeCode(context['type']),
            aspect=context['aspect'],
            widget=widget,
        )
        ContextOption.objects.create(concept=context['concept'], varDef=vDef)

    for lang in widget_info['translations']:
        translation = widget_info['translations'][lang]
        for index in translation:
            value = translation[index]
            usages = widget_info['translation_index_usage'][index]
            for use in usages:
                if use['type'] == 'vdef':
                    vDef = variable_definitions[use['variable']]
                    table = vDef._get_table_id()
                    element_id = vDef.id
                elif use['type'] == 'upo':
                    upo = user_options[use['variable']][use['option']]
                    table = upo._get_table_id()
                    element_id = upo.id
                else:
                    continue

                Translation.objects.create(
                    text_id=index,
                    element_id=element_id,
                    table=table,
                    language=lang,
                    value=value,
                    default=widget_info['default_lang'] == lang
                )

    return widget
コード例 #31
0
def fillWorkspaceUsingTemplate(workspace, template):

    if not isinstance(template, TemplateParser):
        template = TemplateParser(template)

    if template.get_resource_type() != "mashup":
        raise TypeError("Unsupported resource type: %s" % template.get_resource_type())

    user = workspace.creator

    context_values = get_context_values(workspace, workspace.creator)
    processor = TemplateValueProcessor({"user": user, "context": context_values})

    mashup_description = template.get_resource_info()

    new_values = {}
    id_mapping = {"operator": {}, "widget": {}}
    for preference_name in mashup_description["preferences"]:
        new_values[preference_name] = {"inherit": False, "value": mashup_description["preferences"][preference_name]}

    if len(new_values) > 0:
        update_workspace_preferences(workspace, new_values)

    new_forced_values = {"extra_prefs": [], "iwidget": {}, "ioperator": {}}
    for param in mashup_description["params"]:
        new_forced_values["extra_prefs"].append(
            {"name": param["name"], "inheritable": False, "label": param.get("label"), "type": param.get("type")}
        )

    for tab_entry in mashup_description["tabs"]:
        tab = createTab(tab_entry.get("name"), workspace, allow_renaming=True)

        new_values = {}
        for preference_name in tab_entry["preferences"]:
            new_values[preference_name] = {"inherit": False, "value": tab_entry["preferences"][preference_name]}

        if len(new_values) > 0:
            update_tab_preferences(tab, new_values)

        for resource in tab_entry["resources"]:

            position = resource["position"]
            rendering = resource["rendering"]

            widget = get_or_add_widget_from_catalogue(
                resource.get("vendor"), resource.get("name"), resource.get("version"), user
            )

            iwidget_data = {
                "left": int(position.get("x")),
                "top": int(position.get("y")),
                "icon_left": 0,
                "icon_top": 0,
                "zIndex": int(position.get("z")),
                "width": int(rendering.get("width")),
                "height": int(rendering.get("height")),
                "title": resource.get("title"),
                "layout": int(rendering.get("layout")),
                "widget": widget.uri,
            }

            iwidget = SaveIWidget(iwidget_data, user, tab, commit=False)
            if resource.get("readonly"):
                iwidget.readOnly = True

            initial_variable_values = {}
            iwidget_forced_values = {}
            iwidget_info = widget.resource.get_processed_info(process_variables=True)
            for prop_name in resource["properties"]:
                prop = resource["properties"][prop_name]
                read_only = prop.get("readonly")
                if prop.get("value", None) is not None:
                    value = prop["value"]
                else:
                    value = iwidget_info["variables"]["properties"][prop_name]["default"]
                if read_only:
                    iwidget_forced_values[prop_name] = {"value": value}
                else:
                    initial_variable_values[prop_name] = processor.process(value)

            for pref_name in resource["preferences"]:
                pref = resource["preferences"][pref_name]
                read_only = pref.get("readonly")
                if pref.get("value", None) is not None:
                    value = pref["value"]
                else:
                    value = iwidget_info["variables"]["preferences"][pref_name]["default"]
                if read_only:
                    iwidget_forced_values[pref_name] = {"value": value, "hidden": pref.get("hidden", False)}
                else:
                    initial_variable_values[pref_name] = processor.process(value)

            set_initial_values(iwidget, initial_variable_values, iwidget_info)
            iwidget.save()

            if len(iwidget_forced_values) > 0:
                new_forced_values["iwidget"][six.text_type(iwidget.id)] = iwidget_forced_values

            id_mapping["widget"][resource.get("id")] = {
                "id": iwidget.id,
                "name": resource.get("vendor") + "/" + resource.get("name") + "/" + resource.get("version"),
            }

    # wiring
    if len(workspace.wiringStatus) == 0:
        workspace.wiringStatus = get_wiring_skeleton()

    max_id = 0

    for id_ in workspace.wiringStatus["operators"].keys():
        if int(id_) > max_id:
            max_id = int(id_)

    # Process operators info
    for operator_id, operator in six.iteritems(mashup_description["wiring"]["operators"]):
        max_id += 1
        new_id = "%s" % max_id
        id_mapping["operator"][operator_id] = {"id": new_id}
        workspace.wiringStatus["operators"][new_id] = {
            "id": new_id,
            "name": operator["name"],
            "preferences": operator["preferences"],
        }

        ioperator_forced_values = {}
        for pref_id, pref in six.iteritems(operator["preferences"]):
            if pref.get("readonly", False):
                ioperator_forced_values[pref_id] = {"value": pref.get("value"), "hidden": pref.get("hidden", False)}

        if len(ioperator_forced_values) > 0:
            new_forced_values["ioperator"][new_id] = ioperator_forced_values

    # Remap connection ids
    source_mapping = {}
    target_mapping = {}

    for connection in mashup_description["wiring"]["connections"]:
        old_source_name = get_endpoint_name(connection["source"])
        old_target_name = get_endpoint_name(connection["target"])

        connection["source"]["id"] = map_id(connection["source"], id_mapping)
        connection["target"]["id"] = map_id(connection["target"], id_mapping)

        source_mapping[old_source_name] = get_endpoint_name(connection["source"])
        target_mapping[old_target_name] = get_endpoint_name(connection["target"])

    # Add new connections
    workspace.wiringStatus["connections"] += mashup_description["wiring"]["connections"]

    # Merging visual description...
    _remap_component_ids(id_mapping, mashup_description["wiring"]["visualdescription"]["components"], isGlobal=True)
    _remap_connection_endpoints(source_mapping, target_mapping, mashup_description["wiring"]["visualdescription"])

    # Remap mashup description behaviours' ids
    if len(mashup_description["wiring"]["visualdescription"]["behaviours"]) != 0:
        for behaviour in mashup_description["wiring"]["visualdescription"]["behaviours"]:
            _remap_component_ids(id_mapping, behaviour["components"])
            _remap_connection_endpoints(source_mapping, target_mapping, behaviour)

    if (
        len(workspace.wiringStatus["visualdescription"]["behaviours"]) != 0
        or len(mashup_description["wiring"]["visualdescription"]["behaviours"]) != 0
    ):
        if len(workspace.wiringStatus["visualdescription"]["behaviours"]) == 0 and not is_empty_wiring(
            workspace.wiringStatus["visualdescription"]
        ):
            # *TODO* flag to check if the user really want to merge both workspaces.
            _create_new_behaviour(
                workspace.wiringStatus["visualdescription"],
                _("Original wiring"),
                _("This is the wiring description of the original workspace"),
            )

        if len(mashup_description["wiring"]["visualdescription"]["behaviours"]) == 0:
            _create_new_behaviour(
                mashup_description["wiring"]["visualdescription"],
                _("Merged wiring"),
                _("This is the wiring description of the merged mashup."),
            )

        workspace.wiringStatus["visualdescription"]["behaviours"] += mashup_description["wiring"]["visualdescription"][
            "behaviours"
        ]

    # Merge global behaviour components and connections
    workspace.wiringStatus["visualdescription"]["components"]["operator"].update(
        mashup_description["wiring"]["visualdescription"]["components"]["operator"]
    )
    workspace.wiringStatus["visualdescription"]["components"]["widget"].update(
        mashup_description["wiring"]["visualdescription"]["components"]["widget"]
    )
    workspace.wiringStatus["visualdescription"]["connections"] += mashup_description["wiring"]["visualdescription"][
        "connections"
    ]

    # Forced values
    normalize_forced_values(workspace)

    workspace.forcedValues["extra_prefs"] += new_forced_values["extra_prefs"]
    workspace.forcedValues["iwidget"].update(new_forced_values["iwidget"])
    workspace.forcedValues["ioperator"].update(new_forced_values["ioperator"])

    workspace.save()
コード例 #32
0
def fillWorkspaceUsingTemplate(workspace, template):

    if not isinstance(template, TemplateParser):
        template = TemplateParser(template)

    if template.get_resource_type() != 'mashup':
        raise TypeError('Unsupported resource type: %s' % template.get_resource_type())

    user = workspace.creator

    context_values = get_context_values(workspace, workspace.creator)
    processor = TemplateValueProcessor({'user': user, 'context': context_values})

    mashup_description = template.get_resource_info()

    new_values = {}
    id_mapping = {
        'operator': {},
        'widget': {},
    }
    for preference_name in mashup_description['preferences']:
        new_values[preference_name] = {
            'inherit': False,
            'value': mashup_description['preferences'][preference_name],
        }

    if len(new_values) > 0:
        update_workspace_preferences(workspace, new_values)

    new_forced_values = {
        'extra_prefs': [],
        'iwidget': {},
        'ioperator': {},
    }
    for param in mashup_description['params']:
        new_forced_values['extra_prefs'].append({
            'name': param['name'],
            'inheritable': False,
            'label': param.get('label'),
            'type': param.get('type'),
        })

    for tab_entry in mashup_description['tabs']:
        tab = createTab(tab_entry.get('name'), workspace, allow_renaming=True)

        new_values = {}
        for preference_name in tab_entry['preferences']:
            new_values[preference_name] = {
                'inherit': False,
                'value': tab_entry['preferences'][preference_name],
            }

        if len(new_values) > 0:
            update_tab_preferences(tab, new_values)

        for resource in tab_entry['resources']:

            position = resource['position']
            rendering = resource['rendering']

            widget = get_or_add_widget_from_catalogue(resource.get('vendor'), resource.get('name'), resource.get('version'), user)

            iwidget_data = {
                "widget": widget.uri,
                "title": resource.get('title'),
                "left": int(position.get('x')),
                "top": int(position.get('y')),
                "icon_left": 0,
                "icon_top": 0,
                "zIndex": int(position.get('z')),
                "width": int(rendering.get('width')),
                "height": int(rendering.get('height')),
                "layout": int(rendering.get('layout')),
                "minimized": rendering['minimized'],
                "fulldragboard": rendering['fulldragboard'],
            }

            iwidget = SaveIWidget(iwidget_data, user, tab, commit=False)
            if resource.get('readonly'):
                iwidget.readOnly = True

            initial_variable_values = {}
            iwidget_forced_values = {}
            iwidget_info = widget.resource.get_processed_info(process_variables=True)
            for prop_name in resource['properties']:
                prop = resource['properties'][prop_name]
                read_only = prop.get('readonly')
                if prop.get('value', None) is not None:
                    value = prop['value']
                else:
                    value = iwidget_info['variables']['properties'][prop_name]['default']
                if read_only:
                    iwidget_forced_values[prop_name] = {'value': value}
                else:
                    initial_variable_values[prop_name] = processor.process(value)

            for pref_name in resource['preferences']:
                pref = resource['preferences'][pref_name]
                read_only = pref.get('readonly')
                if pref.get('value', None) is not None:
                    value = pref['value']
                else:
                    value = iwidget_info['variables']['preferences'][pref_name]['default']
                if read_only:
                    iwidget_forced_values[pref_name] = {'value': value, 'hidden': pref.get('hidden', False)}
                else:
                    initial_variable_values[pref_name] = processor.process(value)

            set_initial_values(iwidget, initial_variable_values, iwidget_info)
            iwidget.save()

            if len(iwidget_forced_values) > 0:
                new_forced_values['iwidget'][six.text_type(iwidget.id)] = iwidget_forced_values

            id_mapping['widget'][resource.get('id')] = {
                'id': iwidget.id,
                'name': resource.get('vendor') + "/" + resource.get('name') + "/" + resource.get('version')
            }

    # wiring
    if len(workspace.wiringStatus) == 0:
        workspace.wiringStatus = get_wiring_skeleton()

    max_id = 0

    for id_ in workspace.wiringStatus['operators'].keys():
        if int(id_) > max_id:
            max_id = int(id_)

    # Process operators info
    for operator_id, operator in six.iteritems(mashup_description['wiring']['operators']):
        max_id += 1
        new_id = "%s" % max_id
        id_mapping['operator'][operator_id] = {
            'id': new_id
        }
        workspace.wiringStatus['operators'][new_id] = {
            'id': new_id,
            'name': operator['name'],
            'preferences': operator['preferences'],
        }

        ioperator_forced_values = {}
        for pref_id, pref in six.iteritems(operator['preferences']):
            if pref.get('readonly', False):
                ioperator_forced_values[pref_id] = {'value': pref.get('value'), 'hidden': pref.get('hidden', False)}

        if len(ioperator_forced_values) > 0:
            new_forced_values['ioperator'][new_id] = ioperator_forced_values

    # Remap connection ids
    source_mapping = {}
    target_mapping = {}

    for connection in mashup_description['wiring']['connections']:
        old_source_name = get_endpoint_name(connection['source'])
        old_target_name = get_endpoint_name(connection['target'])

        connection['source']['id'] = map_id(connection['source'], id_mapping)
        connection['target']['id'] = map_id(connection['target'], id_mapping)

        source_mapping[old_source_name] = get_endpoint_name(connection['source'])
        target_mapping[old_target_name] = get_endpoint_name(connection['target'])

    # Add new connections
    workspace.wiringStatus['connections'] += mashup_description['wiring']['connections']

    # Merging visual description...
    _remap_component_ids(id_mapping, mashup_description['wiring']['visualdescription']['components'], isGlobal=True)
    _remap_connection_endpoints(source_mapping, target_mapping, mashup_description['wiring']['visualdescription'])

    # Remap mashup description behaviours' ids
    if len(mashup_description['wiring']['visualdescription']['behaviours']) != 0:
        for behaviour in mashup_description['wiring']['visualdescription']['behaviours']:
            _remap_component_ids(id_mapping, behaviour['components'])
            _remap_connection_endpoints(source_mapping, target_mapping, behaviour)

    if len(workspace.wiringStatus['visualdescription']['behaviours']) != 0 or len(mashup_description['wiring']['visualdescription']['behaviours']) != 0:
        if len(workspace.wiringStatus['visualdescription']['behaviours']) == 0 and not is_empty_wiring(workspace.wiringStatus['visualdescription']):
            # *TODO* flag to check if the user really want to merge both workspaces.
            _create_new_behaviour(workspace.wiringStatus['visualdescription'], _("Original wiring"), _("This is the wiring description of the original workspace"))

        if len(mashup_description['wiring']['visualdescription']['behaviours']) == 0:
            _create_new_behaviour(mashup_description['wiring']['visualdescription'], _("Merged wiring"), _("This is the wiring description of the merged mashup."))

        workspace.wiringStatus['visualdescription']['behaviours'] += mashup_description['wiring']['visualdescription']['behaviours']

    # Merge global behaviour components and connections
    workspace.wiringStatus['visualdescription']['components']['operator'].update(mashup_description['wiring']['visualdescription']['components']['operator'])
    workspace.wiringStatus['visualdescription']['components']['widget'].update(mashup_description['wiring']['visualdescription']['components']['widget'])
    workspace.wiringStatus['visualdescription']['connections'] += mashup_description['wiring']['visualdescription']['connections']

    # Forced values
    normalize_forced_values(workspace)

    workspace.forcedValues['extra_prefs'] += new_forced_values['extra_prefs']
    workspace.forcedValues['iwidget'].update(new_forced_values['iwidget'])
    workspace.forcedValues['ioperator'].update(new_forced_values['ioperator'])

    workspace.save()