def vote(): requestBody = request.json postID = requestBody.get('post') voteValue = requestBody.get('vote') if voteValue == 1: db.execute( """UPDATE Post SET likes = likes + 1, points = points + 1 WHERE post = %(post)s;""", {'post': postID}, True) elif voteValue == -1: db.execute( """UPDATE Post SET dislikes = dislikes + 1, points = points - 1 WHERE post = %(post)s;""", {'post': postID}, True) else: return json.dumps({ "code": 3, "response": "Wrong 'vote' value'" }, indent=4) post = get_post_by_id(postID) if not post: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) return json.dumps({"code": 0, "response": post}, indent=4)
def create(): requestBody = request.json # Required forum = requestBody.get('forum') title = try_encode(requestBody.get('title')) if requestBody.get('isClosed'): is_closed = 1 else: is_closed = 0 user = requestBody.get('user') date = requestBody.get('date') message = requestBody.get('message') slug = requestBody.get('slug') # Optional if requestBody.get('isDeleted', False): isDeleted = 1 else: isDeleted = 0 sql = """INSERT INTO Thread (forum, title, isClosed, user, date, message, slug, isDeleted) \ VALUES (%(forum)s, %(title)s, %(isClosed)s, %(user)s, %(date)s, %(message)s, %(slug)s, %(isDeleted)s);""" args = {'forum': forum, 'title': title, 'isClosed': is_closed, 'user': user, 'date': date, 'message': message, 'slug': slug, 'isDeleted': isDeleted} try: db.execute(sql, args, True) except MySQLdb.IntegrityError, message: print message[0]
def create(): requestBody = request.json username = try_encode(requestBody.get('username')) about = requestBody.get('about') name = try_encode(requestBody.get('name')) email = requestBody.get('email') isAnonymousKey = requestBody.get('isAnonymous', False) if isAnonymousKey: isAnonymous = 1 else: 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)
def update(): requestBody = request.json message = requestBody.get('message') slug = requestBody.get('slug') threadID = requestBody.get('thread') db.execute("""UPDATE Thread SET message = %(message)s, slug = %(slug)s WHERE thread = %(thread)s;""", {'message': message, 'slug': slug, 'thread': threadID}, True) return json.dumps({"code": 0, "response": get_thread_by_id(threadID)}, indent=4)
def unsubscribe(): requestBody = request.json user = requestBody.get('user') thread = requestBody.get('thread') db.execute("""DELETE FROM Subscription WHERE subscriber = %(subscriber)s AND thread = %(thread)s;""", {'subscriber': user, 'thread': thread}, True) resultDict = {'thread': thread, 'user': str_to_json(user)} return json.dumps({"code": 0, "response": resultDict}, 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 subscribe(): requestBody = request.json user = requestBody.get('user') thread = requestBody.get('thread') try: db.execute("""INSERT INTO Subscription (subscriber, thread) VALUES (%(subscriber)s, %(thread)s);""", {'subscriber': user, 'thread': thread}, True) except MySQLdb.IntegrityError, message: if message[0] == MYSQL_DUPLICATE_ENTITY_ERROR: print "Already subscribed"
def restore(): thread = request.json.get('thread') postList = get_post_list(thread=thread) for post in postList: restore_post(post['id']) db.execute("""UPDATE Thread SET isDeleted = 0, posts = %(posts)s WHERE thread = %(thread)s;""", {'posts': len(postList), 'thread': thread}, True) return json.dumps({"code": 0, "response": thread}, indent=4)
def remove(): requestBody = request.json thread = requestBody.get('thread') postList = get_post_list(thread=thread) for post in postList: remove_post(post['id']) db.execute("""UPDATE Thread SET isDeleted = 1, posts = 0 WHERE thread = %(thread)s;""", {'thread': thread}, True) 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 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]
def status(): user_count = db.execute("""SELECT count(*) FROM User;""") thread_count = db.execute("""SELECT count(*) FROM Thread;""") forum_count = db.execute("""SELECT count(*) FROM Forum;""") post_count = db.execute("""SELECT count(*) FROM Post;""") users = user_count[0][0] threads = thread_count[0][0] forums = forum_count[0][0] posts = post_count[0][0] return json.dumps({"code": 0, "response": {"user": users, "thread": threads, "forum": forums, "post": posts}})
def update(): requestBody = request.json postID = requestBody.get('post') message = try_encode(requestBody.get('message')) args = {'message': message, 'post': postID} db.execute("""UPDATE Post SET message = %(message)s WHERE post = %(post)s;""", args, True) post = get_post_by_id(postID) if not post: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) return json.dumps({"code": 0, "response": post}, indent=4)
def vote(): requestBody = request.json voteValue = requestBody.get('vote') threadID = requestBody.get('thread') if voteValue == 1: db.execute("""UPDATE Thread SET likes = likes + 1, points = points + 1 WHERE thread = %(thread)s;""", {'thread': threadID}, True) else: db.execute("""UPDATE Thread SET dislikes = dislikes + 1, points = points - 1 WHERE thread = %(thread)s;""", {'thread': threadID}, True) return json.dumps({"code": 0, "response": get_thread_by_id(threadID)}, indent=4)
def update(): requestBody = request.json postID = requestBody.get('post') message = try_encode(requestBody.get('message')) args = {'message': message, 'post': postID} db.execute( """UPDATE Post SET message = %(message)s WHERE post = %(post)s;""", args, True) post = get_post_by_id(postID) if not post: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) return json.dumps({"code": 0, "response": post}, 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 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]
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 vote(): requestBody = request.json postID = requestBody.get('post') voteValue = requestBody.get('vote') if voteValue == 1: db.execute("""UPDATE Post SET likes = likes + 1, points = points + 1 WHERE post = %(post)s;""", {'post': postID}, True) elif voteValue == -1: db.execute("""UPDATE Post SET dislikes = dislikes + 1, points = points - 1 WHERE post = %(post)s;""", {'post': postID}, True) else: return json.dumps({"code": 3, "response": "Wrong 'vote' value'"}, indent=4) post = get_post_by_id(postID) if not post: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) return json.dumps({"code": 0, "response": post}, indent=4)
def status(): user_count = db.execute("""SELECT count(*) FROM User;""") thread_count = db.execute("""SELECT count(*) FROM Thread;""") forum_count = db.execute("""SELECT count(*) FROM Forum;""") post_count = db.execute("""SELECT count(*) FROM Post;""") users = user_count[0][0] threads = thread_count[0][0] forums = forum_count[0][0] posts = post_count[0][0] return json.dumps({ "code": 0, "response": { "user": users, "thread": threads, "forum": forums, "post": posts } })
def create(): requestBody = request.json # Required date = requestBody.get('date') thread = requestBody.get('thread') message = requestBody.get('message') user = requestBody.get('user') forum = requestBody.get('forum') # Optional parent = requestBody.get('parent', None) if requestBody.get('isApproved', False): isApproved = 1 else: isApproved = 0 if requestBody.get('isHighlighted', False): isHighlighted = 1 else: isHighlighted = 0 if requestBody.get('isEdited', False): isEdited = 1 else: isEdited = 0 if requestBody.get('isSpam', False): isSpam = 1 else: isSpam = 0 if requestBody.get('isDeleted', False): isDeleted = 1 else: isDeleted = 0 sql = """INSERT INTO Post (user, thread, forum, message, parent, date, \ isSpam, isEdited, isDeleted, isHighlighted, isApproved) VALUES \ (%(user)s, %(thread)s, %(forum)s, %(message)s, %(parent)s, %(date)s, \ %(isSpam)s, %(isEdited)s, %(isDeleted)s, %(isHighlighted)s, %(isApproved)s);""" args = {'user': user, 'thread': thread, 'forum': forum, 'message': message, 'parent': parent, 'date': date, 'isSpam': isSpam, 'isEdited': isEdited, 'isDeleted': isDeleted, 'isHighlighted': isHighlighted, 'isApproved': isApproved} postID = db.execute(sql, args, True) post = get_post_by_id(postID) inc_posts_for_thread(thread) if not post: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) return json.dumps({"code": 0, "response": post}, 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 create(): requestBody = request.json username = try_encode(requestBody.get('username')) about = requestBody.get('about') name = try_encode(requestBody.get('name')) email = requestBody.get('email') isAnonymousKey = requestBody.get('isAnonymous', False) if isAnonymousKey: isAnonymous = 1 else: 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)
def close(): thread = request.json.get('thread') db.execute("""UPDATE Thread SET isClosed = 1 WHERE thread = %(thread)s;""", {'thread': thread}, True) return json.dumps({"code": 0, "response": thread}, indent=4)
def clear(): db.execute("""DELETE Forum.* FROM Forum;""", post=True) db.execute("""DELETE User.* FROM User;""", post=True) db.execute("""DELETE Post.* FROM Post;""") db.execute("""DELETE Thread.* FROM Thread;""", post=True) db.execute("""DELETE Subscription.* FROM Subscription;""", post=True) db.execute("""DELETE Follower.* FROM Follower;""", post=True) return json.dumps({"code": 0, "response": "OK"})
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 create(): requestBody = request.json # Required date = requestBody.get('date') thread = requestBody.get('thread') message = requestBody.get('message') user = requestBody.get('user') forum = requestBody.get('forum') # Optional parent = requestBody.get('parent', None) if requestBody.get('isApproved', False): isApproved = 1 else: isApproved = 0 if requestBody.get('isHighlighted', False): isHighlighted = 1 else: isHighlighted = 0 if requestBody.get('isEdited', False): isEdited = 1 else: isEdited = 0 if requestBody.get('isSpam', False): isSpam = 1 else: isSpam = 0 if requestBody.get('isDeleted', False): isDeleted = 1 else: isDeleted = 0 sql = """INSERT INTO Post (user, thread, forum, message, parent, date, \ isSpam, isEdited, isDeleted, isHighlighted, isApproved) VALUES \ (%(user)s, %(thread)s, %(forum)s, %(message)s, %(parent)s, %(date)s, \ %(isSpam)s, %(isEdited)s, %(isDeleted)s, %(isHighlighted)s, %(isApproved)s);""" args = { 'user': user, 'thread': thread, 'forum': forum, 'message': message, 'parent': parent, 'date': date, 'isSpam': isSpam, 'isEdited': isEdited, 'isDeleted': isDeleted, 'isHighlighted': isHighlighted, 'isApproved': isApproved } postID = db.execute(sql, args, True) post = get_post_by_id(postID) inc_posts_for_thread(thread) if not post: return json.dumps({"code": 1, "response": "Empty set"}, indent=4) return json.dumps({"code": 0, "response": post}, 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)