def updateArticle(): if request.method == 'POST': try: data = request.data dataDict = json.loads(data) articleID = dataDict['articleID'] if isinstance(articleID, int): pass elif articleID.isdigit(): pass else: return jsonify(status="articleID Should be Numeric/Integer") if isinstance(dataDict['article_text'], basestring): pass else: return jsonify(status="Enter Article Text as a string value") article_text = escape_string(dataDict['article_text'].strip()) cnx = mysql.connector.connect(user=user, password=password, host=host, database=database) cursor = cnx.cursor() cursor.execute( "select count(*) from articles where articleID = '" + str(articleID) + "' ") count = cursor.fetchall()[0][0] if count < 1: return jsonify(status="Invalid articleID") update = "update articles set full_text = '"+ article_text +"' " \ " where articleID = '"+ str(articleID) +"' " cursor.execute(update) rowsaffected = cursor.rowcount cnx.commit() cnx.close if rowsaffected == 1: status = "Update Successful" else: status = "No Update performed" except: status = error_name() else: status = "Operation failed - use HTTP POST method" return jsonify(status=status)
def updateArticle(): if request.method == 'POST': try: data = request.data dataDict = json.loads(data) articleID = dataDict['articleID'] if isinstance(articleID,int): pass elif articleID.isdigit(): pass else: return jsonify(status = "articleID Should be Numeric/Integer") if isinstance(dataDict['article_text'],basestring): pass else: return jsonify(status = "Enter Article Text as a string value") article_text = escape_string(dataDict['article_text'].strip()) cnx = mysql.connector.connect(user=user, password=password, host=host, database=database) cursor = cnx.cursor() cursor.execute("select count(*) from articles where articleID = '"+ str(articleID) +"' ") count = cursor.fetchall()[0][0] if count < 1 : return jsonify(status = "Invalid articleID") update = "update articles set full_text = '"+ article_text +"' " \ " where articleID = '"+ str(articleID) +"' " cursor.execute(update) rowsaffected = cursor.rowcount cnx.commit() cnx.close if rowsaffected == 1: status = "Update Successful" else: status = "No Update performed" except: status = error_name() else: status = "Operation failed - use HTTP POST method" return jsonify(status = status)
def addArticle(): if request.method == 'POST': try: current_time = time.strftime("%Y-%m-%d %I:%M:%S") data = request.data dataDict = json.loads(data) if isinstance(dataDict['article_text'], basestring): pass else: return jsonify(articleID=None, status="Enter Article Text as a string value") article_text = escape_string(dataDict['article_text'].strip()) cnx = mysql.connector.connect(user=user, password=password, host=host, database=database) cursor = cnx.cursor() insert_query = "INSERT INTO articles (pubDate, full_text)" \ " VALUES('%s', '%s')" % \ (current_time, article_text) cursor.execute(insert_query) articleID = cursor.lastrowid rowsaffected = cursor.rowcount cnx.commit() cnx.close if rowsaffected == 1: status = "Add Successful" else: status = "Add failed" except: articleID = None status = error_name() else: articleID = None status = "Operation failed - use HTTP POST method" return jsonify(articleID=articleID, status=status)
def addArticle(): if request.method == 'POST': try: current_time = time.strftime("%Y-%m-%d %I:%M:%S") data = request.data dataDict = json.loads(data) if isinstance(dataDict['article_text'],basestring): pass else: return jsonify(articleID = None ,status = "Enter Article Text as a string value") article_text = escape_string(dataDict['article_text'].strip()) cnx = mysql.connector.connect(user=user, password=password, host=host, database=database) cursor = cnx.cursor() insert_query = "INSERT INTO articles (pubDate, full_text)" \ " VALUES('%s', '%s')" % \ (current_time, article_text) cursor.execute(insert_query) articleID = cursor.lastrowid rowsaffected = cursor.rowcount cnx.commit() cnx.close if rowsaffected == 1: status = "Add Successful" else: status = "Add failed" except: articleID = None status = error_name() else: articleID = None status = "Operation failed - use HTTP POST method" return jsonify(articleID = articleID,status = status)
def UpdateComments(): if request.method == 'POST': try: data = request.data dataDict = json.loads(data) commentID = dataDict['commentID'] if isinstance(commentID, int): pass elif commentID.isdigit(): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,status = "commentID Should be Numeric/Integer") if isinstance(dataDict['commentBody'], basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,status = "Enter commentBody as a string value") cnx = mysql.connector.connect(user=user, password=password, host=host, database=database) cursor = cnx.cursor() cursor.execute( "select count(*) from comments where commentID = '" + str(commentID) + "' ") count = cursor.fetchall()[0][0] if count < 1: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,status = "Invalid commentID") cursor.execute( "select commentDate,recommendationCount,username,location from comments where commentID = '" + str(commentID) + "'") for row in cursor: commentDate = row[0] recommendationCount = row[1] username = row[2] location = row[3] commentBody = escape_string(dataDict['commentBody'].strip()) if 'commentDate' in dataDict: commentDate = dataDict['commentDate'] try: datetime.datetime.strptime(commentDate, '%Y-%m-%d %H:%M:%S') except: err_msg = "Incorrect date format, Correct format should be YYYY-MM-DD H:M:S" return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0 ,status = err_msg) if 'recommendationCount' in dataDict: recommendationCount = dataDict['recommendationCount'] if isinstance(recommendationCount, int): pass elif recommendationCount.isdigit(): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,status = "recommendationCount Should be Numeric/Integer") recommendationCount = int(recommendationCount) if 'username' in dataDict: username = dataDict['username'] if isinstance(username, basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0, status = "Enter username as a string value") if 'location' in dataDict: location = dataDict['location'] if isinstance(location, basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,status = "Enter Location as a string value") # Call updateComment() function of subroutine - calculate_score to re-calculate all the scores ArticleRelevance, ConversationalRelevance, PersonalXP, Readability, Length = \ updateComment(commentBody,commentID) update = "update comments set ArticleRelevance = '"+ str(ArticleRelevance) +"', " \ "ConversationalRelevance = '"+ str(ConversationalRelevance) +"' , " \ "PersonalXP = '"+ str(PersonalXP) +"', Readability = '"+ str(Readability) +"'," \ "CommentLength = '"+ str(Length) +"', CommentBody = '" + commentBody + "' ," \ "commentDate = '"+ str(commentDate) +"', recommendationCount = '" + str(recommendationCount) + "', " \ "username = '******', location = '" + str(location) + "' " \ " where commentID = '"+ str(commentID) +"' " cursor.execute(update) rowsaffected = cursor.rowcount cnx.commit() cnx.close if rowsaffected == 1: status = "Update Successful" else: status = "No update performed" except: ArticleRelevance = 0.0 ConversationalRelevance = 0.0 PersonalXP = 0.0 Readability = 0.0 Length = 0.0 status = error_name() else: ArticleRelevance = 0.0 ConversationalRelevance = 0.0 PersonalXP = 0.0 Readability = 0.0 Length = 0.0 status = "Update Operation failed - use HTTP POST method" return jsonify(ArticleRelevance = ArticleRelevance, ConversationalRelevance = ConversationalRelevance, \ PersonalXP = PersonalXP, Readability = Readability, Length = Length, status = status)
def AddComment(): if request.method == 'POST': try: data = request.data dataDict = json.loads(data) articleID = dataDict['articleID'] if isinstance(articleID, int): pass elif articleID.isdigit(): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "articleID Should be Numeric/Integer") if isinstance(dataDict['commentBody'], basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "Enter commentBody as a string value") commentBody = escape_string(dataDict['commentBody'].strip()) if 'commentDate' in dataDict: commentDate = dataDict['commentDate'] try: datetime.datetime.strptime(commentDate, '%Y-%m-%d %H:%M:%S') except: err_msg = "Incorrect date format, Correct format should be YYYY-MM-DD H:M:S" return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = err_msg) else: commentDate = time.strftime("%Y-%m-%d %H:%M:%S") if 'recommendationCount' in dataDict: recommendationCount = dataDict['recommendationCount'] if isinstance(recommendationCount, int): pass elif recommendationCount.isdigit(): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "recommendationCount Should be Numeric/Integer") recommendationCount = int(dataDict['recommendationCount']) else: recommendationCount = 0 if 'username' in dataDict: username = dataDict['username'] if isinstance(username, basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "Enter username as a string value") else: username = "" if 'location' in dataDict: location = dataDict['location'] if isinstance(location, basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "Enter Location as a string value") else: location = "" cnx = mysql.connector.connect(user=user, password=password, host=host, database=database) cursor = cnx.cursor() cursor.execute( "select count(*) from articles where articleID = '" + str(articleID) + "' ") count = cursor.fetchall()[0][0] if count < 1: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "Invalid articleID") else: # Call addComment() function of subroutine - calculate_score to calculate all the scores ArticleRelevance, ConversationalRelevance, PersonalXP, Readability, Length = \ addComment(commentBody,articleID) insert_query = "INSERT INTO comments (commentBody, articleID," \ "commentDate,recommendationCount,username,location," \ "ArticleRelevance,ConversationalRelevance, PersonalXP, Readability, CommentLength)" \ "VALUES ('%s','%s','%s','%d','%s','%s','%s','%s','%s','%s','%s')" % \ (commentBody,articleID,commentDate,recommendationCount,username,location, \ str(ArticleRelevance),str(ConversationalRelevance),str(PersonalXP),str(Readability),str(Length)) cursor.execute(insert_query) CommentID = cursor.lastrowid rowsaffected = cursor.rowcount cnx.commit() cnx.close if rowsaffected == 1: status = "Add Successful" else: status = "Add failed" except: ArticleRelevance = 0.0 ConversationalRelevance = 0.0 PersonalXP = 0.0 Readability = 0.0 Length = 0.0 status = error_name() CommentID = None else: ArticleRelevance = 0.0 ConversationalRelevance = 0.0 PersonalXP = 0.0 Readability = 0.0 Length = 0.0 CommentID = None status = "Add operation failed - use HTTP POST method" return jsonify(ArticleRelevance = ArticleRelevance, ConversationalRelevance = ConversationalRelevance, \ PersonalXP = PersonalXP, Readability = Readability, Length = Length, \ commentID = CommentID,status=status)
def UpdateComments(): if request.method == 'POST': try: data = request.data dataDict = json.loads(data) commentID = dataDict['commentID'] if isinstance(commentID,int): pass elif commentID.isdigit(): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,status = "commentID Should be Numeric/Integer") if isinstance(dataDict['commentBody'],basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,status = "Enter commentBody as a string value") cnx = mysql.connector.connect(user=user, password=password, host=host, database=database) cursor = cnx.cursor() cursor.execute("select count(*) from comments where commentID = '"+ str(commentID) +"' ") count = cursor.fetchall()[0][0] if count < 1 : return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,status = "Invalid commentID") cursor.execute("select commentDate,recommendationCount,username,location from comments where commentID = '"+ str(commentID) +"'") for row in cursor: commentDate = row[0] recommendationCount = row[1] username = row[2] location = row[3] commentBody = escape_string(dataDict['commentBody'].strip()) if 'commentDate' in dataDict: commentDate = dataDict['commentDate'] try: datetime.datetime.strptime(commentDate, '%Y-%m-%d %H:%M:%S') except: err_msg = "Incorrect date format, Correct format should be YYYY-MM-DD H:M:S" return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0 ,status = err_msg) if 'recommendationCount' in dataDict: recommendationCount = dataDict['recommendationCount'] if isinstance(recommendationCount,int): pass elif recommendationCount.isdigit(): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,status = "recommendationCount Should be Numeric/Integer") recommendationCount = int(recommendationCount) if 'username' in dataDict: username = dataDict['username'] if isinstance(username,basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0, status = "Enter username as a string value") if 'location' in dataDict: location = dataDict['location'] if isinstance(location,basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,status = "Enter Location as a string value") # Call updateComment() function of subroutine - calculate_score to re-calculate all the scores ArticleRelevance, ConversationalRelevance, PersonalXP, Readability, Length = \ updateComment(commentBody,commentID) update = "update comments set ArticleRelevance = '"+ str(ArticleRelevance) +"', " \ "ConversationalRelevance = '"+ str(ConversationalRelevance) +"' , " \ "PersonalXP = '"+ str(PersonalXP) +"', Readability = '"+ str(Readability) +"'," \ "CommentLength = '"+ str(Length) +"', CommentBody = '" + commentBody + "' ," \ "commentDate = '"+ str(commentDate) +"', recommendationCount = '" + str(recommendationCount) + "', " \ "username = '******', location = '" + str(location) + "' " \ " where commentID = '"+ str(commentID) +"' " cursor.execute(update) rowsaffected = cursor.rowcount cnx.commit() cnx.close if rowsaffected == 1: status = "Update Successful" else: status = "No update performed" except: ArticleRelevance = 0.0 ConversationalRelevance = 0.0 PersonalXP = 0.0 Readability = 0.0 Length = 0.0 status = error_name() else: ArticleRelevance = 0.0 ConversationalRelevance = 0.0 PersonalXP = 0.0 Readability = 0.0 Length = 0.0 status = "Update Operation failed - use HTTP POST method" return jsonify(ArticleRelevance = ArticleRelevance, ConversationalRelevance = ConversationalRelevance, \ PersonalXP = PersonalXP, Readability = Readability, Length = Length, status = status)
def AddComment(): if request.method == 'POST': try: data = request.data dataDict = json.loads(data) articleID = dataDict['articleID'] if isinstance(articleID,int): pass elif articleID.isdigit(): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "articleID Should be Numeric/Integer") if isinstance(dataDict['commentBody'],basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "Enter commentBody as a string value") commentBody = escape_string(dataDict['commentBody'].strip()) if 'commentDate' in dataDict: commentDate = dataDict['commentDate'] try: datetime.datetime.strptime(commentDate, '%Y-%m-%d %H:%M:%S') except: err_msg = "Incorrect date format, Correct format should be YYYY-MM-DD H:M:S" return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = err_msg) else: commentDate = time.strftime("%Y-%m-%d %H:%M:%S") if 'recommendationCount' in dataDict: recommendationCount = dataDict['recommendationCount'] if isinstance(recommendationCount,int): pass elif recommendationCount.isdigit(): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "recommendationCount Should be Numeric/Integer") recommendationCount = int(dataDict['recommendationCount']) else: recommendationCount = 0 if 'username' in dataDict: username = dataDict['username'] if isinstance(username,basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "Enter username as a string value") else: username = "" if 'location' in dataDict: location = dataDict['location'] if isinstance(location,basestring): pass else: return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "Enter Location as a string value") else: location = "" cnx = mysql.connector.connect(user=user, password=password, host=host, database=database) cursor = cnx.cursor() cursor.execute("select count(*) from articles where articleID = '"+ str(articleID) +"' ") count = cursor.fetchall()[0][0] if count < 1 : return jsonify(ArticleRelevance = 0.0, ConversationalRelevance = 0.0 , PersonalXP = 0.0 , \ Readability = 0.0, Length = 0.0,commentID = None ,status = "Invalid articleID") else: # Call addComment() function of subroutine - calculate_score to calculate all the scores ArticleRelevance, ConversationalRelevance, PersonalXP, Readability, Length = \ addComment(commentBody,articleID) insert_query = "INSERT INTO comments (commentBody, articleID," \ "commentDate,recommendationCount,username,location," \ "ArticleRelevance,ConversationalRelevance, PersonalXP, Readability, CommentLength)" \ "VALUES ('%s','%s','%s','%d','%s','%s','%s','%s','%s','%s','%s')" % \ (commentBody,articleID,commentDate,recommendationCount,username,location, \ str(ArticleRelevance),str(ConversationalRelevance),str(PersonalXP),str(Readability),str(Length)) cursor.execute(insert_query) CommentID = cursor.lastrowid rowsaffected = cursor.rowcount cnx.commit() cnx.close if rowsaffected == 1: status = "Add Successful" else: status = "Add failed" except: ArticleRelevance = 0.0 ConversationalRelevance = 0.0 PersonalXP = 0.0 Readability = 0.0 Length = 0.0 status = error_name() CommentID = None else: ArticleRelevance = 0.0 ConversationalRelevance = 0.0 PersonalXP = 0.0 Readability = 0.0 Length = 0.0 CommentID = None status = "Add operation failed - use HTTP POST method" return jsonify(ArticleRelevance = ArticleRelevance, ConversationalRelevance = ConversationalRelevance, \ PersonalXP = PersonalXP, Readability = Readability, Length = Length, \ commentID = CommentID,status=status)