Beispiel #1
0
    def create(self, request, workspace_id):
        user = get_user_authentication(request)

        if 'tab' not in request.POST:
            return HttpResponseBadRequest(get_xml_error(_("tab JSON expected")), mimetype='application/xml; charset=UTF-8')

        #TODO we can make this with deserializers (simplejson)
        received_json = request.POST['tab']

        try:
            t = simplejson.loads(received_json)

            if 'name' not in t:
                raise Exception(_('Malformed tab JSON: expecting tab name.'))

            tab_name = t['name']
            workspace = WorkSpace.objects.get(users__id=user.id, pk=workspace_id)

            tab = createTab(tab_name, user, workspace)

            # Returning created Ids
            ids = {'id': tab.id, 'name': tab.name}

            return HttpResponse(json_encode(ids), mimetype='application/json; charset=UTF-8')

        except Exception, e:
            transaction.rollback()
            msg = _("tab cannot be created: ") + unicode(e)

            raise TracedServerError(e, t, request, msg)
Beispiel #2
0
def createEmptyWorkSpace(workSpaceName, user):
    active = False
    workspaces = UserWorkSpace.objects.filter(user__id=user.id, active=True)
    if workspaces.count() == 0:
        # there isn't yet an active workspace
        active = True

    #Workspace creation
    workspace = WorkSpace(name=workSpaceName, creator=user)
    workspace.save()

    #Adding user reference to workspace in the many to many relationship
    user_workspace = UserWorkSpace(user=user, workspace=workspace, active=active)
    user_workspace.save()

    #Tab creation
    createTab(_('Tab'), user, workspace)

    return workspace
