def update_user_variable_values(self, variables, user, creator): for variable in variables: # Only properties and preferences have value if variable.vardef.aspect not in ('PROP', 'PREF'): continue #Does user have his own VariableValue? try: user_variable_value = VariableValue.objects.get(user=user, variable=variable) except VariableValue.DoesNotExist: user_variable_value = None #Does creator have his own VariableValue? try: creator_variable_value = VariableValue.objects.get(user=creator, variable=variable) except VariableValue.DoesNotExist: creator_variable_value = None created = False if not user_variable_value: # User VariableValue does not exist! Creating one! user_variable_value = VariableValue(user=user, value='', variable=variable) created = True #Updating User VariableValue value! user_variable_value = self.update_variable_value(user_variable_value, creator_variable_value, variable, created) if user_variable_value.value is not None: user_variable_value.save()
def update_user_variable_values(self, abstract_var_list, user, creator): for (abstract_var, variable) in abstract_var_list: #Does user have his own VariableValue? try: user_variable_value = VariableValue.objects.get(user=user, abstract_variable=abstract_var) except VariableValue.DoesNotExist: user_variable_value = None #Does creator have his own VariableValue? try: creator_variable_value = VariableValue.objects.get(user=creator, abstract_variable=abstract_var) except VariableValue.DoesNotExist: creator_variable_value = None created = False if (not user_variable_value): #User VariableValue does not exist! Creating one! user_variable_value = VariableValue(user=user, value='', abstract_variable=abstract_var) created = True #Updating User VariableValue value! user_variable_value = self.update_variable_value(user_variable_value, creator_variable_value, abstract_var, created) user_variable_value.save()
def add_user_to_abstract_variable_list(self, abstract_var_list, user): for (abstract_var, variable) in abstract_var_list: variable_values = VariableValue.objects.filter( user=user, abstract_variable=abstract_var) if (len(variable_values) > 0): #The VariableValue of this abstract_variable has been already cloned with the workspace structure #It's not necessary to create a default value from the VariableValue, the value was published by the workspace if (not abstract_var.has_public_value()): #There is a variable value but it shouldn't be returned to the user because it's not public!! #Changing it to default! variable_value = variable_values[0] if variable and variable.vardef.default_value: variable_value.value = variable.vardef.default_value else: variable_value.value = "" variable_value.save() continue #Creating VariableValue with default value for not public varaibles variable_value = VariableValue(user=user, value='', abstract_variable=abstract_var) # variable is the child element of the Variable inheritance # variable == None => wk_variable # variable != None => igadget_variable associated to abstract_variable if variable and variable.vardef.default_value: variable_value.value = variable.vardef.default_value variable_value.save()
def SaveIGadget(igadget, user, tab): gadget_uri = igadget.get('gadget') igadget_code = igadget.get('code') igadget_name = igadget.get('name') width = igadget.get('width') height = igadget.get('height') top = igadget.get('top') left = igadget.get('left') # Creates IGadget position position = Position(posX=left, posY=top, height=height, width=width, minimized=False) position.save() # Creates the new IGadget try: # Gadget uri does not contain the prefix "/user" yet if gadget_uri.startswith("/user") or gadget_uri.startswith("user"): gadget_uri_parts = gadget_uri.split("/") gadget_uri = "/" + "/".join(gadget_uri_parts[gadget_uri_parts.index("gadgets"):]) gadget = Gadget.objects.get(uri=gadget_uri, users=user) new_igadget = IGadget(code=igadget_code, name=igadget_name, gadget=gadget, tab=tab, position=position) new_igadget.save() variableDefs = VariableDef.objects.filter(gadget=gadget) for varDef in variableDefs: # Sets the default value of variable if varDef.default_value: var_value = varDef.default_value else: var_value = '' # Creating the Abstract Variable abstractVar = AbstractVariable(type="IGADGET", name=varDef.name) abstractVar.save() # Creating Value for Abstract Variable variableValue = VariableValue (user=user, value=var_value, abstract_variable=abstractVar) variableValue.save() var = Variable(vardef=varDef, igadget=new_igadget, abstract_variable=abstractVar) var.save() #Wiring related vars (SLOT&EVENTS) have implicit connectables! connectableId = createConnectable(var) transaction.commit() igadget_data = serializers.serialize('python', [new_igadget], ensure_ascii=False) ids = get_igadget_data(igadget_data[0], user, tab.workspace) return ids except Gadget.DoesNotExist: raise Gadget.DoesNotExist(_('referred gadget %(gadget_uri)s does not exist.') % {'gadget_uri': gadget_uri}) except VariableDef.DoesNotExist: #iGadget has no variables. It's normal pass
def createChannel(workspace, name, filter=None, filter_params={}, remote_subscription=None): # Creating abstract variable new_abstract_variable = AbstractVariable(type="WORKSPACE", name=name) new_abstract_variable.save() # Creating variable value new_variable_value = VariableValue(user=workspace.creator, 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() fparam_values = '' if filter is not None: fparam_values = json_encode(filter_params) channel = InOut(name=name, remote_subscription=remote_subscription, workspace_variable=new_ws_variable, filter=filter, filter_param_values=fparam_values, friend_code="") channel.save() return channel
def add_user_to_abstract_variable_list(self, abstract_var_list, user): for (abstract_var, variable) in abstract_var_list: variable_values = VariableValue.objects.filter(user=user, abstract_variable=abstract_var) if (len(variable_values)>0): #The VariableValue of this abstract_variable has been already cloned with the workspace structure #It's not necessary to create a default value from the VariableValue, the value was published by the workspace if (not abstract_var.has_public_value()): #There is a variable value but it shouldn't be returned to the user because it's not public!! #Changing it to default! variable_value = variable_values[0] if variable and variable.vardef.default_value: variable_value.value = variable.vardef.default_value else: variable_value.value = "" variable_value.save() continue #Creating VariableValue with default value for not public varaibles variable_value = VariableValue(user=user, value='', abstract_variable=abstract_var) # variable is the child element of the Variable inheritance # variable == None => wk_variable # variable != None => igadget_variable associated to abstract_variable if variable and variable.vardef.default_value: variable_value.value = variable.vardef.default_value variable_value.save()
def addIGadgetVariable(igadget, user, varDef, initial_value=None): # Sets the default value of variable if initial_value: var_value = initial_value elif varDef.default_value: var_value = varDef.default_value else: var_value = '' # Creating the Abstract Variable abstractVar = AbstractVariable(type="IGADGET", name=varDef.name) abstractVar.save() #check if there is a shared value or set a new one shared_value = None if varDef.shared_var_def: shared_value, created = SharedVariableValue.objects.get_or_create(user=user, shared_var_def=varDef.shared_var_def) if created: #init the value to share shared_value.value = var_value shared_value.save() else: #this VariableValue will take the previously shared value var_value = shared_value.value # Creating Value for Abstract Variable variableValue = VariableValue(user=user, value=var_value, abstract_variable=abstractVar, shared_var_value=shared_value) variableValue.save() var = Variable(vardef=varDef, igadget=igadget, abstract_variable=abstractVar) var.save() #Wiring related vars (SLOT&EVENTS) have implicit connectables! createConnectable(var)
def update_user_variable_values(self, abstract_var_list, user, creator): for (abstract_var, variable) in abstract_var_list: #Does user have his own VariableValue? try: user_variable_value = VariableValue.objects.get( user=user, abstract_variable=abstract_var) except VariableValue.DoesNotExist: user_variable_value = None #Does creator have his own VariableValue? try: creator_variable_value = VariableValue.objects.get( user=creator, abstract_variable=abstract_var) except VariableValue.DoesNotExist: creator_variable_value = None created = False if (not user_variable_value): #User VariableValue does not exist! Creating one! user_variable_value = VariableValue( user=user, value='', abstract_variable=abstract_var) created = True #Updating User VariableValue value! user_variable_value = self.update_variable_value( user_variable_value, creator_variable_value, abstract_var, created) if created: if creator_variable_value: #check if it's shared (only for igadget variables) if variable and variable.vardef.shared_var_def: shared_concept = variable.vardef.shared_var_def shared_var_value, is_new = SharedVariableValue.objects.get_or_create( user=user, shared_var_def=shared_concept) if is_new: if variable.has_public_value(): #clone the value the creator has set shared_var_value.value = SharedVariableValue.objects.get( user=creator, shared_var_def=shared_concept).value else: #set the default value shared_var_value.value = variable.get_default_value( ) shared_var_value.save() if creator_variable_value.shared_var_value: user_variable_value.shared_var_value = shared_var_value #remove the cloned variable value #creator_variable_value.delete() -> problems sharing workspaces. it cannot be done user_variable_value.save()
def add_user_to_abstract_variable_list(self, abstract_var_list, user): for (abstract_var, variable) in abstract_var_list: variable_value = VariableValue(user=user, value='', abstract_variable=abstract_var) # variable is the child element of the Variable inheritance # variable == None => wk_variable # variable != None => igadget_variable associated to abstract_variable if variable and variable.vardef.default_value: variable_value.value = variable.vardef.default_value variable_value.save()
def add_user_to_abstract_variable_list(self, abstract_var_list, user): for (abstract_var, variable) in abstract_var_list: variable_value = VariableValue(user=user, value='', abstract_variable=abstract_var) # variable is the child element of the Variable inheritance # variable == None => wk_variable # variable != None => igadget_variable associated to abstract_variable if variable and variable.vardef.default_value: variable_value.value = variable.vardef.default_value variable_value.save()
def update_user_variable_values(self, abstract_var_list, user, creator): for (abstract_var, variable) in abstract_var_list: #Does user have his own VariableValue? try: user_variable_value = VariableValue.objects.get(user=user, abstract_variable=abstract_var) except VariableValue.DoesNotExist: user_variable_value = None #Does creator have his own VariableValue? try: creator_variable_value = VariableValue.objects.get(user=creator, abstract_variable=abstract_var) except VariableValue.DoesNotExist: creator_variable_value = None created = False if (not user_variable_value): #User VariableValue does not exist! Creating one! user_variable_value = VariableValue(user=user, value='', abstract_variable=abstract_var) created = True #Updating User VariableValue value! user_variable_value = self.update_variable_value(user_variable_value, creator_variable_value, abstract_var, created) if created: if creator_variable_value: #check if it's shared (only for igadget variables) if variable and variable.vardef.shared_var_def: shared_concept = variable.vardef.shared_var_def shared_var_value, is_new = SharedVariableValue.objects.get_or_create(user=user, shared_var_def=shared_concept) if is_new: if variable.has_public_value(): #clone the value the creator has set shared_var_value.value = SharedVariableValue.objects.get(user=creator, shared_var_def=shared_concept).value else: #set the default value shared_var_value.value = variable.get_default_value() shared_var_value.save() if creator_variable_value.shared_var_value: user_variable_value.shared_var_value = shared_var_value #remove the cloned variable value #creator_variable_value.delete() -> problems sharing workspaces. it cannot be done user_variable_value.save()
def createTab(tab_name, user, workspace): # Creating Entry in AbstractVariable table for polimorphic access from Connectable hierarchy abstractVariable = AbstractVariable(name=tab_name, type='WORKSPACE') abstractVariable.save() # Creating Value for Abstract Variable variableValue = VariableValue(user=user, value="", abstract_variable=abstractVariable) variableValue.save() # Creating implicit workspace variable wsVariable = WorkSpaceVariable(workspace=workspace, aspect='TAB', abstract_variable=abstractVariable) wsVariable.save() #Creating implicit OUT Connectable element connectableName = 'tab_' + tab_name connectable = Out(name=connectableName, abstract_variable=abstractVariable) connectable.save() visible = False tabs = Tab.objects.filter(workspace=workspace, visible=True) if tabs.count() == 0: visible = True #it's always the last tab position = Tab.objects.filter(workspace=workspace).count() # Creating tab tab = Tab(name=tab_name, visible=visible, position=position, workspace=workspace, abstract_variable=abstractVariable) tab.save() # Returning created Ids ids = {} ids['id'] = tab.id ids['name'] = tab.name ids['workspaceVariables'] = [ get_workspace_variable_data(wsVariable, user, workspace) ] return ids
def clone_original_variable_value(abstract_variable, creator, new_user): try: original_var_value = VariableValue.objects.get(abstract_variable=abstract_variable, user=creator) value = original_var_value.get_variable_value() except VariableValue.DoesNotExist: #This VariableValue should exist. #However, published workspaces cloned in the old-fashioned way don't have the VariableValue of the creator variable! #Managing everything with AbstractVariable's default value, VariableValue it's unavailable! value = abstract_variable.get_default_value() cloned_value = VariableValue(user=new_user, value=value, abstract_variable=abstract_variable) cloned_value.save() return cloned_value
def createTab(tab_name, user, workspace): # Creating Entry in AbstractVariable table for polimorphic access from Connectable hierarchy abstractVariable = AbstractVariable(name=tab_name, type='WORKSPACE') abstractVariable.save() # Creating Value for Abstract Variable variableValue = VariableValue(user=user, value="", abstract_variable=abstractVariable) variableValue.save() # Creating implicit workspace variable wsVariable = WorkSpaceVariable(workspace=workspace, aspect='TAB', abstract_variable=abstractVariable) wsVariable.save() #Creating implicit OUT Connectable element connectableName = 'tab_' + tab_name connectable = Out(name=connectableName, abstract_variable=abstractVariable) connectable.save() visible = False tabs = Tab.objects.filter(workspace=workspace, visible=True) if tabs.count() == 0: visible = True # Creating tab tab = Tab(name=tab_name, visible=visible, locked=False, workspace=workspace, abstract_variable=abstractVariable) tab.save() # Returning created Ids ids = {} ids['id'] = tab.id ids['name'] = tab.name data = serializers.serialize('python', [wsVariable], ensure_ascii=False) ids['workspaceVariables'] = [ get_workspace_variable_data(d, user) for d in data ] return ids
def add_user_to_abstract_variable_list(self, abstract_var_list, user): for (abstract_var, variable) in abstract_var_list: if (len(VariableValue.objects.filter(user=user, abstract_variable=abstract_var))>0): #Can happen due to linking algorithm. #With a proper inheritance scheme for Variables database models, this can be avoided! # TO BE IMPROVED! continue variable_value = VariableValue(user=user, value='', abstract_variable=abstract_var) # variable is the child element of the Variable inheritance # variable == None => wk_variable # variable != None => igadget_variable associated to abstract_variable if variable and variable.vardef.default_value: variable_value.value = variable.vardef.default_value variable_value.save()
def clone_original_variable_value(abstract_variable, creator, new_user): try: original_var_value = VariableValue.objects.get( abstract_variable=abstract_variable, user=creator) value = original_var_value.get_variable_value() except VariableValue.DoesNotExist: #This VariableValue should exist. #However, published workspaces cloned in the old-fashioned way don't have the VariableValue of the creator variable! #Managing everything with AbstractVariable's default value, VariableValue it's unavailable! value = abstract_variable.get_default_value() cloned_value = VariableValue(user=new_user, value=value, abstract_variable=abstract_variable) cloned_value.save() return cloned_value
def createChannel(workspace, name, filter=None, filter_params={}, remote_subscription=None): # Creating abstract variable new_abstract_variable = AbstractVariable(type="WORKSPACE", name=name) new_abstract_variable.save() # Creating variable value new_variable_value = VariableValue(user=workspace.creator, 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() fparam_values = '' if filter is not None: fparam_values = json_encode(filter_params) channel = InOut(name=name, remote_subscription=remote_subscription, workspace_variable=new_ws_variable, filter=filter, filter_param_values=fparam_values, friend_code="") channel.save() return channel
def add_user_to_abstract_variable_list(self, abstract_var_list, user): for (abstract_var, variable) in abstract_var_list: if (len( VariableValue.objects.filter( user=user, abstract_variable=abstract_var)) > 0): #Can happen due to linking algorithm. #With a proper inheritance scheme for Variables database models, this can be avoided! # TO BE IMPROVED! continue variable_value = VariableValue(user=user, value='', abstract_variable=abstract_var) # variable is the child element of the Variable inheritance # variable == None => wk_variable # variable != None => igadget_variable associated to abstract_variable if variable and variable.vardef.default_value: variable_value.value = variable.vardef.default_value variable_value.save()
def addIGadgetVariable(igadget, user, varDef, initial_value=None): # Sets the default value of variable if initial_value: var_value = initial_value elif varDef.default_value: var_value = varDef.default_value else: var_value = '' # Creating the Abstract Variable abstractVar = AbstractVariable(type="IGADGET", name=varDef.name) abstractVar.save() #check if there is a shared value or set a new one shared_value = None if varDef.shared_var_def: shared_value, created = SharedVariableValue.objects.get_or_create( user=user, shared_var_def=varDef.shared_var_def) if created: #init the value to share shared_value.value = var_value shared_value.save() else: #this VariableValue will take the previously shared value var_value = shared_value.value # Creating Value for Abstract Variable variableValue = VariableValue(user=user, value=var_value, abstract_variable=abstractVar, shared_var_value=shared_value) variableValue.save() var = Variable(vardef=varDef, igadget=igadget, abstract_variable=abstractVar) var.save() #Wiring related vars (SLOT&EVENTS) have implicit connectables! createConnectable(var)
def createTab (tab_name, user, workspace): # Creating Entry in AbstractVariable table for polimorphic access from Connectable hierarchy abstractVariable = AbstractVariable (name=tab_name, type='WORKSPACE') abstractVariable.save() # Creating Value for Abstract Variable variableValue = VariableValue (user=user, value="", abstract_variable=abstractVariable) variableValue.save() # Creating implicit workspace variable wsVariable = WorkSpaceVariable (workspace=workspace, aspect='TAB', abstract_variable=abstractVariable) wsVariable.save() #Creating implicit OUT Connectable element connectableName = 'tab_' + tab_name; connectable = Out(name=connectableName, abstract_variable=abstractVariable) connectable.save() visible = False tabs = Tab.objects.filter(workspace=workspace, visible=True) if tabs.count()==0: visible = True #it's always the last tab position = Tab.objects.filter(workspace=workspace).count() # Creating tab tab = Tab (name=tab_name, visible=visible, position=position, workspace=workspace, abstract_variable=abstractVariable) tab.save() # Returning created Ids ids = {} ids['id'] = tab.id ids['name'] = tab.name data = serializers.serialize('python', [wsVariable], ensure_ascii=False) ids['workspaceVariables'] = [get_workspace_variable_data(d, user, workspace) for d in data] return ids
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) # 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!!! ids_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): 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() channel = InOut(name=new_channel_data['name'], workspace_variable=new_ws_variable, friend_code="") channel.save() #A channel has been generated. It's necessary to correlate provisional and definitive ids! id_mapping = {} id_mapping['id'] = channel.id; id_mapping['provisional_id'] = new_channel_data['id']; id_mapping['var_id'] = new_ws_variable.id; ids_mapping.append(id_mapping); 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() channel = InOut(id=new_channel_data['id'], name=new_channel_data['name'], workspace_variable=workspace_variable, friend_code="") channel.save() #Setting channel connections! #In connections ins = new_channel_data['ins'] for input in ins: if input['connectable_type'] == 'in': connectable = In.objects.get(id=input['id']) if input['connectable_type'] == 'inout': connectable = InOut.objects.get(id=input['id']) connectable.inouts.add(channel); connectable.save() #Out connections outs = new_channel_data['outs'] for output in outs: if output['connectable_type'] == 'out': connectable = Out.objects.get(id=output['id']) if output['connectable_type'] == 'inout': connectable = InOut.objects.get(id=output['id']) connectable.inouts.add(channel); connectable.save() # Saves all channels transaction.commit() json_result = {'ids': ids_mapping} return HttpResponse (json_encode(json_result), mimetype='application/json; charset=UTF-8') except WorkSpace.DoesNotExist: transaction.rollback() msg = _('referred workspace %(workspace_name)s does not exist.') % {'workspace_name': workspace_name} log(msg, request) return HttpResponseBadRequest(get_xml_error(msg)); except Exception, e: transaction.rollback() msg = _('connectables cannot be saved: %(exc)s') % {'exc': e} log(msg, request) return HttpResponseBadRequest(msg)
def SaveIGadget(igadget, user, tab): gadget_uri = igadget.get('gadget') igadget_name = igadget.get('name') width = igadget.get('width') height = igadget.get('height') top = igadget.get('top') left = igadget.get('left') zIndex = igadget.get('zIndex') layout = igadget.get('layout') menu_color = igadget.get('menu_color') # Creates IGadget position position = Position(posX=left, posY=top, posZ=zIndex, height=height, width=width, minimized=False) position.save() # Creates the new IGadget try: # Gadget uri does not contain the prefix "/user" yet if gadget_uri.startswith("/user") or gadget_uri.startswith("user"): gadget_uri_parts = gadget_uri.split("/") gadget_uri = "/" + "/".join(gadget_uri_parts[gadget_uri_parts.index("gadgets"):]) gadget = Gadget.objects.get(uri=gadget_uri, users=user) new_igadget = IGadget(name=igadget_name, gadget=gadget, tab=tab, layout=layout, position=position, transparency=False, menu_color=menu_color) new_igadget.save() variableDefs = VariableDef.objects.filter(gadget=gadget) for varDef in variableDefs: # Sets the default value of variable if varDef.default_value: var_value = varDef.default_value else: var_value = '' # Creating the Abstract Variable abstractVar = AbstractVariable(type="IGADGET", name=varDef.name) abstractVar.save() # Creating Value for Abstract Variable variableValue = VariableValue (user=user, value=var_value, abstract_variable=abstractVar) variableValue.save() var = Variable(vardef=varDef, igadget=new_igadget, abstract_variable=abstractVar) var.save() #Wiring related vars (SLOT&EVENTS) have implicit connectables! connectableId = createConnectable(var) transaction.commit() igadget_data = serializers.serialize('python', [new_igadget], ensure_ascii=False) ids = get_igadget_data(igadget_data[0], user, tab.workspace) return ids except Gadget.DoesNotExist, e: msg = _('referred gadget %(gadget_uri)s does not exist.') % {'gadget_uri': gadget_uri} raise TracedServerError(e, {'igadget': igadget, 'user': user, 'tab': tab}, 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 SaveIGadget(igadget, user, tab): gadget_uri = igadget.get('gadget') igadget_name = igadget.get('name') width = igadget.get('width') height = igadget.get('height') top = igadget.get('top') left = igadget.get('left') zIndex = igadget.get('zIndex') layout = igadget.get('layout') # Creates IGadget position position = Position(posX=left, posY=top, posZ=zIndex, height=height, width=width, minimized=False) position.save() # Creates the new IGadget try: # Gadget uri does not contain the prefix "/user" yet if gadget_uri.startswith("/user") or gadget_uri.startswith("user"): gadget_uri_parts = gadget_uri.split("/") gadget_uri = "/" + "/".join( gadget_uri_parts[gadget_uri_parts.index("gadgets"):]) gadget = Gadget.objects.get(uri=gadget_uri, users=user) new_igadget = IGadget(name=igadget_name, gadget=gadget, tab=tab, layout=layout, position=position, transparency=False) new_igadget.save() variableDefs = VariableDef.objects.filter(gadget=gadget) for varDef in variableDefs: # Sets the default value of variable if varDef.default_value: var_value = varDef.default_value else: var_value = '' # Creating the Abstract Variable abstractVar = AbstractVariable(type="IGADGET", name=varDef.name) abstractVar.save() # Creating Value for Abstract Variable variableValue = VariableValue(user=user, value=var_value, abstract_variable=abstractVar) variableValue.save() var = Variable(vardef=varDef, igadget=new_igadget, abstract_variable=abstractVar) var.save() #Wiring related vars (SLOT&EVENTS) have implicit connectables! connectableId = createConnectable(var) transaction.commit() igadget_data = serializers.serialize('python', [new_igadget], ensure_ascii=False) ids = get_igadget_data(igadget_data[0], user, tab.workspace) return ids except Gadget.DoesNotExist: raise Gadget.DoesNotExist( _('referred gadget %(gadget_uri)s does not exist.') % {'gadget_uri': gadget_uri}) except VariableDef.DoesNotExist: #iGadget has no variables. It's normal pass
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!!! ids_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): 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() channel = InOut(name=new_channel_data['name'], workspace_variable=new_ws_variable, friend_code="") channel.save() #A channel has been generated. It's necessary to correlate provisional and definitive ids! id_mapping = {} id_mapping['id'] = channel.id id_mapping['provisional_id'] = new_channel_data['id'] id_mapping['var_id'] = new_ws_variable.id ids_mapping.append(id_mapping) 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() channel = InOut(id=new_channel_data['id'], name=new_channel_data['name'], workspace_variable=workspace_variable, friend_code="") channel.save() #Setting channel connections! #In connections ins = new_channel_data['ins'] for input in ins: if input['connectable_type'] == 'in': connectable = In.objects.get(id=input['id']) if input['connectable_type'] == 'inout': connectable = InOut.objects.get(id=input['id']) connectable.inouts.add(channel) connectable.save() #Out connections outs = new_channel_data['outs'] for output in outs: if output['connectable_type'] == 'out': connectable = Out.objects.get(id=output['id']) if output['connectable_type'] == 'inout': connectable = InOut.objects.get(id=output['id']) connectable.inouts.add(channel) connectable.save() # Saves all channels transaction.commit() json_result = {'ids': ids_mapping} return HttpResponse(json_encode(json_result), mimetype='application/json; charset=UTF-8') except WorkSpace.DoesNotExist: transaction.rollback() msg = _( 'referred workspace %(workspace_name)s does not exist.') % { 'workspace_name': workspace_name } log(msg, request) return HttpResponseBadRequest(get_xml_error(msg)) except Exception, e: transaction.rollback() msg = _('connectables cannot be saved: %(exc)s') % {'exc': e} log(msg, request) return HttpResponseBadRequest(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 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')