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 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 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 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")