示例#1
0
def _populate_variables_values_cache(workspace, user, key, forced_values=None):
    """ populates VariableValue cached values for that user """
    values_by_varid = {}
    values_by_varname = {}

    if forced_values is None:
        context_values = get_context_values(workspace, user)
        preferences = get_workspace_preference_values(workspace)
        forced_values = process_forced_values(workspace, user, context_values,
                                              preferences)

    for iwidget in IWidget.objects.filter(tab__workspace=workspace):
        # forced_values uses string keys
        svariwidget = str(iwidget.id)
        values_by_varname[iwidget.id] = {}
        iwidget_info = iwidget.widget.resource.get_processed_info()

        for vardef in iwidget_info['preferences']:
            _process_variable(iwidget, svariwidget, vardef, forced_values,
                              values_by_varname, values_by_varid)

        for vardef in iwidget_info['properties']:
            _process_variable(iwidget, svariwidget, vardef, forced_values,
                              values_by_varname, values_by_varid)

    values = {
        'by_varid': values_by_varid,
        'by_varname': values_by_varname,
    }
    cache.set(key, values)

    return values
示例#2
0
def _populate_variables_values_cache(workspace, user, key, forced_values=None):
    """ populates VariableValue cached values for that user """
    values_by_varid = {}
    values_by_varname = {}

    if forced_values is None:
        context_values = get_context_values(workspace, user)
        preferences = get_workspace_preference_values(workspace)
        forced_values = process_forced_values(workspace, user, context_values, preferences)

    for iwidget in IWidget.objects.filter(tab__workspace=workspace):
        # forced_values uses string keys
        svariwidget = str(iwidget.id)
        values_by_varname[iwidget.id] = {}

        if iwidget.widget is None:
            continue

        iwidget_info = iwidget.widget.resource.get_processed_info()

        for vardef in iwidget_info['preferences']:
            _process_variable(iwidget, svariwidget, vardef, forced_values, values_by_varname, values_by_varid)

        for vardef in iwidget_info['properties']:
            _process_variable(iwidget, svariwidget, vardef, forced_values, values_by_varname, values_by_varid)

    values = {
        'by_varid': values_by_varid,
        'by_varname': values_by_varname,
    }
    cache.set(key, values)

    return values
示例#3
0
def _get_global_workspace_data(workspaceDAO, user):
    user_workspace = UserWorkspace.objects.get(user=user, workspace=workspaceDAO)
    data_ret = get_workspace_data(workspaceDAO, user)

    # Context information
    data_ret['context'] = get_workspace_context(user_workspace)

    # Workspace preferences
    preferences = get_workspace_preference_values(workspaceDAO.pk)
    data_ret['preferences'] = preferences

    # Process forced variable values
    concept_values = get_context_values(user_workspace)
    forced_values = process_forced_values(workspaceDAO, user, concept_values, preferences)
    data_ret['empty_params'] = forced_values['empty_params']
    data_ret['extra_prefs'] = forced_values['extra_prefs']
    if len(forced_values['empty_params']) > 0:
        return json.dumps(data_ret, cls=LazyEncoder)

    cache_manager = VariableValueCacheManager(workspaceDAO, user, forced_values)

    # Tabs processing
    # Check if the workspace's tabs have order
    tabs = Tab.objects.filter(workspace=workspaceDAO).order_by('position')
    if tabs.count() > 0:
        if tabs[0].position != None:
            tabs = tabs.order_by('position')
        else:
            #set default order
            for i in range(len(tabs)):
                tabs[i].position = i
                tabs[i].save()
    else:
        tabs = [createTab(_('Tab'), workspaceDAO)]

    tabs_data = [get_tab_data(tab) for tab in tabs]

    data_ret['tabs'] = tabs_data

    for tab in tabs_data:
        tab_pk = tab['id']
        iwidgets = IWidget.objects.filter(tab__id=tab_pk).order_by('id')

        iwidget_data = []
        for iwidget in iwidgets:
            iwidget_data.append(get_iwidget_data(iwidget, user, workspaceDAO, cache_manager))

        tab['iwidgets'] = iwidget_data

    data_ret['wiring'] = json.loads(workspaceDAO.wiringStatus)
    for forced_operator_id, forced_preferences in forced_values['ioperator'].iteritems():
        for forced_pref_name, forced_preference in forced_preferences.iteritems():
            data_ret['wiring']['operators'][forced_operator_id]['preferences'][forced_pref_name]['value'] = forced_preference['value']

    # Params
    last_published_workspace = PublishedWorkspace.objects.filter(workspace=workspaceDAO).order_by('-pk')
    if len(last_published_workspace) > 0:
        data_ret["params"] = json.loads(last_published_workspace[0].params)

    return json.dumps(data_ret, cls=LazyEncoder)
