def update(self, request, workspace_id): user = get_user_authentication(request) # Gets all needed parameters from request if 'json' in request.POST: json = simplejson.loads(request.POST['json']) else: return HttpResponseBadRequest(_(u'JSON parameter expected')) workspace = get_object_or_404(WorkSpace, id=workspace_id) if not user.is_staff and workspace.creator != user: return HttpResponseForbidden() id_mapping = {} # Phase 1: Additions channels_to_add = json['channelsToAdd'] for new_channel in channels_to_add: # Creating the abstract variable for this channel new_abstract_variable = AbstractVariable(type="WORKSPACE", name=new_channel['name']) new_abstract_variable.save() # Creating the variable value entry for this channel new_variable_value = VariableValue(user=user, value="", abstract_variable=new_abstract_variable) new_variable_value.save() # And the workspace variable new_ws_variable = WorkSpaceVariable(workspace=workspace, abstract_variable=new_abstract_variable, aspect="CHANNEL") new_ws_variable.save() channel = InOut(name="", workspace_variable=new_ws_variable, filter=None, filter_param_values=None, friend_code="") channel.save() id_mapping[new_channel['id']] = {'cid': channel.id, 'wvid': new_ws_variable.id} # Phase 2: Updates channels_to_update = json['channelsToUpdate'] for current_channel_data in channels_to_update: current_channel_id = current_channel_data['id'] # search final id if needed if current_channel_data['provisional_id']: current_channel_id = id_mapping[current_channel_id]['cid'] current_channel = InOut.objects.get(id=current_channel_data['id']) for input_to_add in current_channel_data['inputsToRemove']: connectable = In.objects.get(id=input_to_add['id']) connectable.inouts.add(current_channel) connectable.save() for input_to_remove in current_channel_data['inputsToRemove']: connectable = In.objects.get(id=input_to_add['id']) connectable.inouts.remove(current_channel) connectable.save() for output_to_add in current_channel_data['outputsToRemove']: connectable = Out.objects.get(id=output_to_add['id']) connectable.inouts.add(current_channel) connectable.save() for output_to_remove in current_channel_data['outputsToRemove']: connectable = Out.objects.get(id=output_to_add['id']) connectable.inouts.remove(current_channel) connectable.save() for inout_to_add in current_channel_data['inoutsToRemove']: inout_id = input_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['cid'] relationship = RelatedInOut(in_inout=current_channel, out_inout=InOut.objects.get(id=inout_id)) relationship.save() for output_to_remove in current_channel_data['outputsToRemove']: inout_id = input_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['cid'] relationship = RelatedInOut.objects.get(in_inout=current_channel, out_inout=inout_id) relationship.delete() # Phase 3: Deletions channels_to_remove = json['channelsToRemove'] for current_channel_data in channels_to_remove: channel = InOut.objects.get(id=current_channel_data['id']) deleteChannel(channel) json_result = {'id_mapping': id_mapping} return HttpResponse(json_encode(json_result), mimetype='application/json; charset=UTF-8')
def create(self, request, workspace_id): user = get_user_authentication(request) workspace = get_object_or_404(WorkSpace, id=workspace_id) if not user.is_staff and workspace.creator != user: return HttpResponseForbidden() # Gets all needed parameters from request if 'json' in request.POST: json = simplejson.loads(request.POST['json']) else: return HttpResponseBadRequest(_(u'JSON parameter expected')) try: new_channels = json['inOutList'] old_channels = InOut.objects.filter(workspace_variable__workspace=workspace) # Mapping between provisional ids and database-generated ids!!! id_mapping = {} # Hash for mapping External Channels URLs and IDs rchannels_urls_to_ids = [] # A list of the channels removed by the user # Initially, all channels are considered to be deleted, and they # will be removed from this list if they appear in the inOuList # argument channelsDeletedByUser = old_channels[::1] # Disconnect all channels in the workspace for old_channel in old_channels: # Deleting the old relationships between channels # First delete the relationships where old_channel is the input rel_old_channels = RelatedInOut.objects.filter(in_inout=old_channel) for channel_delete in rel_old_channels: channel_delete.delete() # And then the relationships where old_channel is the output rel_old_channels = RelatedInOut.objects.filter(out_inout=old_channel) for channel_delete in rel_old_channels: channel_delete.delete() if old_channel.remote_subscription: old_channel.remote_subscription.delete() # Adding channels recreating JSON structure! for channel_id in new_channels: new_channel_data = new_channels[channel_id] # Remote subscriptions! remote_subscription = None if new_channel_data.get('remote_subscription', None): op_code = unicode(new_channel_data['remote_subscription']['op_code']) url = new_channel_data['remote_subscription']['url'] if op_code != '0': remote_channel, created = RemoteChannel.objects.get_or_create(url=url) data = dict() data['url'] = url data['id'] = remote_channel.id rchannels_urls_to_ids.append(data) remote_subscription = RemoteSubscription(operation_code=op_code, remote_channel=remote_channel) remote_subscription.save() if new_channel_data.get('provisional_id', False): # It's necessary to create a new channel from scratch filter = None filter_params = None if new_channel_data.get('filter_id', ''): filter = Filter.objects.get(id=new_channel_data['filter_id']) filter_params = new_channel_data['filter_params'] channel = createChannel(workspace, new_channel_data['name'], filter, filter_params, remote_subscription) # A channel has been generated. It's necessary to correlate provisional and final ids! id_mapping[new_channel_data['id']] = {'new_id': channel.id, 'new_wv_id': channel.workspace_variable.id} else: channel = InOut.objects.get(id=channel_id) workspace_variable = channel.workspace_variable workspace_variable.abstract_variable.name = new_channel_data['name'] workspace_variable.abstract_variable.save() filter = None filter_params = '' if new_channel_data.get('filter', ''): filter = Filter.objects.get(id=new_channel_data['filter']) filter_params = json_encode(new_channel_data['filter_params']) channel.remote_subscription = remote_subscription channel.name = new_channel_data['name'] channel.filter = filter channel.filter_param_values = filter_params channel.friend_code = "" channel.save() channelsDeletedByUser.remove(channel) # In connections # InOut out connections will be created later for inputId in new_channel_data.get('ins', []): connectable = In.objects.get(id=inputId) connectable.inouts.add(channel) connectable.save() # Out connections # InOut out connections will be created later for outputId in new_channel_data.get('outs', []): connectable = Out.objects.get(id=outputId) connectable.inouts.add(channel) connectable.save() # Now it is time to recreate channel to channel connections for channel_id in new_channels: new_channel_data = new_channels[channel_id] inout_id = new_channel_data['id'] if new_channel_data['provisional_id']: inout_id = id_mapping[inout_id]['new_id'] channel = InOut.objects.get(id=inout_id) for inout_to_add in new_channel_data.get('inouts', []): inout_id = inout_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['new_id'] relationship = RelatedInOut(in_inout=channel, out_inout=InOut.objects.get(id=inout_id)) relationship.save() # Erasing variables associated with channels deleted explicitly by the user for deleted_channel_id in channelsDeletedByUser: # Removing workspace_variable and abstract_variable of channels deleted explicitly by user deleteChannel(deleted_channel_id) json_result = {'ids': id_mapping, 'urls': rchannels_urls_to_ids} return HttpResponse(json_encode(json_result), mimetype='application/json; charset=UTF-8') except Exception, e: msg = _('connectables cannot be saved: %(exc)s') % {'exc': e} raise TracedServerError(e, json, request, msg)
def update(self, request, workspace_id): user = get_user_authentication(request) # Gets all needed parameters from request if 'json' in request.POST: json = simplejson.loads(request.POST['json']) else: return HttpResponseBadRequest(_(u'JSON parameter expected')) workspace = get_object_or_404(WorkSpace, id=workspace_id) if not user.is_staff and workspace.creator != user: return HttpResponseForbidden() id_mapping = {} # Phase 1: Additions channels_to_add = json['channelsToAdd'] for new_channel in channels_to_add: # Creating the abstract variable for this channel new_abstract_variable = AbstractVariable(type="WORKSPACE", name=new_channel['name']) new_abstract_variable.save() # Creating the variable value entry for this channel new_variable_value = VariableValue( user=user, value="", abstract_variable=new_abstract_variable) new_variable_value.save() # And the workspace variable new_ws_variable = WorkSpaceVariable( workspace=workspace, abstract_variable=new_abstract_variable, aspect="CHANNEL") new_ws_variable.save() channel = InOut(name="", workspace_variable=new_ws_variable, filter=None, filter_param_values=None, friend_code="") channel.save() id_mapping[new_channel['id']] = { 'cid': channel.id, 'wvid': new_ws_variable.id } # Phase 2: Updates channels_to_update = json['channelsToUpdate'] for current_channel_data in channels_to_update: current_channel_id = current_channel_data['id'] # search final id if needed if current_channel_data['provisional_id']: current_channel_id = id_mapping[current_channel_id]['cid'] current_channel = InOut.objects.get(id=current_channel_data['id']) for input_to_add in current_channel_data['inputsToRemove']: connectable = In.objects.get(id=input_to_add['id']) connectable.inouts.add(current_channel) connectable.save() for input_to_remove in current_channel_data['inputsToRemove']: connectable = In.objects.get(id=input_to_add['id']) connectable.inouts.remove(current_channel) connectable.save() for output_to_add in current_channel_data['outputsToRemove']: connectable = Out.objects.get(id=output_to_add['id']) connectable.inouts.add(current_channel) connectable.save() for output_to_remove in current_channel_data['outputsToRemove']: connectable = Out.objects.get(id=output_to_add['id']) connectable.inouts.remove(current_channel) connectable.save() for inout_to_add in current_channel_data['inoutsToRemove']: inout_id = input_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['cid'] relationship = RelatedInOut( in_inout=current_channel, out_inout=InOut.objects.get(id=inout_id)) relationship.save() for output_to_remove in current_channel_data['outputsToRemove']: inout_id = input_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['cid'] relationship = RelatedInOut.objects.get( in_inout=current_channel, out_inout=inout_id) relationship.delete() # Phase 3: Deletions channels_to_remove = json['channelsToRemove'] for current_channel_data in channels_to_remove: channel = InOut.objects.get(id=current_channel_data['id']) deleteChannel(channel) json_result = {'id_mapping': id_mapping} return HttpResponse(json_encode(json_result), mimetype='application/json; charset=UTF-8')
def create(self, request, workspace_id): user = get_user_authentication(request) workspace = get_object_or_404(WorkSpace, id=workspace_id) if not user.is_staff and workspace.creator != user: return HttpResponseForbidden() # Gets all needed parameters from request if 'json' in request.POST: json = simplejson.loads(request.POST['json']) else: return HttpResponseBadRequest(_(u'JSON parameter expected')) try: new_channels = json['inOutList'] old_channels = InOut.objects.filter( workspace_variable__workspace=workspace) # Mapping between provisional ids and database-generated ids!!! id_mapping = {} # Hash for mapping External Channels URLs and IDs rchannels_urls_to_ids = [] # A list of the channels removed by the user # Initially, all channels are considered to be deleted, and they # will be removed from this list if they appear in the inOuList # argument channelsDeletedByUser = old_channels[::1] # Disconnect all channels in the workspace for old_channel in old_channels: # Deleting the old relationships between channels # First delete the relationships where old_channel is the input rel_old_channels = RelatedInOut.objects.filter( in_inout=old_channel) for channel_delete in rel_old_channels: channel_delete.delete() # And then the relationships where old_channel is the output rel_old_channels = RelatedInOut.objects.filter( out_inout=old_channel) for channel_delete in rel_old_channels: channel_delete.delete() if old_channel.remote_subscription: old_channel.remote_subscription.delete() # Adding channels recreating JSON structure! for channel_id in new_channels: new_channel_data = new_channels[channel_id] # Remote subscriptions! remote_subscription = None if new_channel_data.get('remote_subscription', None): op_code = unicode( new_channel_data['remote_subscription']['op_code']) url = new_channel_data['remote_subscription']['url'] if op_code != '0': remote_channel, created = RemoteChannel.objects.get_or_create( url=url) data = dict() data['url'] = url data['id'] = remote_channel.id rchannels_urls_to_ids.append(data) remote_subscription = RemoteSubscription( operation_code=op_code, remote_channel=remote_channel) remote_subscription.save() if new_channel_data.get('provisional_id', False): # It's necessary to create a new channel from scratch filter = None filter_params = None if new_channel_data.get('filter_id', ''): filter = Filter.objects.get( id=new_channel_data['filter_id']) filter_params = new_channel_data['filter_params'] channel = createChannel(workspace, new_channel_data['name'], filter, filter_params, remote_subscription) # A channel has been generated. It's necessary to correlate provisional and final ids! id_mapping[new_channel_data['id']] = { 'new_id': channel.id, 'new_wv_id': channel.workspace_variable.id } else: channel = InOut.objects.get(id=channel_id) workspace_variable = channel.workspace_variable workspace_variable.abstract_variable.name = new_channel_data[ 'name'] workspace_variable.abstract_variable.save() filter = None filter_params = '' if new_channel_data.get('filter', ''): filter = Filter.objects.get( id=new_channel_data['filter']) filter_params = json_encode( new_channel_data['filter_params']) channel.remote_subscription = remote_subscription channel.name = new_channel_data['name'] channel.filter = filter channel.filter_param_values = filter_params channel.friend_code = "" channel.save() channelsDeletedByUser.remove(channel) # In connections # InOut out connections will be created later for inputId in new_channel_data.get('ins', []): connectable = In.objects.get(id=inputId) connectable.inouts.add(channel) connectable.save() # Out connections # InOut out connections will be created later for outputId in new_channel_data.get('outs', []): connectable = Out.objects.get(id=outputId) connectable.inouts.add(channel) connectable.save() # Now it is time to recreate channel to channel connections for channel_id in new_channels: new_channel_data = new_channels[channel_id] inout_id = new_channel_data['id'] if new_channel_data['provisional_id']: inout_id = id_mapping[inout_id]['new_id'] channel = InOut.objects.get(id=inout_id) for inout_to_add in new_channel_data.get('inouts', []): inout_id = inout_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['new_id'] relationship = RelatedInOut( in_inout=channel, out_inout=InOut.objects.get(id=inout_id)) relationship.save() # Erasing variables associated with channels deleted explicitly by the user for deleted_channel_id in channelsDeletedByUser: # Removing workspace_variable and abstract_variable of channels deleted explicitly by user deleteChannel(deleted_channel_id) json_result = {'ids': id_mapping, 'urls': rchannels_urls_to_ids} return HttpResponse(json_encode(json_result), mimetype='application/json; charset=UTF-8') except Exception, e: msg = _('connectables cannot be saved: %(exc)s') % {'exc': e} raise TracedServerError(e, json, request, msg)
def create(self, request, workspace_id): user = get_user_authentication(request) # Gets all needed parameters from request if request.POST.has_key('json'): json = simplejson.loads(request.POST['json']) else: return HttpResponseBadRequest (_(u'JSON parameter expected')) try: workspace = WorkSpace.objects.get(id=workspace_id) # Mapping between provisional ids and database-generated ids!!! id_mapping = {} # Erasing variables associated with channels deleted explicitly by the user channelsDeletedByUser = json['channelsForRemoving'] for deleted_channel_id in channelsDeletedByUser: #Removing workspace_variable and abstract_variable of channels deleted explicitly by user deleted_channel = InOut.objects.get(id=deleted_channel_id) #Remove the related In and Out values related_ins = deleted_channel.in_set.all() for rel_in in related_ins: varValue = VariableValue.objects.get(user=user, abstract_variable=rel_in.variable.abstract_variable) varValue.value = None varValue.save() related_outs = deleted_channel.out_set.all() for rel_out in related_outs: varValue = VariableValue.objects.get(user=user, abstract_variable=rel_out.abstract_variable) varValue.value = None varValue.save() abstract_variable = deleted_channel.workspace_variable.abstract_variable VariableValue.objects.get(user=user, abstract_variable=abstract_variable).delete() abstract_variable.delete() deleted_channel.workspace_variable.delete() if deleted_channel.remote_subscription: deleted_channel.remote_subscription.delete() # Erasing all channels of the workspace!! old_channels = InOut.objects.filter(workspace_variable__workspace=workspace) old_channels_info = {} for old_channel in old_channels: # Deleting the old relationships between channels # First delete the relationships where old_channel is the input rel_old_channels = RelatedInOut.objects.filter(in_inout=old_channel) for channel_delete in rel_old_channels: channel_delete.delete() # And then the relationships where old_channel is the output rel_old_channels = RelatedInOut.objects.filter(out_inout=old_channel) for channel_delete in rel_old_channels: channel_delete.delete() if old_channel.remote_subscription: old_channel.remote_subscription.delete() #adding its info to the list of old channels channel_info = {} old_ins_aux = old_channel.in_set.all() channel_info["ins"] = [] for in_aux in old_ins_aux: channel_info["ins"].append(in_aux.id) old_outs_aux = old_channel.out_set.all() channel_info["outs"] = [] for out_aux in old_outs_aux: channel_info["outs"].append(out_aux.id) old_channels_info[old_channel.id] = channel_info # Now delete the current channel old_channel.delete() # Adding channels recreating JSON structure! new_channels = json['inOutList'] for new_channel_data in new_channels: channel_info = None # Remote subscriptions! remote_subscription = None if new_channel_data['remote_subscription']: op_code = unicode(new_channel_data['remote_subscription']['op_code']) url = new_channel_data['remote_subscription']['url'] if op_code != '0': remote_subscription = RemoteSubscription(operation_code=op_code, url=url) remote_subscription.save() if (new_channel_data['provisional_id']): #It's necessary to create all objects! #Creating abstract variable new_abstract_variable = AbstractVariable(type="WORKSPACE", name=new_channel_data['name']) new_abstract_variable.save() #Creating variable value new_variable_value = VariableValue(user=user, value="", abstract_variable=new_abstract_variable) new_variable_value.save() new_ws_variable = WorkSpaceVariable(workspace=workspace, abstract_variable=new_abstract_variable, aspect="CHANNEL") new_ws_variable.save() filter = None fparam_values = None if new_channel_data['filter']: try: filter = Filter.objects.get(id=new_channel_data['filter']) fparam_values = json_encode(new_channel_data['filter_params']) except Filter.DoesNotExist: pass channel = InOut(name=new_channel_data['name'], remote_subscription=remote_subscription, workspace_variable=new_ws_variable, filter=filter, filter_param_values=fparam_values, friend_code="") channel.save() # A channel has been generated. It's necessary to correlate provisional and definitive ids! id_mapping[new_channel_data['id']] = {'new_id': channel.id, 'new_wv_id': new_ws_variable.id} channel_info = None else: #WorkSpaceVariable objects is still in database, it's only necessary to link it! workspace_variable = WorkSpaceVariable.objects.get(id=new_channel_data['var_id']) workspace_variable.abstract_variable.name = new_channel_data['name'] workspace_variable.abstract_variable.save() try: filter = Filter.objects.get(id=new_channel_data['filter']) fparam_values = json_encode(new_channel_data['filter_params']) except Filter.DoesNotExist: filter = None fparam_values = None channel = InOut(id=new_channel_data['id'], remote_subscription=remote_subscription, name=new_channel_data['name'], workspace_variable=workspace_variable, filter=filter, filter_param_values=fparam_values, friend_code="") channel.save() channel_info = old_channels_info[new_channel_data['id']] # In connections # InOut out connections will be created later old_ins = None if channel_info: old_ins = channel_info["ins"] ins = new_channel_data['ins'] for inputId in ins: connectable = In.objects.get(id=inputId) connectable.inouts.add(channel); connectable.save() if old_ins: #clean the old_ins list for old_in in old_ins: if old_in == inputId: old_ins.remove(old_in) break if old_ins: #check if there is any old In not present now to initialize its value for old_in in old_ins: real_old_in = In.objects.get(id=old_in) varValue = VariableValue.objects.get(user=user, abstract_variable=real_old_in.variable.abstract_variable) varValue.value = None varValue.save() # Out connections # InOut out connections will be created later old_outs = None if channel_info: old_outs = channel_info["outs"] outs = new_channel_data['outs'] for outputId in outs: connectable = Out.objects.get(id=outputId) connectable.inouts.add(channel); connectable.save() if old_outs: #clean the old_ins list for old_out in old_outs: if old_out == outputId: old_outs.remove(old_out) break if old_outs: #check if there is any old Out not present now to initialize its value for old_out in old_outs: real_old_out = Out.objects.get(id=old_out) varValue = VariableValue.objects.get(user=user, abstract_variable=real_old_out.abstract_variable) varValue.value = "" varValue.save() # Now it is time to recreate channel to channel connections for new_channel_data in new_channels: channel = InOut(id=new_channel_data['id']) inouts = new_channel_data['inouts'] for inout_to_add in inouts: inout_id = inout_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['new_id'] relationship = RelatedInOut(in_inout=channel, out_inout=InOut.objects.get(id=inout_id)) relationship.save() json_result = {'ids': id_mapping} return HttpResponse (json_encode(json_result), mimetype='application/json; charset=UTF-8') except WorkSpace.DoesNotExist, e: msg = _('referred workspace (id: %(workspace_id)s) does not exist.') % {'workspace_id': workspace_id} raise TracedServerError(e, json, request, msg)
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)
def update(self, request, workspace_id): user = get_user_authentication(request) # Gets all needed parameters from request if request.POST.has_key('json'): json = simplejson.loads(request.POST['json']) else: return HttpResponseBadRequest (_(u'JSON parameter expected')) try: workspace = WorkSpace.objects.get(id=workspace_id) except WorkSpace.DoesNotExist: msg = _('referred workspace %(workspace_name)s does not exist.') % {'workspace_name': workspace_name} raise TracedServerError(e, json, request, msg) id_mapping = {} # Pashe 1: Additions channels_to_add = json['channelsToAdd'] for new_channel in channels_to_add: # Creating the abstract variable for this channel new_abstract_variable = AbstractVariable(type="WORKSPACE", name=new_channel_data['name']) new_abstract_variable.save() # Creating the variable value entry for this channel new_variable_value = VariableValue(user=user, value="", abstract_variable=new_abstract_variable) new_variable_value.save() # And the workspace variable new_ws_variable = WorkSpaceVariable(workspace=workspace, abstract_variable=new_abstract_variable, aspect="CHANNEL") new_ws_variable.save() channel = InOut(name="", workspace_variable=new_ws_variable, filter=None, filter_param_values=None, friend_code="") channel.save() id_mapping[new_channel['id']] = {'cid': channel.id, 'wvid': new_ws_variable.id} # Pashe 2: Updates channels_to_update = json['channelsToUpdate'] for current_channel_data in channels_to_update: current_channel_id = current_channel_data['id'] # search final id if needed if current_channel_data['provisional_id']: current_channel_id = id_mapping[current_channel_id]['cid'] current_channel = InOut.objects.get(id=current_channel_data['id']) for input_to_add in current_channel_data['inputsToRemove']: connectable = In.objects.get(id=input_to_add['id']) connectable.inouts.add(current_channel); connectable.save() for input_to_remove in current_channel_data['inputsToRemove']: connectable = In.objects.get(id=input_to_add['id']) connectable.inouts.remove(current_channel); connectable.save() for output_to_add in current_channel_data['outputsToRemove']: connectable = Out.objects.get(id=output_to_add['id']) connectable.inouts.add(current_channel); connectable.save() for output_to_remove in current_channel_data['outputsToRemove']: connectable = Out.objects.get(id=output_to_add['id']) connectable.inouts.remove(current_channel); connectable.save() for inout_to_add in current_channel_data['inoutsToRemove']: inout_id = input_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['cid'] relationship = RelatedInOut(in_inout=current_channel, out_inout=InOut.objects.get(id=inout_id)) relationship.save() for output_to_remove in current_channel_data['outputsToRemove']: inout_id = input_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['cid'] relationship = RelatedInOut.objects.get(in_inout=current_channel, out_inout=inout_id) relationship.delete() # Pashe 3: Deletions channels_to_remove = json['channelsToRemove'] for current_channel_data in channels_to_remove: channel = InOut.objects.get(id=current_channel_data['id']) abstract_variable = channel.workspace_variable.abstract_variable VariableValue.objects.get(user=user, abstract_variable=abstract_variable).delete() abstract_variable.delete() channel.workspace_variable.delete() channel.delete() json_result = {'id_mapping': id_mapping} return HttpResponse (json_encode(json_result), mimetype='application/json; charset=UTF-8')
def create(self, request, workspace_id): user = get_user_authentication(request) workspace = get_object_or_404(WorkSpace, id=workspace_id) if not user.is_staff and workspace.creator != user: return HttpResponseForbidden() # Gets all needed parameters from request if "json" in request.POST: json = simplejson.loads(request.POST["json"]) else: return HttpResponseBadRequest(_(u"JSON parameter expected")) try: new_channels = json["inOutList"] old_channels = InOut.objects.filter(workspace=workspace) # Mapping between provisional ids and database-generated ids!!! id_mapping = {} # Hash for mapping External Channels URLs and IDs rchannels_urls_to_ids = [] # A list of the channels removed by the user # Initially, all channels are considered to be deleted, and they # will be removed from this list if they appear in the inOuList # argument channelsDeletedByUser = old_channels[::1] # Disconnect all channels in the workspace for old_channel in old_channels: # Deleting the old relationships between channels # First delete the relationships where old_channel is the input rel_old_channels = RelatedInOut.objects.filter(in_inout=old_channel) for channel_delete in rel_old_channels: channel_delete.delete() # And then the relationships where old_channel is the output rel_old_channels = RelatedInOut.objects.filter(out_inout=old_channel) for channel_delete in rel_old_channels: channel_delete.delete() old_channel.in_set.clear() old_channel.out_set.clear() if old_channel.remote_subscription: old_channel.remote_subscription.delete() # Adding channels recreating JSON structure! for channel_id in new_channels: new_channel_data = new_channels[channel_id] # Remote subscriptions! remote_subscription = None if new_channel_data.get("remote_subscription", None): op_code = unicode(new_channel_data["remote_subscription"]["op_code"]) url = new_channel_data["remote_subscription"]["url"] if op_code != "0": remote_channel, created = RemoteChannel.objects.get_or_create(url=url) data = dict() data["url"] = url data["id"] = remote_channel.id rchannels_urls_to_ids.append(data) remote_subscription = RemoteSubscription(operation_code=op_code, remote_channel=remote_channel) remote_subscription.save() if new_channel_data.get("provisional_id", False): # It's necessary to create a new channel from scratch filter = None filter_params = None if new_channel_data.get("filter_id", ""): filter = Filter.objects.get(id=new_channel_data["filter_id"]) filter_params = new_channel_data["filter_params"] channel = createChannel( workspace, new_channel_data["name"], filter, filter_params, remote_subscription ) # A channel has been generated. It's necessary to correlate provisional and final ids! id_mapping[new_channel_data["id"]] = channel.id else: channel = InOut.objects.get(id=channel_id) filter = None filter_params = "" if new_channel_data.get("filter", ""): filter = Filter.objects.get(id=new_channel_data["filter"]) filter_params = json_encode(new_channel_data["filter_params"]) channel.remote_subscription = remote_subscription channel.name = new_channel_data["name"] channel.filter = filter channel.filter_param_values = filter_params channel.friend_code = "" channel.save() channelsDeletedByUser.remove(channel) # In connections # InOut out connections will be created later for inputId in new_channel_data.get("ins", []): connectable = In.objects.get(variable__id=inputId) connectable.inouts.add(channel) connectable.save() # Out connections # InOut out connections will be created later for outputId in new_channel_data.get("outs", []): connectable = Out.objects.get(variable__id=outputId) connectable.inouts.add(channel) connectable.save() # Now it is time to recreate channel to channel connections for channel_id in new_channels: new_channel_data = new_channels[channel_id] inout_id = new_channel_data["id"] if new_channel_data["provisional_id"]: inout_id = id_mapping[inout_id] channel = InOut.objects.get(id=inout_id) for inout_to_add in new_channel_data.get("inouts", []): inout_id = inout_to_add["id"] # search final id if needed if inout_to_add["provisional_id"]: inout_id = id_mapping[inout_id] relationship = RelatedInOut(in_inout=channel, out_inout=InOut.objects.get(id=inout_id)) relationship.save() for deleted_channel_id in channelsDeletedByUser: deleteChannel(deleted_channel_id) json_result = {"ids": id_mapping, "urls": rchannels_urls_to_ids} return HttpResponse(json_encode(json_result), mimetype="application/json; charset=UTF-8") except Exception, e: msg = _("connectables cannot be saved: %(exc)s") % {"exc": e} raise TracedServerError(e, json, request, msg)
def update(self, request, workspace_id): user = get_user_authentication(request) # Gets all needed parameters from request if "json" in request.POST: json = simplejson.loads(request.POST["json"]) else: return HttpResponseBadRequest(_(u"JSON parameter expected")) workspace = get_object_or_404(WorkSpace, id=workspace_id) if not user.is_staff and workspace.creator != user: return HttpResponseForbidden() id_mapping = {} # Phase 1: Additions channels_to_add = json["channelsToAdd"] for new_channel in channels_to_add: channel = createChannel(workspace, new_channel["name"], filter=None, filter_params=None, friend_code="") id_mapping[new_channel["id"]] = channel.id # Phase 2: Updates channels_to_update = json["channelsToUpdate"] for current_channel_data in channels_to_update: current_channel_id = current_channel_data["id"] # search final id if needed if current_channel_data["provisional_id"]: current_channel_id = id_mapping[current_channel_id]["cid"] current_channel = InOut.objects.get(id=current_channel_data["id"]) for input_to_add in current_channel_data["inputsToRemove"]: connectable = In.objects.get(id=input_to_add["id"]) connectable.inouts.add(current_channel) connectable.save() for input_to_remove in current_channel_data["inputsToRemove"]: connectable = In.objects.get(id=input_to_add["id"]) connectable.inouts.remove(current_channel) connectable.save() for output_to_add in current_channel_data["outputsToRemove"]: connectable = Out.objects.get(id=output_to_add["id"]) connectable.inouts.add(current_channel) connectable.save() for output_to_remove in current_channel_data["outputsToRemove"]: connectable = Out.objects.get(id=output_to_add["id"]) connectable.inouts.remove(current_channel) connectable.save() for inout_to_add in current_channel_data["inoutsToRemove"]: inout_id = input_to_add["id"] # search final id if needed if inout_to_add["provisional_id"]: inout_id = id_mapping[inout_id] relationship = RelatedInOut(in_inout=current_channel, out_inout=InOut.objects.get(id=inout_id)) relationship.save() for output_to_remove in current_channel_data["outputsToRemove"]: inout_id = input_to_add["id"] # search final id if needed if inout_to_add["provisional_id"]: inout_id = id_mapping[inout_id] relationship = RelatedInOut.objects.get(in_inout=current_channel, out_inout=inout_id) relationship.delete() # Phase 3: Deletions channels_to_remove = json["channelsToRemove"] for current_channel_data in channels_to_remove: channel = InOut.objects.get(id=current_channel_data["id"]) deleteChannel(channel) json_result = {"id_mapping": id_mapping} return HttpResponse(json_encode(json_result), mimetype="application/json; charset=UTF-8")
def create(self, request, workspace_id): user = get_user_authentication(request) # Gets all needed parameters from request if request.POST.has_key('json'): json = simplejson.loads(request.POST['json']) else: return HttpResponseBadRequest (_(u'JSON parameter expected')) try: workspace = WorkSpace.objects.get(id=workspace_id) # Mapping between provisional ids and database-generated ids!!! id_mapping = {} # Erasing variables associated with channels deleted explicitly by the user channelsDeletedByUser = json['channelsForRemoving'] for deleted_channel_id in channelsDeletedByUser: #Removing workspace_variable and abstract_variable of channels deleted explicitly by user deleted_channel = InOut.objects.get(id=deleted_channel_id) abstract_variable = deleted_channel.workspace_variable.abstract_variable VariableValue.objects.get(user=user, abstract_variable=abstract_variable).delete() abstract_variable.delete() deleted_channel.workspace_variable.delete() # Erasing all channels of the workspace!! old_channels = InOut.objects.filter(workspace_variable__workspace=workspace) if old_channels: for old_channel in old_channels: # Deleting the old relationships between channels # First delete the relationships where old_channel is the input rel_old_channels = RelatedInOut.objects.filter(in_inout=old_channel) if rel_old_channels: rel_old_channels.delete() # And then the relationships where old_channel is the output rel_old_channels = RelatedInOut.objects.filter(out_inout=old_channel) if rel_old_channels: rel_old_channels.delete() old_channels.delete() # Adding channels recreating JSON structure! new_channels = json['inOutList'] for new_channel_data in new_channels: if (new_channel_data['provisional_id']): #It's necessary to create all objects! #Creating abstract variable new_abstract_variable = AbstractVariable(type="WORKSPACE", name=new_channel_data['name']) new_abstract_variable.save() #Creating variable value new_variable_value = VariableValue(user=user, value="", abstract_variable=new_abstract_variable) new_variable_value.save() new_ws_variable = WorkSpaceVariable(workspace=workspace, abstract_variable=new_abstract_variable, aspect="CHANNEL") new_ws_variable.save() filter = None fparam_values = None if new_channel_data['filter']: try: filter = Filter.objects.get(id=new_channel_data['filter']) fparam_values = new_channel_data['filter_params'] except Filter.DoesNotExist: pass channel = InOut(name=new_channel_data['name'], workspace_variable=new_ws_variable, filter=filter, filter_param_values=fparam_values, friend_code="") channel.save() # A channel has been generated. It's necessary to correlate provisional and definitive ids! id_mapping[new_channel_data['id']] = {'new_id': channel.id, 'new_wv_id': new_ws_variable.id} else: #WorkSpaceVariable objects is still in database, it's only necessary to link it! workspace_variable = WorkSpaceVariable.objects.get(id=new_channel_data['var_id']) workspace_variable.abstract_variable.name = new_channel_data['name'] workspace_variable.abstract_variable.save() try: filter = Filter.objects.get(id=new_channel_data['filter']) fparam_values = new_channel_data['filter_params'] except Filter.DoesNotExist: filter = None fparam_values = None channel = InOut(id=new_channel_data['id'], name=new_channel_data['name'], workspace_variable=workspace_variable, filter=filter, filter_param_values=fparam_values, friend_code="") channel.save() # In connections # InOut out connections will be created later ins = new_channel_data['ins'] for inputId in ins: connectable = In.objects.get(id=inputId) connectable.inouts.add(channel); connectable.save() # Out connections # InOut out connections will be created later outs = new_channel_data['outs'] for outputId in outs: connectable = Out.objects.get(id=outputId) connectable.inouts.add(channel); connectable.save() # Now it is time to recreate channel to channel connections for new_channel_data in new_channels: inouts = new_channel_data['inouts'] for inout_to_add in inouts: inout_id = inout_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['new_id'] relationship = RelatedInOut(in_inout=channel, out_inout=InOut.objects.get(id=inout_id)) relationship.save() # Saves all channels #transaction.commit() json_result = {'ids': id_mapping} return HttpResponse (json_encode(json_result), mimetype='application/json; charset=UTF-8') except WorkSpace.DoesNotExist: msg = _('referred workspace %(workspace_name)s does not exist.') % {'workspace_name': workspace_name} raise TracedServerError(e, json, request, msg) except Exception, e: msg = _('connectables cannot be saved: %(exc)s') % {'exc': e} raise TracedServerError(e, json, request, msg)
def create(self, request, workspace_id): user = get_user_authentication(request) # Gets all needed parameters from request if request.POST.has_key('json'): json = simplejson.loads(request.POST['json']) else: return HttpResponseBadRequest(_(u'JSON parameter expected')) try: workspace = WorkSpace.objects.get(id=workspace_id) # Mapping between provisional ids and database-generated ids!!! id_mapping = {} # Erasing variables associated with channels deleted explicitly by the user channelsDeletedByUser = json['channelsForRemoving'] for deleted_channel_id in channelsDeletedByUser: #Removing workspace_variable and abstract_variable of channels deleted explicitly by user deleted_channel = InOut.objects.get(id=deleted_channel_id) abstract_variable = deleted_channel.workspace_variable.abstract_variable VariableValue.objects.get( user=user, abstract_variable=abstract_variable).delete() abstract_variable.delete() deleted_channel.workspace_variable.delete() # Erasing all channels of the workspace!! old_channels = InOut.objects.filter( workspace_variable__workspace=workspace) if old_channels: for old_channel in old_channels: # Deleting the old relationships between channels # First delete the relationships where old_channel is the input rel_old_channels = RelatedInOut.objects.filter( in_inout=old_channel) if rel_old_channels: rel_old_channels.delete() # And then the relationships where old_channel is the output rel_old_channels = RelatedInOut.objects.filter( out_inout=old_channel) if rel_old_channels: rel_old_channels.delete() old_channels.delete() # Adding channels recreating JSON structure! new_channels = json['inOutList'] for new_channel_data in new_channels: if (new_channel_data['provisional_id']): #It's necessary to create all objects! #Creating abstract variable new_abstract_variable = AbstractVariable( type="WORKSPACE", name=new_channel_data['name']) new_abstract_variable.save() #Creating variable value new_variable_value = VariableValue( user=user, value="", abstract_variable=new_abstract_variable) new_variable_value.save() new_ws_variable = WorkSpaceVariable( workspace=workspace, abstract_variable=new_abstract_variable, aspect="CHANNEL") new_ws_variable.save() filter = None fparam_values = None if new_channel_data['filter']: try: filter = Filter.objects.get( id=new_channel_data['filter']) fparam_values = new_channel_data['filter_params'] except Filter.DoesNotExist: pass channel = InOut(name=new_channel_data['name'], workspace_variable=new_ws_variable, filter=filter, filter_param_values=fparam_values, friend_code="") channel.save() # A channel has been generated. It's necessary to correlate provisional and definitive ids! id_mapping[new_channel_data['id']] = { 'new_id': channel.id, 'new_wv_id': new_ws_variable.id } else: #WorkSpaceVariable objects is still in database, it's only necessary to link it! workspace_variable = WorkSpaceVariable.objects.get( id=new_channel_data['var_id']) workspace_variable.abstract_variable.name = new_channel_data[ 'name'] workspace_variable.abstract_variable.save() try: filter = Filter.objects.get( id=new_channel_data['filter']) fparam_values = new_channel_data['filter_params'] except Filter.DoesNotExist: filter = None fparam_values = None channel = InOut(id=new_channel_data['id'], name=new_channel_data['name'], workspace_variable=workspace_variable, filter=filter, filter_param_values=fparam_values, friend_code="") channel.save() # In connections # InOut out connections will be created later ins = new_channel_data['ins'] for inputId in ins: connectable = In.objects.get(id=inputId) connectable.inouts.add(channel) connectable.save() # Out connections # InOut out connections will be created later outs = new_channel_data['outs'] for outputId in outs: connectable = Out.objects.get(id=outputId) connectable.inouts.add(channel) connectable.save() # Now it is time to recreate channel to channel connections for new_channel_data in new_channels: inouts = new_channel_data['inouts'] for inout_to_add in inouts: inout_id = inout_to_add['id'] # search final id if needed if inout_to_add['provisional_id']: inout_id = id_mapping[inout_id]['new_id'] relationship = RelatedInOut( in_inout=channel, out_inout=InOut.objects.get(id=inout_id)) relationship.save() # Saves all channels #transaction.commit() json_result = {'ids': id_mapping} return HttpResponse(json_encode(json_result), mimetype='application/json; charset=UTF-8') except WorkSpace.DoesNotExist: msg = _( 'referred workspace %(workspace_name)s does not exist.') % { 'workspace_name': workspace_name } raise TracedServerError(e, json, request, msg) except Exception, e: msg = _('connectables cannot be saved: %(exc)s') % {'exc': e} raise TracedServerError(e, json, request, msg)