Exemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        if request.version != 'v5':
            raise Http404()

        file_name = None
        file_content = None

        # Check whether AJAX or a standard encoded form was used
        if request.is_ajax():

            # File name must be provided by an HTTP header
            if 'HTTP_X_FILE_NAME' not in request.META:
                return Response('Missing HTTP header for file name.',
                                status=status.HTTP_400_BAD_REQUEST)
            file_name = request.META['HTTP_X_FILE_NAME']
            file_content = request
        else:

            # File content must be already processed by the request
            if len(request.data) != 1:
                return Response('Missing embedded file content.',
                                status=status.HTTP_400_BAD_REQUEST)
            file_handle = request.data.values()[0]
            file_name = file_handle.name
            file_content = file_handle

        # Make sure the file type is supported
        if not file_name or not file_content:
            return Response('Missing file attachment.',
                            status=status.HTTP_400_BAD_REQUEST)
        if os.path.splitext(file_name)[1] not in ['.json']:
            return Response('Unsupported file type.',
                            status=status.HTTP_400_BAD_REQUEST)

        # Attempt to parse the file content
        # TODO: Add buffer overflow protection
        import_dict = json.loads(file_content.read())

        # Attempt to apply the configuration
        try:
            warnings = importer.import_config(import_dict)
        except InvalidConfiguration as ex:
            logger.exception('Unable to import configuration.')
            raise BadParameter(unicode(ex))

        results = [{'id': w.key, 'details': w.details} for w in warnings]
        return Response({'warnings': results})
Exemplo n.º 2
0
    def post(self, request):
        """Imports job and recipe configuration and updates the corresponding models.

        :param request: the HTTP POST request
        :type request: :class:`rest_framework.request.Request`
        :rtype: :class:`rest_framework.response.Response`
        :returns: the HTTP response to send back to the user
        """
        import_dict = rest_util.parse_dict(request, 'import')

        try:
            warnings = importer.import_config(import_dict)
        except InvalidConfiguration as ex:
            logger.exception('Unable to import configuration.')
            raise BadParameter(unicode(ex))

        results = [{'id': w.key, 'details': w.details} for w in warnings]
        return Response({'warnings': results})
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        file_name = None
        file_content = None

        # Check whether AJAX or a standard encoded form was used
        if request.is_ajax():

            # File name must be provided by an HTTP header
            if 'HTTP_X_FILE_NAME' not in request.META:
                return Response('Missing HTTP header for file name.', status=status.HTTP_400_BAD_REQUEST)
            file_name = request.META['HTTP_X_FILE_NAME']
            file_content = request
        else:

            # File content must be already processed by the request
            if len(request.data) != 1:
                return Response('Missing embedded file content.', status=status.HTTP_400_BAD_REQUEST)
            file_handle = request.data.values()[0]
            file_name = file_handle.name
            file_content = file_handle

        # Make sure the file type is supported
        if not file_name or not file_content:
            return Response('Missing file attachment.', status=status.HTTP_400_BAD_REQUEST)
        if os.path.splitext(file_name)[1] not in ['.json']:
            return Response('Unsupported file type.', status=status.HTTP_400_BAD_REQUEST)

        # Attempt to parse the file content
        # TODO: Add buffer overflow protection
        import_dict = json.loads(file_content.read())

        # Attempt to apply the configuration
        try:
            warnings = importer.import_config(import_dict)
        except InvalidConfiguration as ex:
            logger.exception('Unable to import configuration.')
            raise BadParameter(unicode(ex))

        results = [{'id': w.key, 'details': w.details} for w in warnings]
        return Response({'warnings': results})