示例#4
0
def _populate_variables_values_cache(workspace, user, key, forced_values=None):
    """ populates VariableValue cached values for that user """
    values_by_varid = {}
    values_by_varname = {}

    if forced_values == None:
        user_workspace = UserWorkspace.objects.get(user=user, workspace=workspace)
        context_values = get_context_values(user_workspace)
        preferences = get_workspace_preference_values(workspace)
        forced_values = process_forced_values(workspace, user, context_values, preferences)

    var_values = VariableValue.objects.filter(user__id=user.id, variable__iwidget__tab__workspace=workspace)
    for var_value in var_values.select_related('variable__vardef'):
        variwidget = var_value.variable.iwidget.id
        varname = var_value.variable.vardef.name
        # forced_values uses string keys
        svariwidget = str(variwidget)

        if not variwidget in values_by_varname:
            values_by_varname[variwidget] = {}

        entry = {}
        if svariwidget in forced_values['iwidget'] and varname in forced_values['iwidget'][svariwidget]:
            fv_entry = forced_values['iwidget'][svariwidget][varname]

            entry['value'] = fv_entry['value']
            if var_value.variable.vardef.secure:
                entry['value'] = encrypt_value(entry['value'])

            entry['readonly'] = True
            entry['hidden'] = fv_entry.get('hidden', False)

        else:
            if not var_value.variable.vardef.secure:
                entry['value'] = var_value.get_variable_value()
            else:
                entry['value'] = var_value.value

            entry['readonly'] = False
            entry['hidden'] = False

        entry['secure'] = var_value.variable.vardef.secure

        values_by_varname[variwidget][varname] = entry
        values_by_varid[var_value.variable.id] = entry

    values = {
        'by_varid': values_by_varid,
        'by_varname': values_by_varname,
    }
    cache.set(key, values)

    return values
示例#5
0
def _populate_variables_values_cache(workspace, user, key, forced_values=None):
    """ populates VariableValue cached values for that user """
    values_by_varname = {
        "ioperator": {},
        "iwidget": {},
    }
    if forced_values is None:
        context_values = get_context_values(workspace, user)
        preferences = get_workspace_preference_values(workspace)
        forced_values = process_forced_values(workspace, user, context_values, preferences)

    for iwidget in IWidget.objects.filter(tab__workspace=workspace):
        # forced_values uses string keys
        svariwidget = "%s" % iwidget.id
        values_by_varname["iwidget"][svariwidget] = {}

        if iwidget.widget is None:
            continue

        iwidget_info = iwidget.widget.resource.get_processed_info()

        for vardef in iwidget_info.get('preferences', {}):
            value = iwidget.variables.get(vardef['name'], None)
            _process_variable("iwidget", svariwidget, vardef, value, forced_values, values_by_varname, user, workspace.creator)

        for vardef in iwidget_info.get('properties', {}):
            value = iwidget.variables.get(vardef['name'], None)
            _process_variable("iwidget", svariwidget, vardef, value, forced_values, values_by_varname, user, workspace.creator)

    for operator_id, operator in six.iteritems(workspace.wiringStatus.get('operators', {})):

        values_by_varname["ioperator"][operator_id] = {}
        vendor, name, version = operator['name'].split('/')
        try:
            operator_info = CatalogueResource.objects.get(vendor=vendor, short_name=name, version=version).get_processed_info()
        except CatalogueResource.DoesNotExist:
            continue
        for vardef in operator_info.get('preferences', {}):
            value = operator.get("preferences", {}).get(vardef['name'], {}).get("value")
            _process_variable("ioperator", operator_id, vardef, value, forced_values, values_by_varname, user, workspace.creator)

        for vardef in operator_info.get('properties', {}):
            value = operator.get("properties", {}).get(vardef['name'], {}).get("value")
            _process_variable("ioperator", operator_id, vardef, value, forced_values, values_by_varname, user, workspace.creator)

    cache.set(key, values_by_varname)

    return values_by_varname
