예제 #1
0
    def put(self, request, tablature_id):
        '''
        Modify tablature.
        Requires authorization.
        '''
    
        #request.DATA contains the request body already deserialized in a
        #python dictionary

        if not request.DATA:
            
            error = ErrorModel('The artist_id, song_id and body of the tablature\
                               cannot be empty').serialize()
            return Response(error, status=status.HTTP_400_BAD_REQUEST)
        if not database.contains_tablature(tablature_id):
            
            error = ErrorModel("The tablature "+ tablature_id+
                               " is not in the archive").serialize()
            return Response(error, status=status.HTTP_404_NOT_FOUND)
        
        
        #request.DATA contains a dictionary with the entity body input.
        #Deserialize the data and modify the model
        tablaturemodel = TablatureModel(tablature_id)
        try:
            #EXTRACT THE PRIVATE DATA
            if request.DATA.has_key("body"):
                body = request.DATA['body']
            else:
                body = None
            if request.DATA.has_key("artist_id"):
                artist_id = request.DATA["artist_id"]
            else:
                artist_id = None
            if request.DATA.has_key("song_id"):
                song_id = request.DATA['song_id']
            else:
                song_id = None
            #SET VALUES TO USER
            tablaturemodel.tablature_id = tablature_id
            tablaturemodel.body = body
            tablaturemodel.artist_id = artist_id
            tablaturemodel.song_id = song_id
        except Exception as e:
            print "Could not add the data "+ str(e)
            traceback.print_exc()
            return Response(status = 400)
            
        authorization = ''
        try:
            authorization = request.user.username #request.META["HTTP_AUTHORIZATION"]
            try:
                if sys.argv[1] == "test":
                    authorization = request.META["HTTP_AUTHORIZATION"]
            except IndexError:
                pass
        except KeyError:
            pass
        if self._modifyisauthorized(tablature_id, authorization):
            return self._modifytablature(tablaturemodel, request) 
        else:
            return Response(status=status.HTTP_401_UNAUTHORIZED)