def update(request): data = request.body.decode("utf-8") data = json.loads(data) id = data["post"] message = data["message"] sql = "UPDATE Post set message = '%s' where id = %d" % (message, id) sql_update(sql) post = PostManager.get(id) resp = {"code": 0, "response": post.serialize()} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json")
def remove(request): data = request.body.decode("utf-8") data = json.loads(data) id = data["post"] sql = "UPDATE Post set is_deleted = TRUE where id=%d" % id sql_update(sql) post = PostManager.get(id) sql = "UPDATE Thread set posts = posts - 1 where id = %d" % post.thread sql_update(sql) resp = {"code": 0, "response": {"post": id}} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json")
def create(request): data = request.body.decode("utf-8") data = json.loads(data) try: post = PostManager.create(data) sql = "UPDATE Thread set posts = posts + 1 where id = %d" % post.thread sql_update(sql) resp = {"code": 0, "response": post.serialize()} except: resp = {"code": 3, "response": "Incorrect request"} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json")
def vote(request): data = request.body.decode("utf-8") data = json.loads(data) post = data["post"] vote = data["vote"] if vote == -1: sql = "UPDATE Post set dislikes = dislikes + 1 where id=%s" % post else: sql = "UPDATE Post set likes = likes + 1 where id=%s" % post sql_update(sql) post = PostManager.get(post) resp = {"code": 0, "response": post.serialize()} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json")
def details(request): id = request.GET.get("post") try: post = PostManager.get(id) except: resp = {"code": 1, "response": "something shit"} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json") resp = {"code": 0, "response": post.serialize()} related = request.GET.getlist("related") if "user" in related: resp["response"]["user"] = UserManager.get(resp["response"]["user"]).serialize() if "forum" in related: resp["response"]["forum"] = ForumManager.get(resp["response"]["forum"]).serialize() if "thread" in related: resp["response"]["thread"] = ThreadManager.get(resp["response"]["thread"]).serialize() resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json")