コード例 #1
0
ファイル: views.py プロジェクト: aml-development/ozp-backend
    def retrieve(self, request, pk):
        if not hal.validate_version(request.META.get('HTTP_ACCEPT')):
            return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE)
        # listing_root_url = hal.get_abs_url_for_iwc(request)  # flake8: noqa TODO: Is Necessary? - Variable not being used in method
        # data = hal.create_base_structure(request,   # flake8: noqa TODO: Is Necessary? - Variable not being used in method
        #     hal.generate_content_type(
        #         request.accepted_media_type))
        key = self._get_key(pk)
        logger.debug('Got GET IWC Data request for key {0!s}'.format(key))

        instance = model_access.get_data_resource(request.user.username, key)
        if not instance:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = serializers.DataResourceSerializer(instance,
            data=request.data, context={'request': request, 'key': key},
            partial=True)
        if not serializer.is_valid():
            logger.error('{0!s}'.format(serializer.errors))
            return Response(serializer.errors,
                status=status.HTTP_400_BAD_REQUEST)
        resp = serializer.data
        resp = hal.add_hal_structure(resp, request,
            hal.generate_content_type(
                request.accepted_media_type))
        return Response(resp, status=status.HTTP_200_OK)
コード例 #2
0
    def retrieve(self, request, pk):
        if not hal.validate_version(request.META.get('HTTP_ACCEPT')):
            return Response('Invalid version requested',
                            status=status.HTTP_406_NOT_ACCEPTABLE)
        # listing_root_url = hal.get_abs_url_for_iwc(request)  # flake8: noqa TODO: Is Necessary? - Variable not being used in method
        # data = hal.create_base_structure(request,   # flake8: noqa TODO: Is Necessary? - Variable not being used in method
        #     hal.generate_content_type(
        #         request.accepted_media_type))
        key = self._get_key(pk)
        logger.debug('Got GET IWC Data request for key {0!s}'.format(key))

        instance = model_access.get_data_resource(request.user.username, key)
        if not instance:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = serializers.DataResourceSerializer(instance,
                                                        data=request.data,
                                                        context={
                                                            'request': request,
                                                            'key': key
                                                        },
                                                        partial=True)
        if not serializer.is_valid():
            logger.error('{0!s}'.format(serializer.errors))
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        resp = serializer.data
        resp = hal.add_hal_structure(
            resp, request,
            hal.generate_content_type(request.accepted_media_type))
        return Response(resp, status=status.HTTP_200_OK)
コード例 #3
0
def ListDataApiView(request):
    """
    List all data entries for the user
    """
    if not hal.validate_version(request.META.get('HTTP_ACCEPT')):
        return Response('Invalid version requested',
                        status=status.HTTP_406_NOT_ACCEPTABLE)

    listing_root_url = hal.get_abs_url_for_iwc(
        request
    )  # flake8: noqa TODO: Is Necessary? - Variable not being used in method

    data = hal.create_base_structure(
        request, hal.generate_content_type(request.accepted_media_type))

    keys = model_access.get_all_keys(request.user.username)
    embedded_items = []
    for k in keys:
        # remove the leading /
        k = k[1:]
        url = hal.get_abs_url_for_iwc(request) + 'self/data/' + k
        data = hal.add_link_item(
            url, data,
            hal.generate_content_type(
                renderers.DataObjectResourceRenderer.media_type))

        # add data items to _embedded
        key = '/' + k
        try:
            instance = model_access.get_data_resource(request.user.username,
                                                      key)
            if not instance:
                return Response(status=status.HTTP_404_NOT_FOUND)
            serializer = serializers.DataResourceSerializer(instance,
                                                            data=request.data,
                                                            context={
                                                                'request':
                                                                request,
                                                                'key': key
                                                            },
                                                            partial=True)
            if not serializer.is_valid():
                logger.error('{0!s}'.format(serializer.errors))
                return Response(serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
            item = hal.add_hal_structure(
                serializer.data, request,
                hal.generate_content_type(
                    renderers.DataObjectResourceRenderer.media_type))
            item['_links']['self']['href'] += k
            embedded_items.append(item)
        except Exception as e:
            # TODO debug
            raise e

        data['_embedded']['item'] = embedded_items

    return Response(data)
コード例 #4
0
ファイル: views.py プロジェクト: aml-development/ozp-backend
    def destroy(self, request, pk=None):
        if not hal.validate_version(request.META.get('HTTP_ACCEPT')):
            return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE)

        key = self._get_key(pk)
        logger.debug('Got GET IWC Data request for key {0!s}'.format(key))

        instance = model_access.get_data_resource(request.user.username, key)
        if instance:
            instance.delete()
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)
        return Response(status=status.HTTP_204_NO_CONTENT)
