def get_post_info_params(cursor, post_info, related_list): user_info = post_info[4] thread_info = post_info[2] forum_info = post_info[5] for related in related_list: if related == 'user': user_info = get_user_info_external(cursor, post_info[4]) elif related == 'thread': thread_info = get_thread_info_external(cursor, post_info[2]) elif related == 'forum': forum_info = get_forum_info_external(cursor, post_info[5]) resp = { "date": datetime.datetime.strftime(post_info[1], "%Y-%m-%d %H:%M:%S"), "dislikes": post_info[13], "forum": forum_info, "id": post_info[0], "isApproved": true_false_ret(post_info[7]), "isDeleted": true_false_ret(post_info[11]), "isEdited": true_false_ret(post_info[9]), "isHighlighted": true_false_ret(post_info[8]), "isSpam": true_false_ret(post_info[10]), "likes": post_info[12], "message": post_info[3], "parent": zero_check(post_info[6]), "points": post_info[14], "thread": thread_info, "user": user_info } return resp
def thread_details(): conn = mysql.get_db() cursor = conn.cursor() if not request.args.get('thread'): return jsonify(code=3, response="Wrong request") thread_id = request.args.get('thread') try: cursor.execute("SELECT * FROM Thread WHERE id='%s'" % thread_id) except Exception: return jsonify(code=3, response="Wrong request") thread = cursor.fetchall() if not thread: return jsonify(code=3, response="No such thread") thread_info = thread[0] forum_info = thread_info[1] user_info = thread_info[4] related_list = request.args.getlist('related') for related in related_list: if related == 'user': user_info = get_user_info_external(cursor, thread_info[4]) elif related == 'forum': forum_info = get_forum_info_external(cursor, thread_info[1]) elif related == 'thread': return jsonify(code=3, response="Wrong request") num_posts = count_posts_in_thread(cursor, thread_id) if true_false_ret(thread_info[8]): num_posts = 0 resp = { "id": thread_info[0], "forum": forum_info, "title": thread_info[2], "isClosed": true_false_ret(thread_info[3]), "user": user_info, "date": datetime.datetime.strftime(thread_info[5], "%Y-%m-%d %H:%M:%S"), "message": thread_info[6], "slug": thread_info[7], "isDeleted": true_false_ret(thread_info[8]), "likes": thread_info[9], "dislikes": thread_info[10], "points": thread_info[11], "posts": num_posts } return jsonify(code=0, response=resp)
def post_details(): conn = mysql.get_db() cursor = conn.cursor() post_id = request.args.get('post') if not post_id: return jsonify(code=3, response="Wrong request") if int(post_id) < 0: cursor.execute("SELECT max(id) FROM Post") info = cursor.fetchall() if info: post_id = int(info[0][0]) + int(post_id) + 1 try: cursor.execute("SELECT * FROM Post WHERE id='%s'" % post_id) except Exception: return jsonify(code=3, response="Wrong request") post = cursor.fetchall() if not post: return jsonify(code=1, response="User doesn't exist") post_info = post[0] thread_info = post_info[2] user_info = post_info[4] forum_info = post_info[5] related = request.args.getlist('related') if 'user' in related: user_info = get_user_info_external(cursor, post_info[4]) if 'forum' in related: forum_info = get_forum_info_external(cursor, post_info[5]) if 'thread' in related: thread_info = get_thread_info_external(cursor, post_info[2]) resp = { "date": datetime.datetime.strftime(post_info[1], "%Y-%m-%d %H:%M:%S"), "dislikes": post_info[13], "forum": forum_info, "id": post_info[0], "isApproved": true_false_ret(post_info[7]), "isDeleted": true_false_ret(post_info[11]), "isEdited": true_false_ret(post_info[9]), "isHighlighted": true_false_ret(post_info[8]), "isSpam": true_false_ret(post_info[10]), "likes": post_info[12], "message": post_info[3], "parent": zero_check(post_info[6]), "points": post_info[14], "thread": thread_info, "user": user_info } return jsonify(code=0, response=resp)
def get_post_info(post_info): resp = { "date": datetime.datetime.strftime(post_info[1], "%Y-%m-%d %H:%M:%S"), "dislikes": post_info[13], "forum": post_info[5], "id": post_info[0], "isApproved": true_false_ret(post_info[7]), "isDeleted": true_false_ret(post_info[11]), "isEdited": true_false_ret(post_info[9]), "isHighlighted": true_false_ret(post_info[8]), "isSpam": true_false_ret(post_info[10]), "likes": post_info[12], "message": post_info[3], "parent": zero_check(post_info[6]), "points": post_info[14], "thread": post_info[2], "user": post_info[4] } return resp
def get_all_user_info(cursor, user): cursor.execute("SELECT * FROM User where email='%s'" % user) usr_info = cursor.fetchall() if not usr_info: return jsonify(code=1, response="No such user") all_fetched_followers = get_followers(cursor, user) all_fetched_followees = get_followees(cursor, user) all_fetched_subscr = get_subscriptions(cursor, user) resp = { "id": usr_info[0][0], "email": usr_info[0][1], "about": usr_info[0][2], "isAnonymous": true_false_ret(usr_info[0][3]), "name": empty_check(usr_info[0][4]), "username": empty_check(usr_info[0][5]), "followers": all_fetched_followers, "following": all_fetched_followees, "subscriptions": all_fetched_subscr } return jsonify(code=0, response=resp)
def post_create(): conn = mysql.get_db() cursor = conn.cursor() try: req_json = request.get_json() except BadRequest: return jsonify(code=2, response="Cant parse json") if not ('date' in req_json and 'thread' in req_json and 'message' in req_json and 'user' in req_json and 'forum' in req_json): return jsonify(code=3, response="Wrong request") new_post_date = req_json['date'] new_post_thread_id = req_json['thread'] new_post_message = req_json['message'] new_post_user_email = req_json['user'] new_post_forum_short_name = req_json['forum'] new_post_is_approved = 0 new_post_is_high = 0 new_post_is_edited = 0 new_post_is_spam = 0 new_post_is_del = 0 new_post_parent_id = 0 if 'parent' in req_json and req_json['parent'] is not None: new_post_parent_id = req_json['parent'] new_post_path = get_post_path(cursor, new_post_parent_id) if 'isApproved' in req_json and req_json['isApproved']: new_post_is_approved = 1 if 'isHighlighted' in req_json and req_json['isHighlighted']: new_post_is_high = 1 if 'isEdited' in req_json and req_json['isEdited']: new_post_is_edited = 1 if 'isSpam' in req_json and req_json['isSpam']: new_post_is_spam = 1 if 'isDeleted' in req_json and req_json['isDeleted']: new_post_is_del = 1 sql_data = (new_post_date, new_post_thread_id, new_post_message, new_post_user_email, new_post_forum_short_name, new_post_parent_id, new_post_is_approved, new_post_is_high, new_post_is_edited, new_post_is_spam, new_post_is_del, new_post_path) try: cursor.execute( "INSERT INTO Post VALUES (null,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,0,0,0,%s)", sql_data) conn.commit() except Exception: return jsonify(code=3, response="Wrong request") resp = { "date": new_post_date, "forum": new_post_forum_short_name, "id": cursor.lastrowid, "isApproved": true_false_ret(new_post_is_approved), "isDeleted": true_false_ret(new_post_is_del), "isEdited": true_false_ret(new_post_is_edited), "isHighlighted": true_false_ret(new_post_is_high), "isSpam": true_false_ret(new_post_is_spam), "message": new_post_message, "parent": zero_check(new_post_parent_id), "thread": new_post_thread_id, "user": new_post_user_email, "likes": 0, "dislikes": 0, "points": 0 } return jsonify(code=0, response=resp)
def post_create(): conn = mysql.get_db() cursor = conn.cursor() try: req_json = request.get_json() except BadRequest: return jsonify(code=2, response="Cant parse json") if not ('date' in req_json and 'thread' in req_json and 'message' in req_json and 'user' in req_json and 'forum' in req_json): return jsonify(code=3, response="Wrong request") new_post_date = req_json['date'] new_post_thread_id = req_json['thread'] new_post_message = req_json['message'] new_post_user_email = req_json['user'] new_post_forum_short_name = req_json['forum'] new_post_is_approved = 0 new_post_is_high = 0 new_post_is_edited = 0 new_post_is_spam = 0 new_post_is_del = 0 new_post_parent_id = 0 if 'parent' in req_json and req_json['parent'] is not None: new_post_parent_id = req_json['parent'] new_post_path = get_post_path(cursor, new_post_parent_id) if 'isApproved' in req_json and req_json['isApproved']: new_post_is_approved = 1 if 'isHighlighted' in req_json and req_json['isHighlighted']: new_post_is_high = 1 if 'isEdited' in req_json and req_json['isEdited']: new_post_is_edited = 1 if 'isSpam' in req_json and req_json['isSpam']: new_post_is_spam = 1 if 'isDeleted' in req_json and req_json['isDeleted']: new_post_is_del = 1 sql_data = (new_post_date, new_post_thread_id, new_post_message, new_post_user_email, new_post_forum_short_name, new_post_parent_id, new_post_is_approved, new_post_is_high, new_post_is_edited, new_post_is_spam, new_post_is_del, new_post_path) try: cursor.execute("INSERT INTO Post VALUES (null,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,0,0,0,%s)", sql_data) conn.commit() except Exception: return jsonify(code=3, response="Wrong request") resp = { "date": new_post_date, "forum": new_post_forum_short_name, "id": cursor.lastrowid, "isApproved": true_false_ret(new_post_is_approved), "isDeleted": true_false_ret(new_post_is_del), "isEdited": true_false_ret(new_post_is_edited), "isHighlighted": true_false_ret(new_post_is_high), "isSpam": true_false_ret(new_post_is_spam), "message": new_post_message, "parent": zero_check(new_post_parent_id), "thread": new_post_thread_id, "user": new_post_user_email, "likes": 0, "dislikes": 0, "points": 0 } return jsonify(code=0, response=resp)