def get_tablaturecomments(self, tablature_id): """ Get comments to a tablature """ keys_on = "PRAGMA foreign_keys = ON" query = "SELECT * FROM comments WHERE tablature_id = ?" pvalue = (tablature_id,) if not self.contains_tablature(tablature_id): return None con = sqlite3.connect(self.database_name) with con: con.row_factory = sqlite3.Row cur = con.cursor() cur.execute(keys_on) cur.execute(query, pvalue) rows = cur.fetchall() if rows == []: return None comments = [] for row in rows: comments.append(CommentModel.create(row)) return comments
def get_comment(self, comment_id): """ Returns the comment. If comment doesn't exist return "None". """ keys_on = "PRAGMA foreign_keys = ON" query = "SELECT * FROM comments WHERE comment_id = ?" pvalue = (comment_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 else: # print row.keys() return CommentModel.create(row)
def get_comments_by_user(self, user_nickname): """ Returns comments. If comments doesn't exist return "None". """ keys_on = "PRAGMA foreign_keys = ON" query = "SELECT * FROM comments WHERE user_nickname = ?" pvalue = (user_nickname,) # 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 rows = cur.fetchall() comments = [] if rows is None: return None else: # print row.keys() for row in rows: comments.appdend(CommentModel.create(row)) return comments
def get_tablaturecomments(self, tablature_id): ''' Get comments to a tablature ''' keys_on = 'PRAGMA foreign_keys = ON' query = 'SELECT * FROM comments WHERE tablature_id = ?' pvalue = (tablature_id, ) if not self.contains_tablature(tablature_id): return None con = sqlite3.connect(self.database_name) with con: con.row_factory = sqlite3.Row cur = con.cursor() cur.execute(keys_on) cur.execute(query, pvalue) rows = cur.fetchall() if rows == []: return None comments = [] for row in rows: comments.append(CommentModel.create(row)) return comments
def get_comments_by_user(self, user_nickname): ''' Returns comments. If comments doesn't exist return "None". ''' keys_on = 'PRAGMA foreign_keys = ON' query = 'SELECT * FROM comments WHERE user_nickname = ?' pvalue = (user_nickname, ) #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 rows = cur.fetchall() comments = [] if rows is None: return None else: #print row.keys() for row in rows: comments.appdend(CommentModel.create(row)) return comments
def get_comment(self, comment_id): ''' Returns the comment. If comment doesn't exist return "None". ''' keys_on = 'PRAGMA foreign_keys = ON' query = 'SELECT * FROM comments WHERE comment_id = ?' pvalue = (comment_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 else: #print row.keys() return CommentModel.create(row)
def post(self, request, tablature_id): ''' Post a comment to tablature by id. Requires authorization. Returns 400 on bad request. ''' 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) if not database.contains_tablature(tablature_id): error = ErrorModel('Tablature was not found.').serialize() return Response(error, status=status.HTTP_404_NOT_FOUND) commentmodel = None try: commentmodel = CommentModel(None, raw_data=request.DATA) user_nickname = commentmodel.user_nickname commentmodel.tablature_id = tablature_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._isauthorized(user_nickname, authorization): return self._createcomment(commentmodel, request) else: return Response(status=status.HTTP_401_UNAUTHORIZED)
def post(self, request, tablature_id, comment_id): ''' Post reply to an existing comment. ''' #request.DATA contains the request body already deserialized in #a python dictionary if not request.DATA: error = ErrorModel('The body of the comment\ cannot be empty').serialize() return Response(error, status=status.HTTP_400_BAD_REQUEST) if not database.contains_comment(comment_id): error = ErrorModel("The comment "+comment_id+ " is not in the archive").serialize() return Response(error, status=status.HTTP_404_NOT_FOUND) commentmodel = None try: commentmodel = CommentModel('', raw_data=request.DATA) except Exception as e: print "Could not add the data "+ str(e) traceback.print_exc() return Response(status = 400) commentmodel.reply_to = comment_id commentmodel.tablature_id = tablature_id 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(commentmodel.user_nickname, authorization): return self._createreply(commentmodel, request) else: return Response(status=status.HTTP_401_UNAUTHORIZED)