コード例 #5
0
    def destroy(self, request, pk=None):
        if not hal.validate_version(request.META.get('HTTP_ACCEPT')):
            return Response('Invalid version requested',
                            status=status.HTTP_406_NOT_ACCEPTABLE)

        key = self._get_key(pk)
        logger.debug('Got GET IWC Data request for key {0!s}'.format(key))

        instance = model_access.get_data_resource(request.user.username, key)
        if instance:
            instance.delete()
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)
        return Response(status=status.HTTP_204_NO_CONTENT)
コード例 #6
0
ファイル: views.py プロジェクト: TensorWrenchOSS/ozp-backend
def ListDataApiView(request):
    """
    List all data entries for the user
    """
    if not hal.validate_version(request.META.get('HTTP_ACCEPT')):
        return Response('Invalid version requested',
            status=status.HTTP_406_NOT_ACCEPTABLE)

    listing_root_url = hal.get_abs_url_for_iwc(request)

    data = hal.create_base_structure(request,
        hal.generate_content_type(request.accepted_media_type))

    keys = model_access.get_all_keys(request.user.username)
    embedded_items = []
    for k in keys:
        # remove the leading /
        k = k[1:]
        url = hal.get_abs_url_for_iwc(request) + 'self/data/' + k
        data = hal.add_link_item(url, data, hal.generate_content_type(
            renderers.DataObjectResourceRenderer.media_type))

        # add data items to _embedded
        key = '/' + k
        try:
            instance = model_access.get_data_resource(request.user.username,
                key)
            if not instance:
                return Response(status=status.HTTP_404_NOT_FOUND)
            serializer = serializers.DataResourceSerializer(instance,
                data=request.data, context={'request': request, 'key': key},
                partial=True)
            if not serializer.is_valid():
                logger.error('%s' % serializer.errors)
                return Response(serializer.errors,
                    status=status.HTTP_400_BAD_REQUEST)
            item = hal.add_hal_structure(serializer.data, request,
                hal.generate_content_type(
                    renderers.DataObjectResourceRenderer.media_type))
            item['_links']['self']['href'] += k
            embedded_items.append(item)
        except Exception as e:
            # TODO debug
            raise e

        data['_embedded']['item'] = embedded_items


    return Response(data)
