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 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 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 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)
@module.route("/create/", methods=["POST"]) def create(): requestBody = request.json name = requestBody.get('name') short_name = requestBody.get('short_name') user = requestBody.get('user') try: db.execute("""INSERT INTO Forum (name, short_name, user) VALUES (%(name)s, %(short_name)s, %(user)s);""", {'name': name, 'short_name': short_name, 'user': user}, True) except MySQLdb.IntegrityError, message: print message[0] finally: forumDict = get_forum_dict(short_name=short_name) return json.dumps({"code": 0, "response": forumDict}, indent=4) @module.route("/details/", methods=["GET"]) 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)
name = requestBody.get('name') short_name = requestBody.get('short_name') user = requestBody.get('user') try: db.execute( """INSERT INTO Forum (name, short_name, user) VALUES (%(name)s, %(short_name)s, %(user)s);""", { 'name': name, 'short_name': short_name, 'user': user }, True) except MySQLdb.IntegrityError, message: print message[0] finally: forumDict = get_forum_dict(short_name=short_name) return json.dumps({"code": 0, "response": forumDict}, indent=4) @module.route("/details/", methods=["GET"]) 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)