def threads_list(entity, identifier, related, params): if entity == "forum": DBconnector.exist(entity="Forums", identifier="short_name", value=identifier) if entity == "user": DBconnector.exist(entity="Users", identifier="email", value=identifier) query = "SELECT id FROM Threads WHERE " + entity + " = %s " parameters = [identifier] if "since" in params: query += " AND date >= %s" parameters.append(params["since"]) if "order" in params: query += " ORDER BY date " + params["order"] else: query += " ORDER BY date DESC " if "limit" in params: query += " LIMIT " + str(params["limit"]) thread_tuple = DBconnector.select_query(query=query, params=parameters) thread_list = [] for id in thread_tuple: id = id[0] thread_list.append(detail_thread(id=id, related=related)) return thread_list
def restore(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["thread"] try: choose_required(data=request_data, required=required_data) thread_id = request_data["thread"] DBconnector.exist(entity="Threads", identifier="id", value=thread_id) DBconnector.update_query( "UPDATE Threads SET isDeleted = 0 WHERE id = %s", (thread_id, )) thread = {"thread": thread_id} except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": thread }), content_type='application/json') else: return HttpResponse(status=405)
def threads_list(entity, identifier, related, params): if entity == "forum": DBconnector.exist(entity="Forums", identifier="short_name", value=identifier) if entity == "user": DBconnector.exist(entity="Users", identifier="email", value=identifier) query = "SELECT id FROM Threads WHERE " + entity + " = %s " parameters = [identifier] if "since" in params: query += " AND date >= %s" parameters.append(params["since"]) if "order" in params: query += " ORDER BY date " + params["order"] else: query += " ORDER BY date DESC " if "limit" in params: query += " LIMIT " + str(params["limit"]) thread_tuple = DBconnector.select_query(query=query, params=parameters) thread_list = [] for id in thread_tuple: id = id[0] thread_list.append(detail_thread(id=id, related=related)) return thread_list
def update(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["thread", "slug", "message"] try: choose_required(data=request_data, required=required_data) id = request_data["thread"] slug = request_data["slug"] message = request_data["message"] DBconnector.exist(entity="Threads", identifier="id", value=id) DBconnector.update_query( 'UPDATE Threads SET slug = %s, message = %s WHERE id = %s', ( slug, message, id, )) thread = views.detail_thread(id=id, related=[]) except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": thread }), content_type='application/json') else: return HttpResponse(status=405)
def update(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["user", "name", "about"] try: choose_required(data=request_data, required=required_data) DBconnector.exist(entity="Users", identifier="email", value=request_data["user"]) DBconnector.update_query( 'UPDATE Users SET about = %s, name = %s WHERE email = %s', ( request_data["about"], request_data["name"], request_data["user"], )) user = views.detail_user(request_data["user"]) except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": user }), content_type='application/json') else: return HttpResponse(status=405)
def subscribe(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["thread", "user"] try: choose_required(data=request_data, required=required_data) email=request_data["user"] thread_id=request_data["thread"] DBconnector.exist(entity="Threads", identifier="id", value=thread_id) DBconnector.exist(entity="Users", identifier="email", value=email) subscription = DBconnector.select_query('SELECT thread, user FROM Subscriptions WHERE user = %s AND thread = %s', (email, thread_id, )) if len(subscription) == 0: DBconnector.update_query('INSERT INTO Subscriptions (thread, user) VALUES (%s, %s)', (thread_id, email, )) subscription = DBconnector.select_query('SELECT thread, user FROM Subscriptions WHERE user = %s AND thread = %s', (email, thread_id, )) subscription = { "thread": subscription[0][0], "user": subscription[0][1] } except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": subscription}), content_type='application/json') else: return HttpResponse(status=405)
def details(request): if request.method == "GET": request_data = request.GET.dict() required_data = ["user"] try: choose_required(data=request_data, required=required_data) email = request_data["user"] user = DBconnector.select_query( 'SELECT email, about, isAnonymous, id, name, username FROM Users WHERE email = %s', (email, )) user = views.user_format(user) if user is None: raise Exception("No user with email " + email) user["followers"] = views.l_followers(email) user["following"] = views.l_following(email) user["subscriptions"] = views.l_subscriptions(email) except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": user }), content_type='application/json') else: return HttpResponse(status=405)
def create(request): if request.method == "POST": request_data = json.loads(request.body) required_data = [ "forum", "title", "isClosed", "user", "date", "message", "slug" ] optional = intersection(request=request_data, values=["isDeleted"]) try: choose_required(data=request_data, required=required_data) forum = request_data["forum"] title = request_data["title"] isClosed = request_data["isClosed"] user = request_data["user"] date = request_data["date"] message = request_data["message"] slug = request_data["slug"] DBconnector.exist(entity="Users", identifier="email", value=user) DBconnector.exist(entity="Forums", identifier="short_name", value=forum) isDeleted = 0 if "isDeleted" in optional: isDeleted = optional["isDeleted"] thread = DBconnector.select_query( 'SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' \ 'FROM Threads WHERE slug = %s', (slug, )) if len(thread) == 0: DBconnector.update_query('INSERT INTO Threads (forum, title, isClosed, user, date, message, slug, isDeleted) ' \ 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', (forum, title, isClosed, user, date, message, slug, isDeleted, )) thread = DBconnector.select_query('SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' \ 'FROM Threads WHERE slug = %s', (slug, )) response = views.thread_format(thread) del response["dislikes"] del response["likes"] del response["points"] del response["posts"] except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": response }), content_type='application/json') else: return HttpResponse(status=405)
def l_users(short_name, optional): DBconnector.exist(entity="Forums", identifier="short_name", value=short_name) query = "SELECT distinct email FROM Users JOIN Posts ON Posts.user = Users.email " \ " JOIN Forums on Forums.short_name = Posts.forum WHERE Posts.forum = %s " if "since_id" in optional: query += " AND Users.id >= " + str(optional["since_id"]) if "order" in optional: query += " ORDER BY Users.id " + optional["order"] if "limit" in optional: query += " LIMIT " + str(optional["limit"]) users_tuple = DBconnector.select_query(query, (short_name, )) users_list = [] for user in users_tuple: user = user[0] users_list.append(detail_user(user)) return users_list
def l_subscriptions(email): subscriptions_tuple = DBconnector.select_query("SELECT thread FROM Subscriptions WHERE user = %s", (email, )) subscriptions = [] for el in subscriptions_tuple: subscriptions.append(el[0]) return subscriptions
def l_following(email): following_tuple = DBconnector.select_query( "SELECT followee FROM Followers JOIN Users ON Users.email = Followers.followee WHERE follower = %s ", (email, )) following = [] for el in following_tuple: following.append(el[0]) return following
def open(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["thread"] try: choose_required(data=request_data, required=required_data) id = request_data["thread"] DBconnector.exist(entity="Threads", identifier="id", value=id) DBconnector.update_query("UPDATE Threads SET isClosed = 0 WHERE id = %s", (id, )) thread = { "thread": id } except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": thread}), content_type='application/json') else: return HttpResponse(status=405)
def detail_user(email): user = DBconnector.select_query('select email, about, isAnonymous, id, name, username FROM Users WHERE email = %s', (email, )) user = user_format(user) if user is None: raise Exception("No user with email " + email) user["followers"] = l_followers(email) user["following"] = l_following(email) user["subscriptions"] = l_subscriptions(email) return user
def l_following(email): following_tuple = DBconnector.select_query( "SELECT followee FROM Followers JOIN Users ON Users.email = Followers.followee WHERE follower = %s ", (email, )) following = [] for el in following_tuple: following.append(el[0]) return following
def subscribe(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["thread", "user"] try: choose_required(data=request_data, required=required_data) email = request_data["user"] thread_id = request_data["thread"] DBconnector.exist(entity="Threads", identifier="id", value=thread_id) DBconnector.exist(entity="Users", identifier="email", value=email) subscription = DBconnector.select_query( 'SELECT thread, user FROM Subscriptions WHERE user = %s AND thread = %s', ( email, thread_id, )) if len(subscription) == 0: DBconnector.update_query( 'INSERT INTO Subscriptions (thread, user) VALUES (%s, %s)', ( thread_id, email, )) subscription = DBconnector.select_query( 'SELECT thread, user FROM Subscriptions WHERE user = %s AND thread = %s', ( email, thread_id, )) subscription = { "thread": subscription[0][0], "user": subscription[0][1] } except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": subscription }), content_type='application/json') else: return HttpResponse(status=405)
def l_subscriptions(email): subscriptions_tuple = DBconnector.select_query( "SELECT thread FROM Subscriptions WHERE user = %s", (email, )) subscriptions = [] for el in subscriptions_tuple: subscriptions.append(el[0]) return subscriptions
def detail_user(email): user = DBconnector.select_query( 'select email, about, isAnonymous, id, name, username FROM Users WHERE email = %s', (email, )) user = user_format(user) if user is None: raise Exception("No user with email " + email) user["followers"] = l_followers(email) user["following"] = l_following(email) user["subscriptions"] = l_subscriptions(email) return user
def l_users(short_name, optional): DBconnector.exist(entity="Forums", identifier="short_name", value=short_name) query = "SELECT distinct email FROM Users JOIN Posts ON Posts.user = Users.email " \ " JOIN Forums on Forums.short_name = Posts.forum WHERE Posts.forum = %s " if "since_id" in optional: query += " AND Users.id >= " + str(optional["since_id"]) if "order" in optional: query += " ORDER BY Users.id " + optional["order"] if "limit" in optional: query += " LIMIT " + str(optional["limit"]) users_tuple = DBconnector.select_query(query, (short_name, )) users_list = [] for user in users_tuple: user = user[0] users_list.append(detail_user(user)) return users_list
def remove(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["post"] try: choose_required(data=request_data, required=required_data) post_id=request_data["post"] DBconnector.exist(entity="Posts", identifier="id", value=post_id) DBconnector.update_query("UPDATE Posts SET isDeleted = 1 WHERE Posts.id = %s", (post_id, )) post = { "post": post_id } except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": post}), content_type='application/json') else: return HttpResponse(status=405)
def restore(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["post"] try: choose_required(data=request_data, required=required_data) post_id=request_data["post"] DBconnector.exist(entity="Posts", identifier="id", value=post_id) DBconnector.update_query("UPDATE Posts SET isDeleted = 0 WHERE Posts.id = %s", (post_id, )) post = { "post": post_id } except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": post}), content_type='application/json') else: return HttpResponse(status=405)
def update(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["post", "message"] try: choose_required(data=request_data, required=required_data) update_id=request_data["post"] message=request_data["message"] DBconnector.exist(entity="Posts", identifier="id", value=update_id) DBconnector.update_query('UPDATE Posts SET message = %s WHERE id = %s', (message, update_id, )) post = views.detail_post(details_id=update_id, related=[]) except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": post}), content_type='application/json') else: return HttpResponse(status=405)
def create_user(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["email", "username", "name", "about"] optional = intersection(request=request_data, values=["isAnonymous"]) try: choose_required(data=request_data, required=required_data) isAnonymous = 0 if "isAnonymous" in optional: isAnonymous = optional["isAnonymous"] DBconnector.update_query( "INSERT INTO Users (email, about, name, username, isAnonymous) VALUES (%s, %s, %s, %s, %s)", ( request_data["email"], request_data["about"], request_data["name"], request_data["username"], isAnonymous, )) user = DBconnector.select_query( 'SELECT email, about, isAnonymous, id, name, username FROM Users WHERE email = %s', (request_data["email"], )) except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": views.user_format(user) }), content_type='application/json') else: return HttpResponse(status=405)
def detail_forum(short_name, related): forum = DBconnector.select_query( 'SELECT id, name, short_name, user FROM Forums WHERE short_name = %s', (short_name, )) if len(forum) == 0: raise ("No forum with exists short_name=" + short_name) forum = forum_format(forum) if "user" in related: forum["user"] = detail_user(forum["user"]) return forum
def update(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["thread", "slug", "message"] try: choose_required(data=request_data, required=required_data) id=request_data["thread"] slug=request_data["slug"] message=request_data["message"] DBconnector.exist(entity="Threads", identifier="id", value=id) DBconnector.update_query('UPDATE Threads SET slug = %s, message = %s WHERE id = %s', (slug, message, id, )) thread = views.detail_thread(id=id, related=[]) except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": thread}), content_type='application/json') else: return HttpResponse(status=405)
def detail_forum(short_name, related): forum = DBconnector.select_query( 'SELECT id, name, short_name, user FROM Forums WHERE short_name = %s', (short_name, ) ) if len(forum) == 0: raise ("No forum with exists short_name=" + short_name) forum = forum_format(forum) if "user" in related: forum["user"] = detail_user(forum["user"]) return forum
def create(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["name", "short_name", "user"] try: choose_required(data=request_data, required=required_data) name = request_data["name"] short_name = request_data["short_name"] user = request_data["user"] DBconnector.exist(entity="Users", identifier="email", value=user) forum = DBconnector.select_query('SELECT id, name, short_name, user FROM Forums WHERE short_name = %s', (short_name, )) if len(forum) == 0: DBconnector.update_query('INSERT INTO Forums (name, short_name, user) VALUES (%s, %s, %s)', (name, short_name, user, )) forum = DBconnector.select_query('SELECT id, name, short_name, user FROM Forums WHERE short_name = %s', (short_name, )) forum = views.forum_format(forum) except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": forum}), content_type='application/json') else: return HttpResponse(status=405)
def vote(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["thread", "vote"] try: choose_required(data=request_data, required=required_data) id = request_data["thread"] vote = request_data["vote"] DBconnector.exist(entity="Threads", identifier="id", value=id) if vote == -1: DBconnector.update_query( "UPDATE Threads SET dislikes=dislikes+1, points=points-1 where id = %s", (id, )) else: DBconnector.update_query( "UPDATE Threads SET likes=likes+1, points=points+1 where id = %s", (id, )) thread = views.detail_thread(id=id, related=[]) except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": thread }), content_type='application/json') else: return HttpResponse(status=405)
def clear(request): try: response = DBconnector.clear() except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": response }), content_type='application/json')
def detail_thread(id, related): thread = DBconnector.select_query( 'SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' 'FROM Threads WHERE id = %s', (id, )) if len(thread) == 0: raise Exception('No thread exists with id=' + str(id)) thread = thread_format(thread) if "user" in related: thread["user"] = detail_user(thread["user"]) if "forum" in related: thread["forum"] = detail_forum(short_name=thread["forum"], related=[]) return thread
def create(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["forum", "title", "isClosed", "user", "date", "message", "slug"] optional = intersection(request=request_data, values=["isDeleted"]) try: choose_required(data=request_data, required=required_data) forum = request_data["forum"] title = request_data["title"] isClosed = request_data["isClosed"] user = request_data["user"] date = request_data["date"] message = request_data["message"] slug = request_data["slug"] DBconnector.exist(entity="Users", identifier="email", value=user) DBconnector.exist(entity="Forums", identifier="short_name", value=forum) isDeleted = 0 if "isDeleted" in optional: isDeleted = optional["isDeleted"] thread = DBconnector.select_query( 'SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' \ 'FROM Threads WHERE slug = %s', (slug, )) if len(thread) == 0: DBconnector.update_query('INSERT INTO Threads (forum, title, isClosed, user, date, message, slug, isDeleted) ' \ 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', (forum, title, isClosed, user, date, message, slug, isDeleted, )) thread = DBconnector.select_query('SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' \ 'FROM Threads WHERE slug = %s', (slug, )) response = views.thread_format(thread) del response["dislikes"] del response["likes"] del response["points"] del response["posts"] except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": response}), content_type='application/json') else: return HttpResponse(status=405)
def detail_thread(id, related): thread = DBconnector.select_query( 'SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' 'FROM Threads WHERE id = %s', (id, ) ) if len(thread) == 0: raise Exception('No thread exists with id=' + str(id)) thread = thread_format(thread) if "user" in related: thread["user"] = detail_user(thread["user"]) if "forum" in related: thread["forum"] = detail_forum(short_name=thread["forum"], related=[]) return thread
def detail_post(details_id, related): post = DBconnector.select_query("SELECT date, dislikes, forum, id, isApproved, isDeleted, isEdited, " "isHighlighted, isSpam, likes, message, parent, points, thread, user " "FROM Posts WHERE id = %s", (details_id, )) if len(post) == 0: raise Exception("no post with id = " + details_id) post = post_format(post) if "user" in related: post["user"] = detail_user(post["user"]) if "forum" in related: post["forum"] = detail_forum(short_name=post["forum"], related=[]) if "thread" in related: post["thread"] = detail_thread(id=post["thread"], related=[]) return post
def detail_post(details_id, related): post = DBconnector.select_query( "SELECT date, dislikes, forum, id, isApproved, isDeleted, isEdited, " "isHighlighted, isSpam, likes, message, parent, points, thread, user " "FROM Posts WHERE id = %s", (details_id, )) if len(post) == 0: raise Exception("no post with id = " + details_id) post = post_format(post) if "user" in related: post["user"] = detail_user(post["user"]) if "forum" in related: post["forum"] = detail_forum(short_name=post["forum"], related=[]) if "thread" in related: post["thread"] = detail_thread(id=post["thread"], related=[]) return post
def follow(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["follower", "followee"] try: choose_required(data=request_data, required=required_data) DBconnector.exist(entity="Users", identifier="email", value=request_data["follower"]) DBconnector.exist(entity="Users", identifier="email", value=request_data["followee"]) if request_data["follower"] == request_data["followee"]: raise Exception("User with email=" + request_data["follower"] + " can't follow himself") follows = DBconnector.select_query( 'SELECT id FROM Followers WHERE follower = %s AND followee = %s', ( request_data["follower"], request_data["followee"], )) if len(follows) == 0: DBconnector.update_query( 'INSERT INTO Followers (follower, followee) VALUES (%s, %s)', ( request_data["follower"], request_data["followee"], )) following = views.detail_user(request_data["follower"]) except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": following }), content_type='application/json') else: return HttpResponse(status=405)
def unfollow(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["follower", "followee"] try: choose_required(data=request_data, required=required_data) DBconnector.exist(entity="Users", identifier="email", value=request_data["follower"]) DBconnector.exist(entity="Users", identifier="email", value=request_data["followee"]) query = DBconnector.select_query( 'SELECT id FROM Followers WHERE follower = %s AND followee = %s', ( request_data["follower"], request_data["followee"], )) if len(query) != 0: DBconnector.update_query( 'DELETE FROM Followers WHERE follower = %s AND followee = %s', ( request_data["follower"], request_data["followee"], )) else: raise Exception("No such following") following = views.detail_user(request_data["follower"]) except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": following }), content_type='application/json') else: return HttpResponse(status=405)
def list_following(request): if request.method == "GET": request_data = request.GET.dict() required_data = ["user"] follow_param = intersection(request=request_data, values=["limit", "order", "since_id"]) try: choose_required(data=request_data, required=required_data) query = "SELECT followee FROM Followers JOIN Users ON Users.email = Followers.followee WHERE follower = %s " if "since_id" in follow_param: query += " AND Users.id >= " + str(follow_param["since_id"]) if "order" in follow_param: query += " ORDER BY Users.name " + follow_param["order"] else: query += " ORDER BY Users.name DESC " if "limit" in follow_param: query += " LIMIT " + str(follow_param["limit"]) following_tuple = DBconnector.select_query( query=query, params=(request_data["user"], )) following_list = [] for id in following_tuple: id = id[0] following_list.append(views.detail_user(email=id)) except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": following_list }), content_type='application/json') else: return HttpResponse(status=405)
def create(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["name", "short_name", "user"] try: choose_required(data=request_data, required=required_data) name = request_data["name"] short_name = request_data["short_name"] user = request_data["user"] DBconnector.exist(entity="Users", identifier="email", value=user) forum = DBconnector.select_query( 'SELECT id, name, short_name, user FROM Forums WHERE short_name = %s', (short_name, )) if len(forum) == 0: DBconnector.update_query( 'INSERT INTO Forums (name, short_name, user) VALUES (%s, %s, %s)', ( name, short_name, user, )) forum = DBconnector.select_query( 'SELECT id, name, short_name, user FROM Forums WHERE short_name = %s', (short_name, )) forum = views.forum_format(forum) except Exception as e: return HttpResponse(json.dumps({ "code": 1, "response": (e.message) }), content_type='application/json') return HttpResponse(json.dumps({ "code": 0, "response": forum }), content_type='application/json') else: return HttpResponse(status=405)
def vote(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["thread", "vote"] try: choose_required(data=request_data, required=required_data) id = request_data["thread"] vote = request_data["vote"] DBconnector.exist(entity="Threads", identifier="id", value=id) if vote == -1: DBconnector.update_query("UPDATE Threads SET dislikes=dislikes+1, points=points-1 where id = %s", (id, )) else: DBconnector.update_query("UPDATE Threads SET likes=likes+1, points=points+1 where id = %s", (id, )) thread = views.detail_thread(id=id, related=[]) except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": thread}), content_type='application/json') else: return HttpResponse(status=405)
def create(request): if request.method == "POST": request_data = json.loads(request.body) required_data = ["user", "forum", "thread", "message", "date"] optional_data = ["parent", "isApproved", "isHighlighted", "isEdited", "isSpam", "isDeleted"] optional = intersection(request=request_data, values=optional_data) try: choose_required(data=request_data, required=required_data) date = request_data["date"] thread = request_data["thread"] message = request_data["message"] user = request_data["user"] forum = request_data["forum"] DBconnector.exist(entity="Threads", identifier="id", value=thread) DBconnector.exist(entity="Forums", identifier="short_name", value=forum) DBconnector.exist(entity="Users", identifier="email", value=user) isTread = DBconnector.select_query( "SELECT Threads.id FROM Threads JOIN Forums ON Threads.forum = Forums.short_name " \ "WHERE Threads.forum = %s AND Threads.id = %s", (forum, thread, )) if len(isTread) == 0: raise Exception("no thread with id = " + str(thread) + " in forum " + forum) if "parent" in optional: isPost = DBconnector.select_query("SELECT Posts.id FROM Posts JOIN Threads ON Threads.id = Posts.thread " "WHERE Posts.id = %s AND Threads.id = %s", (optional["parent"], thread, )) if len(isPost) == 0: raise Exception("No post with id = " + optional["parent"]) update_thread_posts = "UPDATE Threads SET posts = posts + 1 WHERE id = %s" query = "INSERT INTO Posts (message, user, forum, thread, date" values = "(%s, %s, %s, %s, %s" parameters = [message, user, forum, thread, date] for param in optional: query += ", " + param values += ", %s" parameters.append(optional[param]) query += ") VALUES " + values + ")" DBconnector.update_query(update_thread_posts, (thread, )) post_id = DBconnector.update_query(query, parameters) post = DBconnector.select_query("SELECT date, dislikes, forum, id, isApproved, isDeleted, isEdited, " \ "isHighlighted, isSpam, likes, message, parent, points, thread, user " \ "FROM Posts WHERE id = %s", (post_id, )) post = views.post_format(post) del post["dislikes"] del post["likes"] del post["parent"] del post["points"] except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": post}), content_type='application/json') else: return HttpResponse(status=405)
def clear(request): try: response = DBconnector.clear() except Exception as e: return HttpResponse(json.dumps({"code": 1, "response": (e.message)}), content_type='application/json') return HttpResponse(json.dumps({"code": 0, "response": response}), content_type='application/json')