def details(request): email = request.GET.get('user') try: user = UserManager.get(email) resp = {"code": 0, "response": user.serialize()} except: resp = {"code": 1, "response": "User with this email doesn't exist"} 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: user = UserManager.create(data) resp = {"code": 0, "response": user.serialize()} except: resp = {"code": 5, "response": "User with this email already exists"} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json")
def details(request): short_name = request.GET.get('forum') try: forum = ForumManager.get(short_name) resp = {"code": 0, "response": forum.serialize()} if request.GET.get('related') == "user": resp['response']['user'] = UserManager.get(resp['response']['user']).serialize() except: resp = {"code": 1, "response": "Forum with this short_name doesn't exist"} resp = json.dumps(resp) return HttpResponse(resp, content_type='application/json')
def updateProfile(request): data = request.body.decode('utf-8') data = json.loads(data) email = data['user'] about = data['about'] name = data['name'] sql = "UPDATE User set about = '%s', name = '%s' where email = '%s'" % (about, name, email) sql_update(sql) sql = "UPDATE Post set name = %s where user = '******'" % ("'" + name + "'" if name != None else "NULL", email) sql_update(sql) resp = {"code": 0, "response": UserManager.get(email).serialize()} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json")
def unfollow(request): data = request.body.decode('utf-8') data = json.loads(data) follower = data['follower'] followee = data['followee'] sql = "Update Follow set is_deleted=1 where follower='%s' and followee='%s'" % (follower, followee) try: id = sql_insert(sql, 'Follow') except: resp = {"code": 3, "response": "One of this users doesn't exist"} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json") resp = {"code": 0, "response": UserManager.get(follower).serialize()} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json")
def details(request): id = request.GET.get('thread') thread = ThreadManager.get(id) resp = {"code": 0, "response": thread.serialize()} related = request.GET.getlist('related') if "thread" in related: resp = {"code": 3, "response": "Incorrect request"} resp = json.dumps(resp) return HttpResponse(resp, content_type='application/json') 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() resp = json.dumps(resp) return HttpResponse(resp, content_type='application/json')
def follow(request): data = request.body.decode('utf-8') data = json.loads(data) follower = data['follower'] followee = data['followee'] sql = 'INSERT INTO Follow(follower,followee,is_deleted) VALUES(' sql += "'" + follower + "', " sql += "'" + followee + "', " sql += '0)' try: id = sql_insert(sql, 'Follow') except: resp = {"code": 3, "response": "One of this users doesn't exist"} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json") resp = {"code": 0, "response": UserManager.get(follower).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")
def listThreads(request): forum = request.GET.get('forum') since = request.GET.get('since') limit = request.GET.get('limit') order = request.GET.get('order') related = request.GET.getlist('related') sql = "SELECT * from Thread " sql += "where forum='%s' " % forum if since is not None: sql += "and date>='%s' " % since if order is not None: if order == "asc": sql += "order by date ASC " else: sql += "order by date DESC " if limit is not None: sql += "limit %s" % limit result = sql_select(sql) if result == (): resp = {"code": 0, "response": []} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json") threads = [] if "forum" in related: forum = ForumManager.get(forum) for data in result: params = {'forum': data[1], 'user': data[2], 'title': data[3], 'message': data[4], 'slug': data[5], 'date': data[6].strftime("%Y-%m-%d %H:%M:%S"), 'isClosed': data[7], 'isDeleted': data[8]} thread = Thread(params) thread.id = data[0] thread.likes = data[9] thread.dislikes = data[10] thread.posts = data[11] thread.points = thread.likes - thread.dislikes serialized_thread = thread.serialize() if "user" in related: user = UserManager.get(thread.user) serialized_thread["user"] = user.serialize() if "forum" in related: serialized_thread["forum"] = forum.serialize() threads.append(serialized_thread) resp = {"code": 0, "response": threads} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json")
def save(self): if self.id == None: user = UserManager.get(self.user) sql = "INSERT INTO Post(user,thread,forum,message,date,is_approved,is_highlighted,is_edited,is_spam,is_deleted,path,user_id,name) VALUES(" \ "'%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, %d, '%s', %d, %s)" % ( self.user, self.thread, self.forum, self.message, self.date, self.is_approved, self.is_highlighted, self.is_edited, self.is_spam, self.is_deleted, self.path, user.id,"'" + user.name + "'" if user.name != None else "NULL") id = sql_insert(sql, 'User') self.id = id if self.path == None: sql = "UPDATE Post set path = '%s' where id = %s" % (Converter.from_dec_to_hex(int(self.id)), self.id) sql_update(sql) self.path = self.id else: sql = "UPDATE Post set path = '%s' where id = %s" % (self.path+Converter.from_dec_to_hex(int(self.id)), self.id) sql_update(sql) self.path = self.path+str(self.id) return self else: return self
def listPosts(request): forum = request.GET.get('forum') since = request.GET.get('since') limit = request.GET.get('limit') order = request.GET.get('order') related = request.GET.getlist('related') sql = "SELECT * from Post " if "thread" in related: sql += "join Thread on Post.thread=Thread.id " sql += "where Post.forum='%s' " % forum if since is not None: sql += "and Post.date>='%s' " % since if order is not None: if order == "asc": sql += "order by Post.date ASC " else: sql += "order by Post.date DESC " if limit is not None: sql += "limit %s" % limit result = sql_select(sql) if result == (): resp = {"code": 0, "response": []} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json") resultset = [] if "forum" in related: forum = ForumManager.get(forum) for data in result: params = {'user': data[1], 'thread': data[2], 'forum': data[3], 'date': data[4].strftime("%Y-%m-%d %H:%M:%S"), 'message': data[5], 'isApproved': data[6], 'isHighlighted': data[7], 'isEdited': data[8], 'isSpam': data[9], 'isDeleted': data[10], } post = Post(params) post.id = data[0] post.likes = data[11] post.dislikes = data[12] post.points = post.likes - post.dislikes post.parent = Converter.from_hex_to_dec(data[13][-10:-5]) if data[13] != Converter.from_dec_to_hex( int(post.id)) else None serialized_post = post.serialize() if "thread" in related: params = {'forum': data[17], 'user': data[18], 'title': data[19], 'message': data[20], 'slug': data[21], 'date': data[22].strftime("%Y-%m-%d %H:%M:%S"), 'isClosed': data[23], 'isDeleted': data[24]} thread = Thread(params) thread.id = data[16] thread.likes = data[25] thread.dislikes = data[26] thread.posts = data[27] thread.points = thread.likes - thread.dislikes serialized_post["thread"] = thread.serialize() if "user" in related: user = UserManager.get(post.user) serialized_post["user"] = user.serialize() if "forum" in related: serialized_post["forum"] = forum.serialize() resultset.append(serialized_post) resp = {"code": 0, "response": resultset} resp = json.dumps(resp) return HttpResponse(resp, content_type="application/json")