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 create(request): data = request.body.decode('utf-8') data = json.loads(data) try: forum = ForumManager.create(data) resp = {"code": 0, "response": forum.serialize()} except: try: forum = ForumManager.get(data['short_name']) resp = {"code": 3, "response": forum.serialize()} except: resp = {"code": 3, "response": "This user doesn't exist"} 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 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 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")