示例#1
0
文件: views.py 项目: crs4/ACTIVE
    def put(self, request, pk, format=None):
        """
        Method used to update the audio item information providing
        serialized fresh data.

        @param request: HttpRequest object containing the updated AudioItem fields.
        @type request: HttpRequest
        @param pk: Audio's id.
        @type pk: int
        @param format: Format used for data serialization.
        @type format: string
        @return: HttpResponse containing the uploaded audio item data, error if it doesn't exists.
        @rtype: HttpResponse
        """
        with edit_lock:
            item = self.get_object(pk, request.user)
            serializer = AudioItemSerializer(item, data=request.data, partial=True)
            if serializer.is_valid():
                serializer.save()
                logger.debug('Updated data on AudioItem with id ' + str(pk))
                return Response(serializer.data)
            logger.error('Error on update for AudioItem with id ' + str(pk))
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例#2
0
文件: views.py 项目: crs4/ACTIVE
    def post(self, request, format=None):
        """
        Method used create a new AudioItem object.
        All data is provided in a JSON format (serialized) an then is
        converted in an object that will be saved in the database.

        @param request: HttpRequest object containing all AudioItem data.
        @type request: HttpRequest
        @param format: Format used for data serialization.
        @type format: string
        @return: HttpResponse containing the id of the new AudioItem object or an error.
        @rtype: HttpResponse
        """
	request.data['owner'] = request.user.id
        serializer = AudioItemSerializer(data=request.data)
        if request.FILES and request.FILES['file'].content_type.split('/')[0] != 'audio' :
            logger.error('Error on AudioItem creation: format not supported')
            return Response('Content type not supported', status=status.HTTP_400_BAD_REQUEST)
        if serializer.is_valid():
            serializer.save()
            logger.debug('Created a new AudioItem object')
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        logger.error('Error on AudioItem creation')
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)