示例#6
0
def _get_global_workspace_data(workspaceDAO, user):
    data_ret = get_workspace_data(workspaceDAO, user)

    # Workspace preferences
    preferences = get_workspace_preference_values(workspaceDAO)
    data_ret['preferences'] = preferences

    data_ret['users'] = []

    for u in workspaceDAO.users.all():
        try:
            is_organization = u.organization is not None
        except:
            is_organization = False

        data_ret['users'].append({
            "fullname":
            u.get_full_name(),
            "username":
            u.username,
            "organization":
            is_organization,
            "accesslevel":
            "owner" if workspaceDAO.creator == u else "read",
        })

    # Process forced variable values
    concept_values = get_context_values(workspaceDAO, user)
    forced_values = process_forced_values(workspaceDAO, user, concept_values,
                                          preferences)
    data_ret['empty_params'] = forced_values['empty_params']
    data_ret['extra_prefs'] = forced_values['extra_prefs']
    if len(forced_values['empty_params']) > 0:
        return json.dumps(data_ret, cls=LazyEncoder)

    cache_manager = VariableValueCacheManager(workspaceDAO, user,
                                              forced_values)

    # Tabs processing
    # Check if the workspace's tabs have order
    tabs = Tab.objects.filter(workspace=workspaceDAO).order_by('position')
    if tabs.count() > 0:
        if tabs[0].position is not None:
            tabs = tabs.order_by('position')
        else:
            # set default order
            for i in range(len(tabs)):
                tabs[i].position = i
                tabs[i].save()
    else:
        tabs = [createTab(_('Tab'), workspaceDAO)]

    data_ret['tabs'] = [
        get_tab_data(tab,
                     workspace=workspaceDAO,
                     cache_manager=cache_manager,
                     user=user) for tab in tabs
    ]
    data_ret['wiring'] = deepcopy(workspaceDAO.wiringStatus)
    for operator_id, operator in six.iteritems(data_ret['wiring'].get(
            'operators', {})):
        try:
            (vendor, name, version) = operator['name'].split('/')
        except:
            continue

        try:
            resource = CatalogueResource.objects.get(vendor=vendor,
                                                     short_name=name,
                                                     version=version)
            operator_info = resource.get_processed_info(process_variables=True)
            # Check if the resource is available, if not, variables should not be retrieved
            if not resource.is_available_for(workspaceDAO.creator):
                raise CatalogueResource.DoesNotExist
        except CatalogueResource.DoesNotExist:
            operator["preferences"] = {}
            operator["properties"] = {}
            continue

        operator_forced_values = forced_values['ioperator'].get(
            operator_id, {})
        # Build operator preference data
        for preference_name, preference in six.iteritems(
                operator.get('preferences', {})):
            vardef = operator_info['variables']['preferences'].get(
                preference_name)
            value = preference.get('value', None)

            # Handle multiuser
            variable_user = user if vardef is not None and vardef[
                "multiuser"] else workspaceDAO.creator

            if preference_name in operator_forced_values:
                preference['value'] = operator_forced_values[preference_name][
                    'value']
            elif value is None or value["users"].get("%s" % variable_user.id,
                                                     None) is None:
                # If not defined / not defined for the current user, take the default value
                preference['value'] = parse_value_from_text(
                    vardef, vardef['default'])
            else:
                preference['value'] = value["users"].get("%s" %
                                                         variable_user.id)

            # Secure censor
            if vardef is not None and vardef["secure"]:
                preference['value'] = "" if preference.get(
                    'value') is None or decrypt_value(
                        preference.get('value')) == "" else "********"

        # Build operator property data
        for property_name, property in six.iteritems(
                operator.get('properties', {})):
            vardef = operator_info['variables']['properties'].get(
                property_name)
            value = property.get('value', None)

            # Handle multiuser
            variable_user = user if vardef is not None and vardef[
                "multiuser"] else workspaceDAO.creator

            if property_name in operator_forced_values:
                property['value'] = operator_forced_values[property_name][
                    'value']
            elif value is None or value["users"].get("%s" % variable_user.id,
                                                     None) is None:
                # If not defined / not defined for the current user, take the default value
                property['value'] = parse_value_from_text(
                    vardef, vardef['default'])
            else:
                property['value'] = value["users"].get("%s" % variable_user.id)

            # Secure censor
            if vardef is not None and vardef["secure"]:
                property['value'] = "" if property.get(
                    'value') is None or decrypt_value(
                        property.get('value')) == "" else "********"

    return json.dumps(data_ret, cls=LazyEncoder)
