def open(request): thread_query = json.loads(request.body.decode("utf-8")) thread = int(thread_query.get('thread')) execute( """UPDATE Thread SET isClosed = False WHERE thread = %(thread)s;""", {'thread': thread}, True) return JsonResponse({"code": 0, "response": thread})
def remove(request): thread_query = json.loads(request.body) thread_ID = thread_query.get('thread') execute("""UPDATE Post SET isDeleted = 1 WHERE thread = '%(thread)s';""", {'thread': thread_ID}, True) execute( """UPDATE Thread SET posts = 0, isDeleted = True WHERE thread = %(thread)s;""", {'thread': thread_ID}, True) return JsonResponse({"code": 0, "response": {"thread": thread_ID}})
def update(request): post_query = json.loads(request.body.decode("utf-8")) post_ID = int(post_query.get('post')) message = post_query.get('message') args = {'post': post_ID, 'message': message} execute("""UPDATE Post SET message = %(message)s WHERE post = %(post)s;""", args, True) post_dict = get_post_by_id(post_ID) return JsonResponse({"code": 0, "response": post_dict})
def updateProfile(request): user = json.loads(request.body.decode("utf-8")) email = user.get('user') name = user.get('name') about = user.get('about') args = {'about': about, 'name': name, 'email': email} execute("""UPDATE User SET about = %(about)s, name = %(name)s WHERE email = %(email)s;""", args, True) user_dict = get_user_dict(email) return JsonResponse({"code": 0, "response": user_dict})
def unfollow(request): follow = json.loads(request.body.decode("utf-8")) follower = follow.get('follower') followee = follow.get('followee') args = {'follower': follower, 'following': followee} execute("""DELETE FROM Follower WHERE follower = %(follower)s AND followee = %(following)s;""", args, True) user_dict = get_user_dict(follower) user_dict['followers'] = get_followers_list(follower) user_dict['following'] = get_following_list(follower) user_dict['subscriptions'] = get_subscribed_threads_list(follower) return JsonResponse({"code": 0, "response": user_dict})
def follow(request): follow = json.loads(request.body.decode("utf-8")) follower = follow.get('follower') followee = follow.get('followee') execute("""INSERT INTO Follower (follower, followee) VALUES \ (%(follower)s, %(followee)s);""", {'follower': follower, 'followee': followee}) user_dict = get_user_dict(follower) user_dict['followers'] = get_followers_list(follower) user_dict['following'] = get_following_list(follower) user_dict['subscriptions'] = get_subscribed_threads_list(follower) return JsonResponse({"code": 0, "response": user_dict})
def update(request): up_thread = json.loads(request.body.decode("utf-8")) thread = up_thread.get('thread') message = up_thread.get('message') slug = up_thread.get('slug') args = {'thread': thread, 'message': message, 'slug': slug} execute( """UPDATE Thread SET message = %(message)s, slug = %(slug)s WHERE thread = %(thread)s;""", args, True) thread_dict = get_thread_by_id(thread) return JsonResponse({"code": 0, "response": thread_dict})
def vote(request): voteBody = json.loads(request.body) post = voteBody.get('post') vote = voteBody.get('vote') if vote == 1: execute( """UPDATE Post SET likes = likes + 1, points = points + 1 WHERE post = %(post)s;""", {'post': post}, True) elif vote == -1: execute( """UPDATE Post SET dislikes = dislikes + 1, points = points - 1 WHERE post = %(post)s;""", {'post': post}, True) post_dict = get_post_by_id(post) return JsonResponse({"code": 0, "response": post_dict})
def restore(request): post_query = json.loads(request.body) postID = post_query.get('post') post = get_post_by_id(postID) if type(post) == type([]): return JsonResponse({"code": 1, "response": "Empty set"}) thread_id = post['thread'] execute("""UPDATE Post SET isDeleted = 0 WHERE post = %(post)s;""", {'post': postID}, True) execute( """UPDATE Thread SET posts = posts + 1 WHERE thread = %(thread)s;""", {'thread': thread_id}, post=True) return JsonResponse({"code": 0, "response": {"post": postID}})
def vote(request): voteBody = json.loads(request.body) thread = voteBody.get('thread') vote = voteBody.get('vote') if vote == 1: execute( """UPDATE Thread SET likes = likes + 1, points = points + 1 WHERE thread = %(thread)s;""", {'thread': thread}, True) elif vote == -1: execute( """UPDATE Thread SET dislikes = dislikes + 1, points = points - 1 WHERE thread = %(thread)s;""", {'thread': thread}, True) thread_dict = get_thread_by_id(thread) return JsonResponse({"code": 0, "response": thread_dict})
def restore(request): thread_query = json.loads(request.body) thread_ID = thread_query.get('thread') execute( "UPDATE Post SET isDeleted = False WHERE thread = '{thread}';".format( thread=str(thread_ID) + ""), {}, True) posts = int( execute("SELECT COUNT(*) FROM Post WHERE thread = '{thread}';".format( thread=str(thread_ID) + ""))[0][0]) execute( """UPDATE Thread SET posts = %(posts)s, isDeleted = False WHERE thread = %(thread)s;""", { 'posts': posts, 'thread': thread_ID }, True) return JsonResponse({"code": 0, "response": {"thread": thread_ID}})
def create(request): user = json.loads(request.body.decode("utf-8")) email = user.get('email') username = user.get('username') name = user.get('name') about = user.get('about') isAnonymous = user.get('isAnonymous', False) 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: ID = execute(sql, args, True) except IntegrityError: return JsonResponse({"code": 5, "response": "This user already exists"}) except DatabaseError: return JsonResponse({"code": 4, "response": "Oh, we have some really bad error"}) #user_dict = get_user_dict(email) return JsonResponse({"code": 0, "response":{'id': ID, 'email': email, 'name': name, 'username': username, 'isAnonymous': isAnonymous, 'about': about}})
def create(request): forum = json.loads(request.body) name = forum.get('name') short_name = forum.get('short_name') user = forum.get('user') sql = """INSERT INTO Forum (name, short_name, user) VALUES \ (%(name)s, %(short_name)s, %(user)s);""" args = {'name': name, 'short_name': short_name, 'user': user} try: execute(sql, args, True) except IntegrityError: forum_dict = get_forum_dict(short_name) return JsonResponse({"code": 0, "response": forum_dict}) except DatabaseError: return JsonResponse({ "code": 4, "response": "Oh, we have some really bad error" }) forum_dict = get_forum_dict(short_name) return JsonResponse({"code": 0, "response": forum_dict})
def subscribe(request): subscription = json.loads(request.body.decode("utf-8")) user = subscription.get('user') thread = subscription.get('thread') try: subscription_id = execute( """INSERT INTO Subscription (subscriber, thread) VALUES \ (%(user)s, %(thread)s);""", { 'user': user, 'thread': thread }, True) except IntegrityError: 1 subscription_dict = get_subscription(user, thread) return JsonResponse({"code": 0, "response": subscription_dict})
def unsubscribe(request): subscription = json.loads(request.body.decode("utf-8")) user = subscription.get('user') thread = subscription.get('thread') subscription_id = execute( """DELETE FROM Subscription WHERE subscriber = %(subscriber)s AND thread = %(thread)s;""", { 'subscriber': user, 'thread': thread }, True) return JsonResponse({ "code": 0, "response": { 'thread': thread, 'user': user } })
def create(request): thread = json.loads(request.body.decode("utf-8")) title = thread.get('title') user = thread.get('user') forum = thread.get('forum') message = thread.get('message') date = thread.get('date') slug = thread.get('slug') if thread.get('isClosed'): isClosed = True else: isClosed = False if thread.get('isDeleted', False): isDeleted = True else: isDeleted = False sql = """INSERT INTO Thread (title, user, forum, message, date, slug, isDeleted, isClosed) VALUES \ (%(title)s, %(user)s, %(forum)s, %(message)s, %(date)s, %(slug)s, %(isDeleted)s, %(isClosed)s);""" args = { 'title': title, 'user': user, 'forum': forum, 'message': message, 'date': date, 'slug': slug, 'isDeleted': isDeleted, 'isClosed': isClosed } try: thread_id = execute(sql, args, True) except IntegrityError: thread_dict = get_thread_dict(title) return JsonResponse({"code": 0, "response": thread_dict}) except DatabaseError: return JsonResponse({ "code": 4, "response": "Oh, we have some really bad error" }) thread_dict = get_thread_by_id(thread_id) return JsonResponse({"code": 0, "response": thread_dict})
def create(request): post = json.loads(request.body) date = post.get('date') thread = post.get('thread') message = post.get('message') user = post.get('user') forum = post.get('forum') isDeleted = post.get('isDeleted', False) isSpam = post.get('isSpam', False) isEdited = post.get('isEdited', False) isHighlighted = post.get('isHighlighted', False) isApproved = post.get('isApproved', False) parent = post.get('parent') mpath = "" sql = """INSERT INTO Post (date, thread, forum, message, user, isSpam, isDeleted, isEdited, isApproved, isHighlighted, parent, mpath) VALUES \ (%(date)s, %(thread)s, %(forum)s, %(message)s, %(user)s, %(isSpam)s, %(isDeleted)s, %(isEdited)s, %(isApproved)s, %(isHighlighted)s, %(parent)s, %(mpath)s);""" args = { 'date': date, 'thread': thread, 'forum': forum, 'message': message, 'user': user, 'isSpam': isSpam, 'isDeleted': isDeleted, 'isEdited': isEdited, 'isApproved': isApproved, 'isHighlighted': isHighlighted, 'parent': parent, 'mpath': mpath } try: postID = execute(sql, args, True) except IntegrityError: post_dict = get_post_dict(user, date) return JsonResponse({"code": 0, "response": post_dict}) except DatabaseError: return JsonResponse({ "code": 4, "response": "Oh, we have some really bad error" }) add_post_to_thread(thread) if parent == None: mpath = mpath + date + '-' + str(postID) else: mpath_que = execute( """SELECT mpath FROM Post \ WHERE post = %(parent)s;""", {'parent': parent}) mpath = mpath_que[0][0] + "." + str(date) + '-' + str(postID) execute("""UPDATE Post SET mpath = %(mpath)s WHERE post = %(id)s;""", { 'mpath': mpath, 'id': postID }) return JsonResponse({ "code": 0, "response": { 'id': postID, 'user': user, 'thread': thread, 'forum': forum, 'message': message, 'parent': parent, 'date': date, 'isSpam': isSpam, 'isEdited': isEdited, 'isDeleted': isDeleted, 'isHighlighted': isHighlighted, 'isApproved': isApproved } })