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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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)