示例#7
0
def _populate_variables_values_cache(workspace, user, key, forced_values=None):
    """ populates VariableValue cached values for that user """
    values_by_varname = {
        "ioperator": {},
        "iwidget": {},
    }
    if forced_values is None:
        context_values = get_context_values(workspace, user)
        preferences = get_workspace_preference_values(workspace)
        forced_values = process_forced_values(workspace, user, context_values,
                                              preferences)

    for iwidget in IWidget.objects.filter(tab__workspace=workspace):
        # forced_values uses string keys
        svariwidget = "%s" % iwidget.id
        values_by_varname["iwidget"][svariwidget] = {}

        if iwidget.widget is None:
            continue

        iwidget_info = iwidget.widget.resource.get_processed_info()

        for vardef in iwidget_info.get('preferences', {}):
            value = iwidget.variables.get(vardef['name'], None)
            _process_variable("iwidget", svariwidget, vardef, value,
                              forced_values, values_by_varname, user,
                              workspace.creator)

        for vardef in iwidget_info.get('properties', {}):
            value = iwidget.variables.get(vardef['name'], None)
            _process_variable("iwidget", svariwidget, vardef, value,
                              forced_values, values_by_varname, user,
                              workspace.creator)

    for operator_id, operator in six.iteritems(
            workspace.wiringStatus.get('operators', {})):

        values_by_varname["ioperator"][operator_id] = {}
        vendor, name, version = operator['name'].split('/')
        try:
            operator_info = CatalogueResource.objects.get(
                vendor=vendor, short_name=name,
                version=version).get_processed_info()
        except CatalogueResource.DoesNotExist:
            continue
        for vardef in operator_info.get('preferences', {}):
            value = operator.get("preferences", {}).get(vardef['name'],
                                                        {}).get("value")
            _process_variable("ioperator", operator_id, vardef, value,
                              forced_values, values_by_varname, user,
                              workspace.creator)

        for vardef in operator_info.get('properties', {}):
            value = operator.get("properties", {}).get(vardef['name'],
                                                       {}).get("value")
            _process_variable("ioperator", operator_id, vardef, value,
                              forced_values, values_by_varname, user,
                              workspace.creator)

    cache.set(key, values_by_varname)

    return values_by_varname
