def details(): qs = get_json(request) thread_id = qs.get('thread') if not thread_id: return json.dumps({"code": 2, "response": "No 'thread' key"}, indent=4) thread = get_thread_by_id(thread_id) if thread == list(): return json.dumps({"code": 1, "response": "Empty set"}, indent=4) related_values = list() qs_related = qs.get('related') if type(qs_related) is list: related_values.extend(qs_related) elif type(qs_related) is str: related_values.append(qs_related) forum_related = False user_related = False for related_value in related_values: if related_value == 'forum': forum_related = True elif related_value == 'user': user_related = True else: return json.dumps({"code": 3, "response": "Wrong related value"}, indent=4) if forum_related: thread['forum'] = get_forum_dict(short_name=thread['forum']) if user_related: thread['user'] = get_user_dict(thread['user']) return json.dumps({"code": 0, "response": thread}, indent=4)
def update_profile(): request_body = request.json about = try_encode(request_body.get('about')) email = try_encode(request_body.get('user')) name = try_encode(request_body.get('name')) args = {'about': about, 'name': name, 'email': email} db.execute("""UPDATE User SET about = %(about)s, name = %(name)s WHERE email = %(email)s;""", args, True) return json.dumps({"code": 0, "response": get_user_dict(email)}, indent=4)
def list_posts(): qs = get_json(request) forum = qs.get('forum') if not forum: return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4) # Related part related_values = list() qs_related = qs.get('related') if type(qs_related) is list: related_values.extend(qs_related) elif type(qs_related) is str: related_values.append(qs_related) thread_related = False forum_related = False user_related = False for related_value in related_values: if related_value == 'thread': thread_related = True elif related_value == 'forum': forum_related = True elif related_value == 'user': user_related = True else: return json.dumps({ "code": 3, "response": "Wrong related value" }, indent=4) since = qs.get('since', '') limit = qs.get('limit', -1) sort = qs.get('sort', 'flat') order = qs.get('order', 'desc') post_list = get_post_list(forum=forum, since=since, limit=limit, sort=sort, order=order) for post in post_list: if user_related: post['user'] = get_user_dict(post['user']) if thread_related: post['thread'] = get_thread_by_id(post['thread']) if forum_related: post['forum'] = get_forum_dict(short_name=post['forum']) return json.dumps({"code": 0, "response": post_list}, indent=4)
def update_profile(): request_body = request.json about = try_encode(request_body.get('about')) email = try_encode(request_body.get('user')) name = try_encode(request_body.get('name')) args = {'about': about, 'name': name, 'email': email} db.execute( """UPDATE User SET about = %(about)s, name = %(name)s WHERE email = %(email)s;""", args, True) return json.dumps({"code": 0, "response": get_user_dict(email)}, indent=4)
def details(): qs = get_json(request) email = qs.get('user') if not email: return json.dumps({"code": 2, "response": "No 'user' key"}, indent=4) user = get_user_dict(email) user['followers'] = get_followers_list(email) user['following'] = get_following_list(email) user['subscriptions'] = get_subscribed_threads_list(email) return json.dumps({"code": 0, "response": user}, indent=4)
def follow_method(do_unfollow): request_body = request.json follower = request_body.get('follower') followee = request_body.get('followee') args = {'follower': follower, 'following': followee} if not do_unfollow: db.execute("""INSERT INTO Follower (follower, following) VALUES (%(follower)s, %(following)s);""", args, True) else: db.execute("""DELETE FROM Follower WHERE follower = %(follower)s AND following = %(following)s;""", args, True) return json.dumps({"code": 0, "response": get_user_dict(follower)}, indent=4)
def details(): qs = get_json(request) short_name = qs.get('forum') if not short_name: return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4) forum_dict = get_forum_dict(short_name=short_name) if not forum_dict: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) if qs.get('related', '') == 'user': forum_dict['user'] = get_user_dict(forum_dict['user']) return json.dumps({"code": 0, "response": forum_dict}, indent=4)
def list_threads(): qs = get_json(request) forum = qs.get('forum') if not forum: return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4) since = qs.get('since', '') order = qs.get('order', '') limit = qs.get('limit', -1) thread_list = get_thread_list(forum=forum, since=since, order=order, limit=limit) # Related part related_values = list() qs_related = qs.get('related') if type(qs_related) is list: related_values.extend(qs_related) elif type(qs_related) is str: related_values.append(qs_related) forum_related = False user_related = False for related_value in related_values: if related_value == 'forum': forum_related = True elif related_value == 'user': user_related = True else: return json.dumps({ "code": 3, "response": "Wrong related value" }, indent=4) for thread in thread_list: if user_related: thread['user'] = get_user_dict(thread['user']) thread['user']['subscriptions'] = get_subscribed_threads_list( thread['user']['email']) if forum_related: thread['forum'] = get_forum_dict(short_name=thread['forum']) return json.dumps({"code": 0, "response": thread_list}, indent=4)
def list_posts(): qs = get_json(request) forum = qs.get('forum') if not forum: return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4) # Related part related_values = list() qs_related = qs.get('related') if type(qs_related) is list: related_values.extend(qs_related) elif type(qs_related) is str: related_values.append(qs_related) thread_related = False forum_related = False user_related = False for related_value in related_values: if related_value == 'thread': thread_related = True elif related_value == 'forum': forum_related = True elif related_value == 'user': user_related = True else: return json.dumps({"code": 3, "response": "Wrong related value"}, indent=4) since = qs.get('since', '') limit = qs.get('limit', -1) sort = qs.get('sort', 'flat') order = qs.get('order', 'desc') post_list = get_post_list(forum=forum, since=since, limit=limit, sort=sort, order=order) for post in post_list: if user_related: post['user'] = get_user_dict(post['user']) if thread_related: post['thread'] = get_thread_by_id(post['thread']) if forum_related: post['forum'] = get_forum_dict(short_name=post['forum']) return json.dumps({"code": 0, "response": post_list}, indent=4)
def details(): qs = get_json(request) post_id = qs.get('post') if not post_id: return json.dumps({"code": 2, "response": "No 'post' key"}, indent=4) post = get_post_by_id(post_id) if not post: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) related_values = list() qs_related = qs.get('related') if type(qs_related) is list: related_values.extend(qs_related) elif type(qs_related) is str: related_values.append(qs_related) thread_related = False forum_related = False user_related = False for related_value in related_values: if related_value == 'forum': forum_related = True elif related_value == 'user': user_related = True elif related_value == 'thread': thread_related = True else: return json.dumps({ "code": 3, "response": "Wrong related value" }, indent=4) if thread_related: post['thread'] = get_thread_by_id(post['thread']) if forum_related: post['forum'] = get_forum_dict(short_name=post['forum']) if user_related: post['user'] = get_user_dict(post['user']) return json.dumps({"code": 0, "response": post}, indent=4)
def follow_method(do_unfollow): request_body = request.json follower = request_body.get('follower') followee = request_body.get('followee') args = {'follower': follower, 'following': followee} if not do_unfollow: db.execute( """INSERT INTO Follower (follower, following) VALUES (%(follower)s, %(following)s);""", args, True) else: db.execute( """DELETE FROM Follower WHERE follower = %(follower)s AND following = %(following)s;""", args, True) return json.dumps({ "code": 0, "response": get_user_dict(follower) }, indent=4)
def list_threads(): qs = get_json(request) forum = qs.get('forum') if not forum: return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4) since = qs.get('since', '') order = qs.get('order', '') limit = qs.get('limit', -1) thread_list = get_thread_list(forum=forum, since=since, order=order, limit=limit) # Related part related_values = list() qs_related = qs.get('related') if type(qs_related) is list: related_values.extend(qs_related) elif type(qs_related) is str: related_values.append(qs_related) forum_related = False user_related = False for related_value in related_values: if related_value == 'forum': forum_related = True elif related_value == 'user': user_related = True else: return json.dumps({"code": 3, "response": "Wrong related value"}, indent=4) for thread in thread_list: if user_related: thread['user'] = get_user_dict(thread['user']) thread['user']['subscriptions'] = get_subscribed_threads_list(thread['user']['email']) if forum_related: thread['forum'] = get_forum_dict(short_name=thread['forum']) return json.dumps({"code": 0, "response": thread_list}, indent=4)
db.execute(sql, args, True) except MySQLdb.IntegrityError, message: if message[0] == MYSQL_DUPLICATE_ENTITY_ERROR: return json.dumps( { "code": 5, "response": "This user already exists" }, indent=4) return json.dumps( { "code": 4, "response": "Oh, we have some really bad error" }, indent=4) user_dict = get_user_dict(email) return json.dumps({"code": 0, "response": user_dict}, indent=4) @module.route("/details/", methods=["GET"]) def details(): qs = get_json(request) email = qs.get('user') if not email: return json.dumps({"code": 2, "response": "No 'user' key"}, indent=4) user = get_user_dict(email) user['followers'] = get_followers_list(email) user['following'] = get_following_list(email)
is_anonymous = 0 sql = """INSERT INTO User (username, about, name, email, isAnonymous) VALUES \ (%(username)s, %(about)s, %(name)s, %(email)s, %(isAnonymous)s);""" args = {'username': username, 'about': about, 'name': name, 'email': email, 'isAnonymous': is_anonymous} try: db.execute(sql, args, True) except MySQLdb.IntegrityError, message: if message[0] == MYSQL_DUPLICATE_ENTITY_ERROR: return json.dumps({"code": 5, "response": "This user already exists"}, indent=4) return json.dumps({"code": 4, "response": "Oh, we have some really bad error"}, indent=4) user_dict = get_user_dict(email) return json.dumps({"code": 0, "response": user_dict}, indent=4) @module.route("/details/", methods=["GET"]) def details(): qs = get_json(request) email = qs.get('user') if not email: return json.dumps({"code": 2, "response": "No 'user' key"}, indent=4) user = get_user_dict(email) user['followers'] = get_followers_list(email) user['following'] = get_following_list(email)