def post(self, request, coordframe): """Create a new coordinate frame View to create a new coordinate frame Args: request: DRF Request object coordframe : Coordinate frame name Returns: CoordinateFrame """ coordframe_data = request.data.copy() coordframe_data['name'] = coordframe serializer = CoordinateFrameSerializer(data=coordframe_data) if serializer.is_valid(): serializer.save(creator=self.request.user) coordframe_obj = CoordinateFrame.objects.get(name=coordframe_data['name']) # Assign permissions to the users primary group and admin group BossPermissionManager.add_permissions_primary_group(self.request.user, coordframe_obj) BossPermissionManager.add_permissions_admin_group(coordframe_obj) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return BossHTTPError("{}".format(serializer.errors), ErrorCodes.INVALID_POST_ARGUMENT)
def put(self, request, coordframe): """ Update a coordinate frame using django rest framework Args: request: DRF Request object coordframe: Coordinate frame name Returns: CoordinateFrame """ try: # Check if the object exists coordframe_obj = CoordinateFrame.objects.get(name=coordframe) if request.user.has_perm("update", coordframe_obj): serializer = CoordinateFrameUpdateSerializer(coordframe_obj, data=request.data, partial=True) if serializer.is_valid(): serializer.save() # return the object back to the user coordframe = serializer.data['name'] coordframe_obj = CoordinateFrame.objects.get(name=coordframe) serializer = CoordinateFrameSerializer(coordframe_obj) return Response(serializer.data) else: return BossHTTPError("{}".format(serializer.errors), ErrorCodes.INVALID_POST_ARGUMENT) else: return BossPermissionError('update', coordframe) except CoordinateFrame.DoesNotExist: return BossResourceNotFoundError(coordframe)
def list(self, request, *args, **kwargs): """ Display only objects that a user has access to Args: request: DRF request *args: **kwargs: Returns: Coordinate frames that user has view permissions on """ coords = get_objects_for_user(request.user, 'read', klass=CoordinateFrame) serializer = CoordinateFrameSerializer(coords, many=True) return Response(serializer.data)
def get(self, request, coordframe): """ GET requests for a single instance of a coordinateframe Args: request: DRF Request object coordframe: Coordinate frame name specifying the coordinate frame you want Returns: CoordinateFrame """ try: coordframe_obj = CoordinateFrame.objects.get(name=coordframe) serializer = CoordinateFrameSerializer(coordframe_obj) return Response(serializer.data) except CoordinateFrame.DoesNotExist: return BossResourceNotFoundError(coordframe)
def get(self, request, coordframe): """ GET requests for a single instance of a coordinateframe Args: request: DRF Request object coordframe: Coordinate frame name specifying the coordinate frame you want Returns: CoordinateFrame """ try: coordframe_obj = CoordinateFrame.objects.get(name=coordframe) if coordframe_obj.to_be_deleted is not None: return BossHTTPError("Invalid Request. This Resource has been marked for deletion", ErrorCodes.RESOURCE_MARKED_FOR_DELETION) serializer = CoordinateFrameSerializer(coordframe_obj) return Response(serializer.data) except CoordinateFrame.DoesNotExist: return BossResourceNotFoundError(coordframe)