def list_folowings(cursor, limit, order, since_id, user_email, is_follower): cursor.execute("SELECT * FROM User where email='%s'" % user_email) usr_info = cursor.fetchall() if not usr_info: return jsonify(code=1, response="No such list") if is_follower: query = "SELECT F.follower FROM Following F INNER JOIN User U ON F.follower = U.email"\ " WHERE U.id >= %s AND F.followee= '%s' ORDER BY F.follower %s %s " % (since_id, user_email, order, limit) cursor.execute(query) all_followers = cursor.fetchall() all_fetched_followers = [] # all = all_followers[0] for x in all_followers: all_fetched_followers.append(get_user_info_external(cursor, x)) else: query = "SELECT F.followee FROM Following F INNER JOIN User U ON F.followee = U.email"\ " WHERE U.id >= %s AND F.follower= '%s' ORDER BY F.followee %s %s " % (since_id, user_email, order, limit) cursor.execute(query) all_followers = cursor.fetchall() all_fetched_followers = [] for x in all_followers: all_fetched_followers.append(get_user_info_external(cursor, x[0])) return jsonify(code=0, response=all_fetched_followers)
def forum_details(): conn = mysql.get_db() cursor = conn.cursor() forum_short_name = request.args.get('forum') if not forum_short_name: return jsonify(code=3, response="Wrong request") try: cursor.execute("SELECT * FROM Forum WHERE short_name='%s'" % forum_short_name) except Exception: return jsonify(code=3, response="Wrong request") forum = cursor.fetchall() if not forum: return jsonify(code=1, response="No such thread") forum_info = forum[0] forum_id = forum_info[0] forum_name = forum_info[1] forum_short_name = forum_info[2] user_email = forum_info[3] if request.args.get('related') == 'user': user_info = get_user_info_external(cursor, user_email) else: user_info = user_email resp = { "id": forum_id, "name": forum_name, "short_name": forum_short_name, "user": user_info } return jsonify(code=0, response=resp)
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)