コード例 #7
0
    def update(self, request, pk=None):
        if not hal.validate_version(request.META.get('HTTP_ACCEPT')):
            return Response('Invalid version requested',
                            status=status.HTTP_406_NOT_ACCEPTABLE)

        key = self._get_key(pk)
        logger.debug('Got GET IWC Data request for key {0!s}'.format(key))

        logger.debug('request.data: {0!s}'.format(request.data))
        instance = model_access.get_data_resource(request.user.username, key)

        if instance:
            serializer = serializers.DataResourceSerializer(instance,
                                                            data=request.data,
                                                            context={
                                                                'request':
                                                                request,
                                                                'key': key
                                                            },
                                                            partial=True)
            if not serializer.is_valid():
                logger.error('{0!s}'.format(serializer.errors))
                return Response(serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
            serializer.save()
            resp = serializer.data
            resp = hal.add_hal_structure(
                resp, request,
                hal.generate_content_type(request.accepted_media_type))
            return Response(resp, status=status.HTTP_200_OK)
        else:
            serializer = serializers.DataResourceSerializer(data=request.data,
                                                            context={
                                                                'request':
                                                                request,
                                                                'key': key
                                                            },
                                                            partial=True)
            if not serializer.is_valid():
                logger.error('ERROR: {0!s}'.format(serializer.errors))
                return Response(serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
            serializer.save()
            resp = serializer.data
            resp = hal.add_hal_structure(
                resp, request,
                hal.generate_content_type(request.accepted_media_type))
            return Response(resp, status=status.HTTP_201_CREATED)
コード例 #8
0
ファイル: views.py プロジェクト: aml-development/ozp-backend
    def update(self, request, pk=None):
        if not hal.validate_version(request.META.get('HTTP_ACCEPT')):
            return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE)

        key = self._get_key(pk)
        logger.debug('Got GET IWC Data request for key {0!s}'.format(key))

        logger.debug('request.data: {0!s}'.format(request.data))
        instance = model_access.get_data_resource(request.user.username, key)

        if instance:
            serializer = serializers.DataResourceSerializer(instance,
                data=request.data, context={'request': request, 'key': key},
                partial=True)
            if not serializer.is_valid():
                logger.error('{0!s}'.format(serializer.errors))
                return Response(serializer.errors,
                    status=status.HTTP_400_BAD_REQUEST)
            serializer.save()
            resp = serializer.data
            resp = hal.add_hal_structure(resp, request,
                hal.generate_content_type(
                    request.accepted_media_type))
            return Response(resp, status=status.HTTP_200_OK)
        else:
            serializer = serializers.DataResourceSerializer(
                data=request.data, context={'request': request, 'key': key},
                partial=True)
            if not serializer.is_valid():
                logger.error('ERROR: {0!s}'.format(serializer.errors))
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
            serializer.save()
            resp = serializer.data
            resp = hal.add_hal_structure(resp, request,
                hal.generate_content_type(
                    request.accepted_media_type))
            return Response(resp, status=status.HTTP_201_CREATED)
コード例 #9
0
def DataApiView(request, key=None):
    """
    Data API

    ---
    request_serializer: serializers.DataResourceSerializer
    """
    # ensure key starts with a / and does not end with one
    if not key.startswith('/'):
        key = '/' + key
    if key.endswith('/'):
        key = key[:-1]

    logger.debug('Got IWC Data request for key {0!s}'.format(key))

    if not hal.validate_version(request.META.get('HTTP_ACCEPT')):
        return Response('Invalid version requested',
                        status=status.HTTP_406_NOT_ACCEPTABLE)

    listing_root_url = hal.get_abs_url_for_iwc(
        request
    )  # flake8: noqa TODO: Is Necessary? - Variable not being used in method
    data = hal.create_base_structure(
        request,  # flake8: noqa TODO: Is Necessary? - Variable not being used in method
        hal.generate_content_type(request.accepted_media_type))

    if request.method == 'PUT':
        try:
            logger.debug('request.data: {0!s}'.format(request.data))
            instance = model_access.get_data_resource(request.user.username,
                                                      key)
            if instance:
                serializer = serializers.DataResourceSerializer(
                    instance,
                    data=request.data,
                    context={
                        'request': request,
                        'key': key
                    },
                    partial=True)
                if not serializer.is_valid():
                    logger.error('{0!s}'.format(serializer.errors))
                    return Response(serializer.errors,
                                    status=status.HTTP_400_BAD_REQUEST)
                serializer.save()
                resp = serializer.data
                resp = hal.add_hal_structure(
                    resp, request,
                    hal.generate_content_type(request.accepted_media_type))
                return Response(resp, status=status.HTTP_200_OK)
            else:
                serializer = serializers.DataResourceSerializer(
                    data=request.data,
                    context={
                        'request': request,
                        'key': key
                    },
                    partial=True)
                if not serializer.is_valid():
                    logger.error('ERROR: {0!s}'.format(serializer.errors))
                    return Response(serializer.errors,
                                    status=status.HTTP_400_BAD_REQUEST)
                serializer.save()
                resp = serializer.data
                resp = hal.add_hal_structure(
                    resp, request,
                    hal.generate_content_type(request.accepted_media_type))
                return Response(resp, status=status.HTTP_201_CREATED)
        except Exception as e:
            # TODO debug
            # raise e
            return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
    if request.method == 'GET':
        try:
            instance = model_access.get_data_resource(request.user.username,
                                                      key)
            if not instance:
                return Response(status=status.HTTP_404_NOT_FOUND)
            serializer = serializers.DataResourceSerializer(instance,
                                                            data=request.data,
                                                            context={
                                                                'request':
                                                                request,
                                                                'key': key
                                                            },
                                                            partial=True)
            if not serializer.is_valid():
                logger.error('{0!s}'.format(serializer.errors))
                return Response(serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
            resp = serializer.data
            resp = hal.add_hal_structure(
                resp, request,
                hal.generate_content_type(request.accepted_media_type))
            return Response(resp, status=status.HTTP_200_OK)
        except Exception as e:
            # TODO debug
            raise e
            return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
    if request.method == 'DELETE':
        try:
            instance = model_access.get_data_resource(request.user.username,
                                                      key)
            if instance:
                instance.delete()
            else:
                return Response(status=status.HTTP_404_NOT_FOUND)
            return Response(status=status.HTTP_204_NO_CONTENT)
        except Exception as e:
            # TODO debug
            raise e
            return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
コード例 #10
0
ファイル: views.py プロジェクト: TensorWrenchOSS/ozp-backend
def DataApiView(request, key=None):
    """
    Data API

    ---
    request_serializer: serializers.DataResourceSerializer
    """
    # ensure key starts with a / and does not end with one
    if not key.startswith('/'):
        key = '/' + key
    if key.endswith('/'):
        key = key[:-1]

    logger.debug('Got IWC Data request for key %s' % key)

    if not hal.validate_version(request.META.get('HTTP_ACCEPT')):
        return Response('Invalid version requested',
            status=status.HTTP_406_NOT_ACCEPTABLE)

    listing_root_url = hal.get_abs_url_for_iwc(request)
    data = hal.create_base_structure(request,
        hal.generate_content_type(
            request.accepted_media_type))

    if request.method == 'PUT':
        try:
            logger.debug('request.data: %s' % request.data)
            instance = model_access.get_data_resource(request.user.username,
                key)
            if instance:
                serializer = serializers.DataResourceSerializer(instance,
                    data=request.data, context={'request': request, 'key': key},
                    partial=True)
                if not serializer.is_valid():
                    logger.error('%s' % serializer.errors)
                    return Response(serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)
                serializer.save()
                resp = serializer.data
                resp = hal.add_hal_structure(resp, request,
                    hal.generate_content_type(
                        request.accepted_media_type))
                return Response(resp, status=status.HTTP_200_OK)
            else:
                serializer = serializers.DataResourceSerializer(
                    data=request.data, context={'request': request, 'key': key},
                    partial=True)
                if not serializer.is_valid():
                    logger.error('ERROR: %s' % serializer.errors)
                    return Response(serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)
                serializer.save()
                resp = serializer.data
                resp = hal.add_hal_structure(resp, request,
                    hal.generate_content_type(
                        request.accepted_media_type))
                return Response(resp, status=status.HTTP_201_CREATED)
        except Exception as e:
            # TODO debug
            # raise e
            return Response(str(e),
                status=status.HTTP_400_BAD_REQUEST)
    if request.method == 'GET':
        try:
            instance = model_access.get_data_resource(request.user.username,
                key)
            if not instance:
                return Response(status=status.HTTP_404_NOT_FOUND)
            serializer = serializers.DataResourceSerializer(instance,
                data=request.data, context={'request': request, 'key': key},
                partial=True)
            if not serializer.is_valid():
                logger.error('%s' % serializer.errors)
                return Response(serializer.errors,
                    status=status.HTTP_400_BAD_REQUEST)
            resp = serializer.data
            resp = hal.add_hal_structure(resp, request,
                hal.generate_content_type(
                    request.accepted_media_type))
            return Response(resp, status=status.HTTP_200_OK)
        except Exception as e:
            # TODO debug
            raise e
            return Response(str(e),
                status=status.HTTP_400_BAD_REQUEST)
    if request.method == 'DELETE':
        try:
            instance = model_access.get_data_resource(request.user.username,
                key)
            if instance:
                instance.delete()
            else:
                return Response(status=status.HTTP_404_NOT_FOUND)
            return Response(status=status.HTTP_204_NO_CONTENT)
        except Exception as e:
            # TODO debug
            raise e
            return Response(str(e),
                status=status.HTTP_400_BAD_REQUEST)