def put(self, request, collection): """ Update a collection using django rest framework Args: request: DRF Request object collection: Collection name Returns: Collection """ try: # Check if the object exists collection_obj = Collection.objects.get(name=collection) # Check for permissions if request.user.has_perm("update", collection_obj): serializer = CollectionSerializer(collection_obj, data=request.data, partial=True) if serializer.is_valid(): serializer.save() # update the lookup key if you update the name if 'name' in request.data and request.data['name'] != collection: lookup_key = str(collection_obj.pk) boss_key = request.data['name'] LookUpKey.update_lookup(lookup_key, boss_key, request.data['name']) return Response(serializer.data) else: return BossHTTPError("{}".format(serializer.errors), ErrorCodes.INVALID_POST_ARGUMENT) else: return BossPermissionError('update', collection) except Collection.DoesNotExist: return BossResourceNotFoundError(collection)
def put(self, request, collection, experiment, channel_layer): """ Update new Channel or Layer Args: request: DRF Request object collection: Collection name experiment: Experiment name channel_layer: Channel or Layer name Returns : ChannelLayer """ channel_layer_data = request.data.copy() if 'is_channel' in channel_layer_data: channel_layer_data['is_channel'] = self.get_bool(channel_layer_data['is_channel']) try: # Check if the object exists collection_obj = Collection.objects.get(name=collection) experiment_obj = Experiment.objects.get(name=experiment, collection=collection_obj) channel_layer_obj = ChannelLayer.objects.get(name=channel_layer, experiment=experiment_obj) if request.user.has_perm("update", channel_layer_obj): serializer = ChannelLayerSerializer(channel_layer_obj, data=request.data, partial=True) if serializer.is_valid(): serializer.save() # update the lookup key if you update the name if 'name' in request.data and request.data['name'] != channel_layer: lookup_key = str(collection_obj.pk) + '&' + str(experiment_obj.pk) + '&' \ + str(channel_layer_obj.pk) boss_key = collection_obj.name + '&' + experiment_obj.name + '&' + request.data['name'] LookUpKey.update_lookup(lookup_key, boss_key, collection_obj.name, experiment_obj.name, request.data['name']) return Response(serializer.data) else: return BossHTTPError("{}".format(serializer.errors), ErrorCodes.INVALID_POST_ARGUMENT) else: return BossPermissionError('update', channel_layer) except Collection.DoesNotExist: return BossResourceNotFoundError(collection) except Experiment.DoesNotExist: return BossResourceNotFoundError(experiment) except ChannelLayer.DoesNotExist: return BossResourceNotFoundError(channel_layer)
def put(self, request, collection, experiment): """ Update a experiment using django rest framework Args: request: DRF Request object collection: Collection name experiment : Experiment name for the new experiment Returns: Experiment """ try: # Check if the object exists collection_obj = Collection.objects.get(name=collection) experiment_obj = Experiment.objects.get(name=experiment, collection=collection_obj) if request.user.has_perm("update", experiment_obj): serializer = ExperimentUpdateSerializer(experiment_obj, data=request.data, partial=True) if serializer.is_valid(): serializer.save() # update the lookup key if you update the name if 'name' in request.data and request.data['name'] != experiment: lookup_key = str(collection_obj.pk) + '&' + str(experiment_obj.pk) boss_key = collection_obj.name + '&' + request.data['name'] LookUpKey.update_lookup(lookup_key, boss_key, collection_obj.name, request.data['name']) # return the object back to the user experiment = serializer.data['name'] experiment_obj = Experiment.objects.get(name=experiment, collection=collection_obj) serializer = ExperimentReadSerializer(experiment_obj) return Response(serializer.data) else: return BossHTTPError("{}".format(serializer.errors), ErrorCodes.INVALID_POST_ARGUMENT) else: return BossPermissionError('update', experiment) except Collection.DoesNotExist: return BossResourceNotFoundError(collection) except Experiment.DoesNotExist: return BossResourceNotFoundError(experiment)
def put(self, request, collection, experiment, channel): """ Update new Channel Args: request: DRF Request object collection: Collection name experiment: Experiment name channel: Channel name Returns : Channel """ if 'name' in request.data: channel_name = request.data['name'] else: channel_name = channel try: # Check if the object exists collection_obj = Collection.objects.get(name=collection) experiment_obj = Experiment.objects.get(name=experiment, collection=collection_obj) channel_obj = Channel.objects.get(name=channel, experiment=experiment_obj) if request.user.has_perm("update", channel_obj): # The source and related channels are names and need to be removed from the dict before serialization source_channels = request.data.pop('sources', []) related_channels = request.data.pop('related', []) # Validate the source and related channels if they are incuded channels = self.validate_source_related_channels(experiment_obj, source_channels, related_channels) source_channels_objs = channels[0] related_channels_objs = channels[1] serializer = ChannelUpdateSerializer(channel_obj, data=request.data, partial=True) if serializer.is_valid(): serializer.save() channel_obj = Channel.objects.get(name=channel_name, experiment=experiment_obj) # Save source and related channels if they are valid channel_obj = self.update_source_related_channels(channel_obj, experiment_obj, source_channels_objs, related_channels_objs) # update the lookup key if you update the name if 'name' in request.data and request.data['name'] != channel: lookup_key = str(collection_obj.pk) + '&' + str(experiment_obj.pk) + '&' \ + str(channel_obj.pk) boss_key = collection_obj.name + '&' + experiment_obj.name + '&' + request.data['name'] LookUpKey.update_lookup(lookup_key, boss_key, collection_obj.name, experiment_obj.name, request.data['name']) # return the object back to the user channel = serializer.data['name'] channel_obj = Channel.objects.get(name=channel, experiment=experiment_obj) serializer = ChannelReadSerializer(channel_obj) return Response(serializer.data) else: return BossHTTPError("{}".format(serializer.errors), ErrorCodes.INVALID_POST_ARGUMENT) else: return BossPermissionError('update', channel) except Collection.DoesNotExist: return BossResourceNotFoundError(collection) except Experiment.DoesNotExist: return BossResourceNotFoundError(experiment) except Channel.DoesNotExist: return BossResourceNotFoundError(channel)