def details(): qs = get_json(request) threadID = qs.get('thread') if not threadID: return json.dumps({"code": 2, "response": "No 'thread' key"}, indent=4) thread = get_thread_by_id(threadID) if thread == list(): return json.dumps({"code": 1, "response": "Empty set"}, indent=4) relatedValues = list() qsRelated = qs.get('related') if type(qsRelated) is list: relatedValues.extend(qsRelated) elif type(qsRelated) is str: relatedValues.append(qsRelated) forumRelated = False userRelated = False for relatedValue in relatedValues: if relatedValue == 'forum': forumRelated = True elif relatedValue == 'user': userRelated = True else: return json.dumps({"code": 3, "response": "Wrong related value"}, indent=4) if forumRelated: thread['forum'] = get_forum_dict(short_name=thread['forum']) if userRelated: thread['user'] = get_user_dict(thread['user']) return json.dumps({"code": 0, "response": thread}, indent=4)
def list_method(): qs = get_json(request) forum = qs.get('forum') thread = qs.get('thread') if not forum and not thread: return json.dumps({ "code": 2, "response": "No 'forum' or 'thread' key" }, indent=4) since = qs.get('since', '') limit = qs.get('limit', -1) order = qs.get('order', '') if forum: postList = get_post_list(forum=forum, since=since, limit=limit, order=order) else: postList = get_post_list(thread=thread, since=since, limit=limit, order=order) return json.dumps({"code": 0, "response": postList}, indent=4)
def listFollowers(isFollowing): qs = get_json(request) email = qs.get('user') if not email: return json.dumps({"code": 2, "response": "No 'user' key"}, indent=4) # Since part since_id = qs.get('since_id', -1) if since_id != -1: sinceSql = """AND User.user >= {}""".format(since_id) else: sinceSql = "" # Order part order_sql = """ORDER BY User.name {}""".format(qs.get('order', 'desc')) # Limit part limit = qs.get('limit', -1) if limit != -1: try: limit = int(limit) except ValueError: return json.dumps({"code": 3, "response": "Wrong limit value"}, indent=4) if limit < 0: return json.dumps({"code": 3, "response": "Wrong limit value"}, indent=4) limit_sql = """LIMIT {}""".format(limit) else: limit_sql = "" sql = """SELECT about, email, user, isAnonymous, name, username FROM User JOIN Follower ON """ if not isFollowing: sql += """Follower.follower = User.email WHERE Follower.following""" else: sql += """Follower.following = User.email WHERE Follower.follower""" sql += """ = %(email)s {since_value} {order_value} {limit_value};""".format( since_value=sinceSql, order_value=order_sql, limit_value=limit_sql) userListSql = db.execute(sql, {'email': email}) if not userListSql: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) user_list = list() for userSql in userListSql: followerEmail = str_to_json(userSql[1]) user_list.append({'about': str_to_json(userSql[0]), 'email': followerEmail, 'id': str_to_json(userSql[2]), 'isAnonymous': str_to_json(userSql[3]), 'name': str_to_json(userSql[4]), 'username': str_to_json(userSql[5]), 'followers': get_followers_list(followerEmail), 'following': get_following_list(followerEmail), 'subscriptions': get_subscribed_threads_list(followerEmail)}) return json.dumps({"code": 0, "response": user_list}, indent=4)
def listPosts(): qs = get_json(request) forum = qs.get('forum') if not forum: return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4) # Related part relatedValues = list() qsRelated = qs.get('related') if type(qsRelated) is list: relatedValues.extend(qsRelated) elif type(qsRelated) is str: relatedValues.append(qsRelated) threadRelated = False forumRelated = False userRelated = False for relatedValue in relatedValues: if relatedValue == 'thread': threadRelated = True elif relatedValue == 'forum': forumRelated = True elif relatedValue == 'user': userRelated = 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') postList = get_post_list(forum=forum, since=since, limit=limit, sort=sort, order=order) for post in postList: if userRelated: post['user'] = get_user_dict(post['user']) if threadRelated: post['thread'] = get_thread_by_id(post['thread']) if forumRelated: post['forum'] = get_forum_dict(short_name=post['forum']) return json.dumps({"code": 0, "response": postList}, indent=4)
def list_posts(): qs = get_json(request) thread = qs.get('thread') since = qs.get('since', '') limit = qs.get('limit', -1) order = qs.get('order', 'desc') sort = qs.get('sort', 'flat') postList = get_post_list(thread=thread, since=since, limit=limit, sort=sort, order=order) return json.dumps({"code": 0, "response": postList}, indent=4)
def listUsers(): qs = get_json(request) if not qs.get('forum'): return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4) # Since id part since_id = qs.get('since_id') if since_id: try: since_id = int(since_id) except ValueError: return json.dumps({"code": 3, "response": "Wrong since_id value"}, indent=4) since_id_sql = """AND User.user >= {}""".format(since_id) else: since_id_sql = '' # Limit part if qs.get('limit'): limit = qs.get('limit')[0] try: limit = int(limit) except ValueError: return json.dumps({"code": 3, "response": "Wrong limit value"}, indent=4) if limit < 0: return json.dumps({"code": 3, "response": "Wrong limit value"}, indent=4) limit_sql = """LIMIT {}""".format(limit) else: limit_sql = '' # Order part order = qs.get('order', 'desc') order_sql = """ORDER BY User.name {}""".format(order) sql = """SELECT User.user, User.email, User.name, User.username, User.isAnonymous, User.about FROM User \ WHERE User.email IN (SELECT DISTINCT user FROM Post WHERE forum = %(forum)s) {snc_sql} {ord_sql} \ {lim_sql};""".format(snc_sql=since_id_sql, lim_sql=limit_sql, ord_sql=order_sql) userListSql = db.execute(sql, {'forum': qs.get('forum')}) userList = list() for userSql in userListSql: email = str_to_json(userSql[1]) userList.append({'id': str_to_json(userSql[0]), 'email': email, 'name': str_to_json(userSql[2]), 'username': str_to_json(userSql[3]), 'isAnonymous': str_to_json(userSql[4]), 'about': str_to_json(userSql[5]), 'subscriptions': get_subscribed_threads_list(email)}) return json.dumps({"code": 0, "response": userList}, indent=4)
def listPosts(): qs = get_json(request) email = qs.get('user') if not email: return json.dumps({"code": 2, "response": "No 'user' key"}, indent=4) since = qs.get('since', '') limit = qs.get('limit', -1) order = qs.get('order', 'desc') postList = get_post_list(user=email, since=since, limit=limit, order=order) return json.dumps({"code": 0, "response": postList}, 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 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) forumDict = get_forum_dict(short_name=short_name) if not forumDict: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) if qs.get('related', '') == 'user': forumDict['user'] = get_user_dict(forumDict['user']) return json.dumps({"code": 0, "response": forumDict}, indent=4)
def listThreads(): 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) threadList = get_thread_list(forum=forum, since=since, order=order, limit=limit) # Related part relatedValues = list() qsRelated = qs.get('related') if type(qsRelated) is list: relatedValues.extend(qsRelated) elif type(qsRelated) is str: relatedValues.append(qsRelated) forumRelated = False userRelated = False for relatedValue in relatedValues: if relatedValue == 'forum': forumRelated = True elif relatedValue == 'user': userRelated = True else: return json.dumps({ "code": 3, "response": "Wrong related value" }, indent=4) for thread in threadList: if userRelated: thread['user'] = get_user_dict(thread['user']) thread['user']['subscriptions'] = get_subscribed_threads_list( thread['user']['email']) if forumRelated: thread['forum'] = get_forum_dict(short_name=thread['forum']) return json.dumps({"code": 0, "response": threadList}, indent=4)
def listPosts(): qs = get_json(request) forum = qs.get('forum') if not forum: return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4) # Related part relatedValues = list() qsRelated = qs.get('related') if type(qsRelated) is list: relatedValues.extend(qsRelated) elif type(qsRelated) is str: relatedValues.append(qsRelated) threadRelated = False forumRelated = False userRelated = False for relatedValue in relatedValues: if relatedValue == 'thread': threadRelated = True elif relatedValue == 'forum': forumRelated = True elif relatedValue == 'user': userRelated = 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') postList = get_post_list(forum=forum, since=since, limit=limit, sort=sort, order=order) for post in postList: if userRelated: post['user'] = get_user_dict(post['user']) if threadRelated: post['thread'] = get_thread_by_id(post['thread']) if forumRelated: post['forum'] = get_forum_dict(short_name=post['forum']) return json.dumps({"code": 0, "response": postList}, indent=4)
def list_method(): qs = get_json(request) forum = qs.get('forum') thread = qs.get('thread') if not forum and not thread: return json.dumps({"code": 2, "response": "No 'forum' or 'thread' key"}, indent=4) since = qs.get('since', '') limit = qs.get('limit', -1) order = qs.get('order', '') if forum: postList = get_post_list(forum=forum, since=since, limit=limit, order=order) else: postList = get_post_list(thread=thread, since=since, limit=limit, order=order) return json.dumps({"code": 0, "response": postList}, indent=4)
def list_method(): qs = get_json(request) if qs.get('forum'): key = "forum" elif qs.get('user'): key = "user" else: return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4) keyValue = qs.get(key) since = qs.get('since', '') order = qs.get('order', '') limit = qs.get('limit', -1) if key == "forum": threadList = get_thread_list(forum=keyValue, since=since, order=order, limit=limit) else: threadList = get_thread_list(user=keyValue, since=since, order=order, limit=limit) return json.dumps({"code": 0, "response": threadList}, indent=4)
def listThreads(): 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) threadList = get_thread_list(forum=forum, since=since, order=order, limit=limit) # Related part relatedValues = list() qsRelated = qs.get('related') if type(qsRelated) is list: relatedValues.extend(qsRelated) elif type(qsRelated) is str: relatedValues.append(qsRelated) forumRelated = False userRelated = False for relatedValue in relatedValues: if relatedValue == 'forum': forumRelated = True elif relatedValue == 'user': userRelated = True else: return json.dumps({"code": 3, "response": "Wrong related value"}, indent=4) for thread in threadList: if userRelated: thread['user'] = get_user_dict(thread['user']) thread['user']['subscriptions'] = get_subscribed_threads_list(thread['user']['email']) if forumRelated: thread['forum'] = get_forum_dict(short_name=thread['forum']) return json.dumps({"code": 0, "response": threadList}, indent=4)
def listFollowers(isFollowing): qs = get_json(request) email = qs.get('user') if not email: return json.dumps({"code": 2, "response": "No 'user' key"}, indent=4) # Since part since_id = qs.get('since_id', -1) if since_id != -1: sinceSql = """AND User.user >= {}""".format(since_id) else: sinceSql = "" # Order part order_sql = """ORDER BY User.name {}""".format(qs.get('order', 'desc')) # Limit part limit = qs.get('limit', -1) if limit != -1: try: limit = int(limit) except ValueError: return json.dumps({ "code": 3, "response": "Wrong limit value" }, indent=4) if limit < 0: return json.dumps({ "code": 3, "response": "Wrong limit value" }, indent=4) limit_sql = """LIMIT {}""".format(limit) else: limit_sql = "" sql = """SELECT about, email, user, isAnonymous, name, username FROM User JOIN Follower ON """ if not isFollowing: sql += """Follower.follower = User.email WHERE Follower.following""" else: sql += """Follower.following = User.email WHERE Follower.follower""" sql += """ = %(email)s {since_value} {order_value} {limit_value};""".format( since_value=sinceSql, order_value=order_sql, limit_value=limit_sql) userListSql = db.execute(sql, {'email': email}) if not userListSql: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) user_list = list() for userSql in userListSql: followerEmail = str_to_json(userSql[1]) user_list.append({ 'about': str_to_json(userSql[0]), 'email': followerEmail, 'id': str_to_json(userSql[2]), 'isAnonymous': str_to_json(userSql[3]), 'name': str_to_json(userSql[4]), 'username': str_to_json(userSql[5]), 'followers': get_followers_list(followerEmail), 'following': get_following_list(followerEmail), 'subscriptions': get_subscribed_threads_list(followerEmail) }) return json.dumps({"code": 0, "response": user_list}, indent=4)
def listUsers(): qs = get_json(request) if not qs.get('forum'): return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4) # Since id part since_id = qs.get('since_id') if since_id: try: since_id = int(since_id) except ValueError: return json.dumps({ "code": 3, "response": "Wrong since_id value" }, indent=4) since_id_sql = """AND User.user >= {}""".format(since_id) else: since_id_sql = '' # Limit part if qs.get('limit'): limit = qs.get('limit')[0] try: limit = int(limit) except ValueError: return json.dumps({ "code": 3, "response": "Wrong limit value" }, indent=4) if limit < 0: return json.dumps({ "code": 3, "response": "Wrong limit value" }, indent=4) limit_sql = """LIMIT {}""".format(limit) else: limit_sql = '' # Order part order = qs.get('order', 'desc') order_sql = """ORDER BY User.name {}""".format(order) sql = """SELECT User.user, User.email, User.name, User.username, User.isAnonymous, User.about FROM User \ WHERE User.email IN (SELECT DISTINCT user FROM Post WHERE forum = %(forum)s) {snc_sql} {ord_sql} \ {lim_sql};""".format(snc_sql=since_id_sql, lim_sql=limit_sql, ord_sql=order_sql) userListSql = db.execute(sql, {'forum': qs.get('forum')}) userList = list() for userSql in userListSql: email = str_to_json(userSql[1]) userList.append({ 'id': str_to_json(userSql[0]), 'email': email, 'name': str_to_json(userSql[2]), 'username': str_to_json(userSql[3]), 'isAnonymous': str_to_json(userSql[4]), 'about': str_to_json(userSql[5]), 'subscriptions': get_subscribed_threads_list(email) }) return json.dumps({"code": 0, "response": userList}, indent=4)