示例#8
0
def _get_global_workspace_data(workspaceDAO, user):
    data_ret = get_workspace_data(workspaceDAO, user)

    # Workspace preferences
    preferences = get_workspace_preference_values(workspaceDAO)
    data_ret['preferences'] = preferences

    data_ret['users'] = []

    for user in workspaceDAO.users.all():
        try:
            is_organization = user.organization is not None
        except:
            is_organization = False

        data_ret['users'].append({
            "fullname": user.get_full_name(),
            "username": user.username,
            "organization": is_organization,
            "accesslevel": "owner" if workspaceDAO.creator == user else "read",
        })

    # Process forced variable values
    concept_values = get_context_values(workspaceDAO, user)
    forced_values = process_forced_values(workspaceDAO, user, concept_values, preferences)
    data_ret['empty_params'] = forced_values['empty_params']
    data_ret['extra_prefs'] = forced_values['extra_prefs']
    if len(forced_values['empty_params']) > 0:
        return json.dumps(data_ret, cls=LazyEncoder)

    cache_manager = VariableValueCacheManager(workspaceDAO, user, forced_values)

    # Tabs processing
    # Check if the workspace's tabs have order
    tabs = Tab.objects.filter(workspace=workspaceDAO).order_by('position')
    if tabs.count() > 0:
        if tabs[0].position is not None:
            tabs = tabs.order_by('position')
        else:
            # set default order
            for i in range(len(tabs)):
                tabs[i].position = i
                tabs[i].save()
    else:
        tabs = [createTab(_('Tab'), workspaceDAO)]

    data_ret['tabs'] = [get_tab_data(tab, workspace=workspaceDAO, cache_manager=cache_manager) for tab in tabs]
    data_ret['wiring'] = workspaceDAO.wiringStatus
    for operator_id, operator in six.iteritems(data_ret['wiring'].get('operators', {})):
        try:
            (vendor, name, version) = operator['name'].split('/')
        except:
            continue

        try:
            resource = CatalogueResource.objects.get(vendor=vendor, short_name=name, version=version)
            if not resource.is_available_for(workspaceDAO.creator):
                # The operator used by this instance is missing
                continue
        except CatalogueResource.DoesNotExist:
            continue

        operator_info = resource.get_processed_info(process_variables=True)

        operator_forced_values = forced_values['ioperator'].get(operator_id, {})
        for preference_name, preference in six.iteritems(operator.get('preferences', {})):
            vardef = operator_info['variables']['preferences'].get(preference_name)
            if preference_name in operator_forced_values:
                preference['value'] = operator_forced_values[preference_name]['value']
            elif preference.get('value') is None and vardef is not None:
                preference['value'] = parse_value_from_text(vardef, vardef['default'])

    return json.dumps(data_ret, cls=LazyEncoder)
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()
def fillWorkspaceUsingTemplate(workspace, template):

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

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

    user = workspace.creator

    context_values = get_context_values(workspace, workspace.creator)
    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": {}, "ioperator": {}}
    for param in workspace_info["params"]:
        new_forced_values["extra_prefs"].append(
            {"name": 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()

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

            iwidget_id_mapping[resource.get("id")] = iwidget

    # wiring
    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
    ioperator_id_mapping = {}

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

    wiring_status = {
        "operators": workspace_wiring_status["operators"],
        "connections": workspace_wiring_status["connections"],
    }

    # Process operators info
    for operator_id, operator in six.iteritems(workspace_info["wiring"]["operators"]):
        max_id += 1
        new_id = unicode(max_id)
        ioperator_id_mapping[operator_id] = new_id
        wiring_status["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

    for connection in workspace_info["wiring"]["connections"]:
        source_id = str(connection["source"]["id"])
        target_id = str(connection["target"]["id"])

        if connection["source"]["type"] == "iwidget":
            source_id = iwidget_id_mapping[source_id].id
        elif connection["source"]["type"] == "ioperator":
            source_id = ioperator_id_mapping[source_id]

        if connection["target"]["type"] == "iwidget":
            target_id = iwidget_id_mapping[target_id].id
        elif connection["target"]["type"] == "ioperator":
            target_id = ioperator_id_mapping[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 six.iteritems(wiring_view["iwidgets"]):
                iwidgets_views[iwidget_id_mapping[key].id] = widget

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

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

    workspace.wiringStatus = json.dumps(wiring_status)

    # Forced values
    if workspace.forcedValues is not None and workspace.forcedValues != "":
        forced_values = json.loads(workspace.forcedValues)
    else:
        forced_values = {"extra_prefs": [], "iwidget": {}, "ioperator": {}}

    if "ioperator" not in forced_values:
        forced_values["ioperator"] = {}

    if "iwidget" not in forced_values:
        forced_values["iwidget"] = {}

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

    workspace.save()
示例#11
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()
示例#12
0
def _get_global_workspace_data(workspaceDAO, user):
    data_ret = get_workspace_data(workspaceDAO, user)

    # Workspace preferences
    preferences = get_workspace_preference_values(workspaceDAO)
    data_ret['preferences'] = preferences

    data_ret['users'] = []

    for user in workspaceDAO.users.all():
        try:
            is_organization = user.organization is not None
        except:
            is_organization = False

        data_ret['users'].append({
            "fullname":
            user.get_full_name(),
            "username":
            user.username,
            "organization":
            is_organization,
            "accesslevel":
            "owner" if workspaceDAO.creator == user else "read",
        })

    # Process forced variable values
    concept_values = get_context_values(workspaceDAO, user)
    forced_values = process_forced_values(workspaceDAO, user, concept_values,
                                          preferences)
    data_ret['empty_params'] = forced_values['empty_params']
    data_ret['extra_prefs'] = forced_values['extra_prefs']
    if len(forced_values['empty_params']) > 0:
        return json.dumps(data_ret, cls=LazyEncoder)

    cache_manager = VariableValueCacheManager(workspaceDAO, user,
                                              forced_values)

    # Tabs processing
    # Check if the workspace's tabs have order
    tabs = Tab.objects.filter(workspace=workspaceDAO).order_by('position')
    if tabs.count() > 0:
        if tabs[0].position is not None:
            tabs = tabs.order_by('position')
        else:
            # set default order
            for i in range(len(tabs)):
                tabs[i].position = i
                tabs[i].save()
    else:
        tabs = [createTab(_('Tab'), workspaceDAO)]

    data_ret['tabs'] = [
        get_tab_data(tab, workspace=workspaceDAO, cache_manager=cache_manager)
        for tab in tabs
    ]
    data_ret['wiring'] = workspaceDAO.wiringStatus
    for operator_id, operator in six.iteritems(data_ret['wiring'].get(
            'operators', {})):
        try:
            (vendor, name, version) = operator['name'].split('/')
        except:
            continue

        try:
            resource = CatalogueResource.objects.get(vendor=vendor,
                                                     short_name=name,
                                                     version=version)
            if not resource.is_available_for(workspaceDAO.creator):
                # The operator used by this instance is missing
                continue
        except CatalogueResource.DoesNotExist:
            continue

        operator_info = resource.get_processed_info(process_variables=True)

        operator_forced_values = forced_values['ioperator'].get(
            operator_id, {})
        for preference_name, preference in six.iteritems(
                operator.get('preferences', {})):
            vardef = operator_info['variables']['preferences'].get(
                preference_name)
            if preference_name in operator_forced_values:
                preference['value'] = operator_forced_values[preference_name][
                    'value']
            elif preference.get('value') is None and vardef is not None:
                preference['value'] = parse_value_from_text(
                    vardef, vardef['default'])

    return json.dumps(data_ret, cls=LazyEncoder)
示例#13
0
def _get_global_workspace_data(workspaceDAO, user):
    data_ret = get_workspace_data(workspaceDAO, user)

    # Workspace preferences
    preferences = get_workspace_preference_values(workspaceDAO)
    data_ret['preferences'] = preferences

    data_ret['users'] = []

    for u in workspaceDAO.users.all():
        try:
            is_organization = u.organization is not None
        except:
            is_organization = False

        data_ret['users'].append({
            "fullname": u.get_full_name(),
            "username": u.username,
            "organization": is_organization,
            "accesslevel": "owner" if workspaceDAO.creator == u else "read",
        })

    # Process forced variable values
    concept_values = get_context_values(workspaceDAO, user)
    forced_values = process_forced_values(workspaceDAO, user, concept_values, preferences)
    data_ret['empty_params'] = forced_values['empty_params']
    data_ret['extra_prefs'] = forced_values['extra_prefs']
    if len(forced_values['empty_params']) > 0:
        return json.dumps(data_ret, cls=LazyEncoder)

    cache_manager = VariableValueCacheManager(workspaceDAO, user, forced_values)

    # Tabs processing
    # Check if the workspace's tabs have order
    tabs = Tab.objects.filter(workspace=workspaceDAO).order_by('position')
    if tabs.count() > 0:
        if tabs[0].position is not None:
            tabs = tabs.order_by('position')
        else:
            # set default order
            for i in range(len(tabs)):
                tabs[i].position = i
                tabs[i].save()
    else:
        tabs = [createTab(_('Tab'), workspaceDAO)]

    data_ret['tabs'] = [get_tab_data(tab, workspace=workspaceDAO, cache_manager=cache_manager, user=user) for tab in tabs]
    data_ret['wiring'] = deepcopy(workspaceDAO.wiringStatus)
    for operator_id, operator in six.iteritems(data_ret['wiring'].get('operators', {})):
        try:
            (vendor, name, version) = operator['name'].split('/')
        except:
            continue

        try:
            resource = CatalogueResource.objects.get(vendor=vendor, short_name=name, version=version)
            operator_info = resource.get_processed_info(process_variables=True)
            # Check if the resource is available, if not, variables should not be retrieved
            if not resource.is_available_for(workspaceDAO.creator):
                raise CatalogueResource.DoesNotExist
        except CatalogueResource.DoesNotExist:
            operator["preferences"] = {}
            operator["properties"] = {}
            continue

        operator_forced_values = forced_values['ioperator'].get(operator_id, {})
        # Build operator preference data
        for preference_name, preference in six.iteritems(operator.get('preferences', {})):
            vardef = operator_info['variables']['preferences'].get(preference_name)
            value = preference.get('value', None)

            # Handle multiuser
            variable_user = user if vardef is not None and vardef["multiuser"] else workspaceDAO.creator

            if preference_name in operator_forced_values:
                preference['value'] = operator_forced_values[preference_name]['value']
            elif value is None or value["users"].get("%s" % variable_user.id, None) is None:
                # If not defined / not defined for the current user, take the default value
                preference['value'] = parse_value_from_text(vardef, vardef['default'])
            else:
                preference['value'] = value["users"].get("%s" % variable_user.id)

            # Secure censor
            if vardef is not None and vardef["secure"]:
                preference['value'] = "" if preference.get('value') is None or decrypt_value(preference.get('value')) == "" else "********"

        # Build operator property data
        for property_name, property in six.iteritems(operator.get('properties', {})):
            vardef = operator_info['variables']['properties'].get(property_name)
            value = property.get('value', None)

            # Handle multiuser
            variable_user = user if vardef is not None and vardef["multiuser"] else workspaceDAO.creator

            if property_name in operator_forced_values:
                property['value'] = operator_forced_values[property_name]['value']
            elif value is None or value["users"].get("%s" % variable_user.id, None) is None:
                # If not defined / not defined for the current user, take the default value
                property['value'] = parse_value_from_text(vardef, vardef['default'])
            else:
                property['value'] = value["users"].get("%s" % variable_user.id)

            # Secure censor
            if vardef is not None and vardef["secure"]:
                property['value'] = "" if property.get('value') is None or decrypt_value(property.get('value')) == "" else "********"

    return json.dumps(data_ret, cls=LazyEncoder)
示例#14
0
def _get_global_workspace_data(workspaceDAO, user):
    data_ret = get_workspace_data(workspaceDAO, user)

    # Context information
    data_ret['context'] = get_workspace_context(workspaceDAO, user)

    # Workspace preferences
    preferences = get_workspace_preference_values(workspaceDAO)
    data_ret['preferences'] = preferences

    data_ret['users'] = []

    for user in workspaceDAO.users.all():
        try:
            is_organization = resource.organization is not None
        except:
            is_organization = False

        data_ret['users'].append({
            "fullname":
            user.get_full_name(),
            "username":
            user.username,
            "organization":
            is_organization,
            "accesslevel":
            "owner" if workspaceDAO.creator == user else "read",
        })

    # Process forced variable values
    concept_values = get_context_values(workspaceDAO, user)
    forced_values = process_forced_values(workspaceDAO, user, concept_values,
                                          preferences)
    data_ret['empty_params'] = forced_values['empty_params']
    data_ret['extra_prefs'] = forced_values['extra_prefs']
    if len(forced_values['empty_params']) > 0:
        return json.dumps(data_ret, cls=LazyEncoder)

    cache_manager = VariableValueCacheManager(workspaceDAO, user,
                                              forced_values)

    # Tabs processing
    # Check if the workspace's tabs have order
    tabs = Tab.objects.filter(workspace=workspaceDAO).order_by('position')
    if tabs.count() > 0:
        if tabs[0].position is not None:
            tabs = tabs.order_by('position')
        else:
            # set default order
            for i in range(len(tabs)):
                tabs[i].position = i
                tabs[i].save()
    else:
        tabs = [createTab(_('Tab'), workspaceDAO)]

    tabs_data = [get_tab_data(tab) for tab in tabs]

    data_ret['tabs'] = tabs_data

    for tab in tabs_data:
        tab_pk = tab['id']
        iwidgets = IWidget.objects.filter(tab__id=tab_pk).order_by('id')

        iwidget_data = []
        for iwidget in iwidgets:
            iwidget_data.append(
                get_iwidget_data(iwidget, workspaceDAO, cache_manager))

        tab['iwidgets'] = iwidget_data

    data_ret['wiring'] = workspaceDAO.wiringStatus
    for forced_operator_id, forced_preferences in six.iteritems(
            forced_values['ioperator']):
        for forced_pref_name, forced_preference in six.iteritems(
                forced_preferences):
            data_ret['wiring']['operators'][forced_operator_id]['preferences'][
                forced_pref_name]['value'] = forced_preference['value']

    return json.dumps(data_ret, cls=LazyEncoder)
示例#15
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()
示例#16
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()
示例#17
0
文件: utils.py 项目: Mognom/wirecloud
def _get_global_workspace_data(workspaceDAO, user):
    data_ret = get_workspace_data(workspaceDAO, user)

    # Context information
    data_ret['context'] = get_workspace_context(workspaceDAO, user)

    # Workspace preferences
    preferences = get_workspace_preference_values(workspaceDAO)
    data_ret['preferences'] = preferences

    data_ret['users'] = []

    for user in workspaceDAO.users.all():
        try:
            is_organization = resource.organization is not None
        except:
            is_organization = False

        data_ret['users'].append({
            "fullname": user.get_full_name(),
            "username": user.username,
            "organization": is_organization,
            "accesslevel": "owner" if workspaceDAO.creator == user else "read",
        })

    # Process forced variable values
    concept_values = get_context_values(workspaceDAO, user)
    forced_values = process_forced_values(workspaceDAO, user, concept_values, preferences)
    data_ret['empty_params'] = forced_values['empty_params']
    data_ret['extra_prefs'] = forced_values['extra_prefs']
    if len(forced_values['empty_params']) > 0:
        return json.dumps(data_ret, cls=LazyEncoder)

    cache_manager = VariableValueCacheManager(workspaceDAO, user, forced_values)

    # Tabs processing
    # Check if the workspace's tabs have order
    tabs = Tab.objects.filter(workspace=workspaceDAO).order_by('position')
    if tabs.count() > 0:
        if tabs[0].position is not None:
            tabs = tabs.order_by('position')
        else:
            # set default order
            for i in range(len(tabs)):
                tabs[i].position = i
                tabs[i].save()
    else:
        tabs = [createTab(_('Tab'), workspaceDAO)]

    tabs_data = [get_tab_data(tab) for tab in tabs]

    data_ret['tabs'] = tabs_data

    for tab in tabs_data:
        tab_pk = tab['id']
        iwidgets = IWidget.objects.filter(tab__id=tab_pk).order_by('id')

        iwidget_data = []
        for iwidget in iwidgets:
            iwidget_data.append(get_iwidget_data(iwidget, workspaceDAO, cache_manager))

        tab['iwidgets'] = iwidget_data

    data_ret['wiring'] = workspaceDAO.wiringStatus
    for forced_operator_id, forced_preferences in six.iteritems(forced_values['ioperator']):
        for forced_pref_name, forced_preference in six.iteritems(forced_preferences):
            data_ret['wiring']['operators'][forced_operator_id]['preferences'][forced_pref_name]['value'] = forced_preference['value']

    return json.dumps(data_ret, cls=LazyEncoder)