Exemplo n.º 1
0
 def post(self, request):
     '''
     Post a tablature.
     Requires authorized user.
     '''
     if not request.DATA:
         error = ErrorModel('The artist_id, song_id and the body of the tablature\
                            cannot be empty').serialize()
         return Response(error, status=status.HTTP_400_BAD_REQUEST)
     tablaturemodel = None
     try:
         tablaturemodel = TablatureModel(None, raw_data=request.DATA)
         user_nickname = tablaturemodel.user_nickname
     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._isauthorized(user_nickname, authorization):
         return self._createtablature(tablaturemodel, request) 
     else:
         return Response(status=status.HTTP_401_UNAUTHORIZED)
Exemplo n.º 2
0
    def get_tablatures(self, artist_id, song_id):
        '''
        Return list of tablatures to specified song.
        If parameters are left empty return all tablatures.
        If song parameter is left empty return all tablatures by artist.
        Return "None" if song, artist or tablatures are not found.
        '''

        keys_on = 'PRAGMA foreign_keys = ON'

        if song_id == '' and artist_id == '':
            query = 'SELECT * FROM tablatures'
        elif song_id == '':
            query = 'SELECT * FROM tablatures WHERE artist_id = ?'
            pvalue = (artist_id, )
        elif artist_id == '':
            query = 'SELECT * FROM tablatures WHERE song_id = ?'
            pvalue = (song_id, )
        else:
            query = 'SELECT * FROM tablatures WHERE artist_id = ? AND song_id = ?'
            pvalue = (
                artist_id,
                song_id,
            )

        #artist_id = None

        #connects (and creates if necessary) to the database. gets a connection object
        con = sqlite3.connect(self.database_name)
        with con:
            con.row_factory = sqlite3.Row
            cur = con.cursor()
            cur.execute(keys_on)
            #execute the statement
            if song_id == '' and artist_id == '':
                cur.execute(query)
            else:
                cur.execute(query, pvalue)
            #get results
            rows = cur.fetchall()
            if rows == []:
                return None

            tablatures = []
            for row in rows:
                tablatures.append(TablatureModel.create(row))
            return tablatures
Exemplo n.º 3
0
    def post(self, request, artist_id, song_id):
        '''
        Add a new tablature.
        Returns 400 on bad request.
        Returns 401 on unauthorized user.
        Returns 201 on successful creation.
        '''

        
        if not request.DATA:
            error = ErrorModel('The body of the tablature\
                               cannot be empty').serialize()
            return Response(error, status=status.HTTP_400_BAD_REQUEST)
        tablaturemodel = None
        try:
            if not request.DATA.has_key("artist_id"):
                request.DATA["artist_id"] = artist_id
            if not request.DATA.has_key("song_id"):
                request.DATA["song_id"] = song_id
            tablaturemodel = TablatureModel(None, raw_data=request.DATA)
            user_nickname = tablaturemodel.user_nickname
            if (user_nickname == None):
                error = ErrorModel('Nickname of user cannot be None.').serialize()
                return Response(status=status.HTTP_400_BAD_REQUEST)
        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._isauthorized(user_nickname, authorization):
            return self._createtablature(tablaturemodel, request) 
        else:
            return Response(status=status.HTTP_401_UNAUTHORIZED)
Exemplo n.º 4
0
    def get_tablatures(self, artist_id, song_id):
        """
        Return list of tablatures to specified song.
        If parameters are left empty return all tablatures.
        If song parameter is left empty return all tablatures by artist.
        Return "None" if song, artist or tablatures are not found.
        """

        keys_on = "PRAGMA foreign_keys = ON"

        if song_id == "" and artist_id == "":
            query = "SELECT * FROM tablatures"
        elif song_id == "":
            query = "SELECT * FROM tablatures WHERE artist_id = ?"
            pvalue = (artist_id,)
        elif artist_id == "":
            query = "SELECT * FROM tablatures WHERE song_id = ?"
            pvalue = (song_id,)
        else:
            query = "SELECT * FROM tablatures WHERE artist_id = ? AND song_id = ?"
            pvalue = (artist_id, song_id)

        # artist_id = None

        # connects (and creates if necessary) to the database. gets a connection object
        con = sqlite3.connect(self.database_name)
        with con:
            con.row_factory = sqlite3.Row
            cur = con.cursor()
            cur.execute(keys_on)
            # execute the statement
            if song_id == "" and artist_id == "":
                cur.execute(query)
            else:
                cur.execute(query, pvalue)
            # get results
            rows = cur.fetchall()
            if rows == []:
                return None

            tablatures = []
            for row in rows:
                tablatures.append(TablatureModel.create(row))
            return tablatures
Exemplo n.º 5
0
    def get_tablature(self, tablature_id):
        """
        Return a tablature.
        Return "None" if tablature was not found.
        """

        keys_on = "PRAGMA foreign_keys = ON"
        query = "SELECT * FROM tablatures WHERE tablature_id = ?"
        pvalue = (tablature_id,)
        # connects (and creates if necessary) to the database. gets a connection object
        con = sqlite3.connect(self.database_name)
        with con:
            con.row_factory = sqlite3.Row
            cur = con.cursor()
            cur.execute(keys_on)
            # execute the statement
            cur.execute(query, pvalue)
            # just one result possible
            row = cur.fetchone()
            if row is None:
                return None
            return TablatureModel.create(row)
Exemplo n.º 6
0
    def get_tablature(self, tablature_id):
        '''
        Return a tablature.
        Return "None" if tablature was not found.
        '''

        keys_on = 'PRAGMA foreign_keys = ON'
        query = 'SELECT * FROM tablatures WHERE tablature_id = ?'
        pvalue = (tablature_id, )
        #connects (and creates if necessary) to the database. gets a connection object
        con = sqlite3.connect(self.database_name)
        with con:
            con.row_factory = sqlite3.Row
            cur = con.cursor()
            cur.execute(keys_on)
            #execute the statement
            cur.execute(query, pvalue)
            #just one result possible
            row = cur.fetchone()
            if row is None:
                return None
            return TablatureModel.create(row)
Exemplo n.º 7
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)