def fillWorkspaceUsingTemplate(workspace, template, xml=None):

    if xml is None:
        if isinstance(template, unicode):
            # Work around: ValueError: Unicode strings with encoding
            # declaration are not supported.
            template = template.encode('utf-8')

        xml = etree.fromstring(template)

    user = workspace.creator
    concept_values = get_concept_values(user)
    processor = TemplateValueProcessor({'user': user, 'context': concept_values})

    workspace_structure = INCLUDED_RESOURCES_XPATH(xml)[0]
    read_only_workspace = workspace_structure.get('readonly') == 'true'

    preferences = PREFERENCE_XPATH(workspace_structure)
    new_values = {}
    igadget_id_mapping = {}
    for preference in preferences:
        new_values[preference.get('name')] = {
            'inherit': False,
            'value': preference.get('value'),
        }

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

    forced_values = {
        'extra_prefs': {},
        'igadget': {},
    }
    params = PARAM_XPATH(workspace_structure)
    for param in params:
        forced_values['extra_prefs'][param.get('name')] = {
            'inheritable': False,
            'label': param.get('label'),
            'type': param.get('type')
        }

    tabs = TAB_XPATH(workspace_structure)
    tab_id_mapping = {}

    for tabElement in tabs:
        tab = createTab(tabElement.get('name'), user, workspace, allow_renaming=True)
        tab_id_mapping[tabElement.get('id')] = tab

        preferences = PREFERENCE_XPATH(tabElement)
        new_values = {}
        for preference in preferences:
            new_values[preference.get('name')] = {
                'inherit': False,
                'value': preference.get('value'),
            }

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

        resources = RESOURCE_XPATH(tabElement)
        for resource in resources:
            igadget_uri = "/workspace/" + str(workspace.id) + "/tab/" + str(tab.id) + "/igadgets"

            position = POSITION_XPATH(resource)[0]
            rendering = RENDERING_XPATH(resource)[0]

            initial_variable_values = {}
            igadget_forced_values = {}
            properties = PROPERTIES_XPATH(resource)
            for prop in properties:
                read_only = prop.get('readonly')
                if read_only and read_only == 'true':
                    igadget_forced_values[prop.get('name')] = {'value': prop.get('value')}
                else:
                    initial_variable_values[prop.get('name')] = processor.process(prop.get('value'))

            preferences = PREFERENCE_XPATH(resource)
            for pref in preferences:
                read_only = pref.get('readonly')
                if read_only and read_only == 'true':
                    hidden = pref.get('hidden') == 'true'
                    igadget_forced_values[pref.get('name')] = {'value': pref.get('value'), 'hidden': hidden}
                else:
                    initial_variable_values[pref.get('name')] = processor.process(pref.get('value'))

            gadget = get_or_add_gadget_from_catalogue(resource.get('vendor'), resource.get('name'), resource.get('version'), user, None)

            igadget_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'),
                "menu_color": "FFFFFF",
                "layout": int(rendering.get('layout')),
                "uri": igadget_uri,
                "gadget": gadget.uri}

            igadget = SaveIGadget(igadget_data, user, tab, initial_variable_values)
            if read_only_workspace or resource.get('readonly') == 'true':
                igadget.readOnly = True
                igadget.save()

            forced_values['igadget'][str(igadget.id)] = igadget_forced_values
            igadget_id_mapping[resource.get('id')] = igadget

    if workspace.forcedValues != None and workspace.forcedValues != '':
        old_forced_values = simplejson.loads(workspace.forcedValues)
    else:
        old_forced_values = {
            'extra_preferences': {},
            'igadget': {},
        }

    forced_values['igadget'].update(old_forced_values['igadget'])
    workspace.forcedValues = simplejson.dumps(forced_values, ensure_ascii=False)
    workspace.save()

    # wiring
    wiring = WIRING_XPATH(xml)[0]
    channels = CHANNEL_XPATH(wiring)
    channel_connectables = {}
    for channel in channels:
        connectable = createChannel(workspace, channel.get('name'))

        save = False
        if read_only_workspace or channel.get('readonly') == 'true':
            connectable.readOnly = True
            save = True

        filter_name = channel.get('filter')
        if filter_name:
            save = True
            connectable.filter = Filter.objects.get(name=filter_name)
            connectable.filter_param_values = channel.get('filter_params')

        if save:
            connectable.save()

        channel_connectables[channel.get('id')] = {
            'connectable': connectable,
            'element': channel,
        }

    for key in channel_connectables:
        channel = channel_connectables[key]
        ins = IN_XPATH(channel['element'])
        for in_ in ins:
            igadget_id = in_.get('igadget')
            igadget = igadget_id_mapping[igadget_id]
            name = in_.get('name')

            connectable = In.objects.get(variable__vardef__name=name, variable__igadget=igadget)
            connectable.inouts.add(channel['connectable'])
            connectable.save()

        outs = OUT_XPATH(channel['element'])
        for out in outs:
            if 'igadget' in out.attrib:
                igadget_id = out.get('igadget')
                igadget = igadget_id_mapping[igadget_id]
                name = out.get('name')

                variable = Variable.objects.get(igadget=igadget, vardef__name=name)
                connectable = Out.objects.get(variable=variable)
            else:
                pass

            connectable.inouts.add(channel['connectable'])
            connectable.save()

        out_channels = CHANNEL_XPATH(channel['element'])
        for out_channel_element in out_channels:
            out_channel = channel_connectables[out_channel_element.get('id')]['connectable']
            relation = RelatedInOut(in_inout=channel['connectable'], out_inout=out_channel)
            relation.save()

    from commons.get_data import _invalidate_cached_variable_values
    _invalidate_cached_variable_values(workspace)
Beispiel #4
0
def _get_global_workspace_data(workSpaceDAO, user):
    data_ret = {}
    data_ret['workspace'] = get_workspace_data(workSpaceDAO, user)

    # Context information
    concept_values = get_concept_values(user)
    data_ret['workspace']['concepts'] = get_concepts_data(concept_values)

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

    # Process forced variable values
    forced_values = process_forced_values(workSpaceDAO, user, concept_values, preferences)
    data_ret['workspace']['empty_params'] = forced_values['empty_params']
    data_ret['workspace']['extra_prefs'] = forced_values['extra_prefs']
    if len(forced_values['empty_params']) > 0:
        return data_ret

    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('id')
    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'), user, workSpaceDAO)]

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

    data_ret['workspace']['tabList'] = tabs_data

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

        igadget_data = []
        for igadget in igadgets:
            igadget_data.append(get_igadget_data(igadget, user, workSpaceDAO, cache_manager))

        tab['igadgetList'] = igadget_data

    workspace_variables_data = get_workspace_variables_data(workSpaceDAO, user)
    data_ret['workspace']['channels'] = workspace_variables_data

    # Filter information
    filters = Filter.objects.all()
    data_ret['workspace']['filters'] = [get_filter_data(f) for f in filters]

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

    return data_ret