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 update_profile(): requestBody = request.json about = try_encode(requestBody.get('about')) email = try_encode(requestBody.get('user')) name = try_encode(requestBody.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 follow(): requestBody = request.json follower = requestBody.get('follower') followee = requestBody.get('followee') args = {'follower': follower, 'following': followee} db.execute("""INSERT INTO Follower (follower, following) VALUES (%(follower)s, %(following)s);""", args, True) return json.dumps({"code": 0, "response": get_user_dict(follower)}, indent=4)
def unfollow(): requestBody = request.json follower = requestBody.get('follower') followee = requestBody.get('followee') args = {'follower': follower, 'following': followee} 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 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 update_profile(): requestBody = request.json about = try_encode(requestBody.get('about')) email = try_encode(requestBody.get('user')) name = try_encode(requestBody.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 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 unfollow(): requestBody = request.json follower = requestBody.get('follower') followee = requestBody.get('followee') args = {'follower': follower, 'following': followee} 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 follow(): requestBody = request.json follower = requestBody.get('follower') followee = requestBody.get('followee') args = {'follower': follower, 'following': followee} db.execute( """INSERT INTO Follower (follower, following) VALUES (%(follower)s, %(following)s);""", args, True) return json.dumps({ "code": 0, "response": get_user_dict(follower) }, 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 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)
isAnonymous = 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': isAnonymous} 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) userDict = get_user_dict(email) return json.dumps({"code": 0, "response": userDict}, 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) user['subscriptions'] = get_subscribed_threads_list(email)
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) userDict = get_user_dict(email) return json.dumps({"code": 0, "response": userDict}, 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)