def details(request): code = Code if request.method != 'GET': return HttpResponse( dumps({ 'code': code.NOT_VALID, 'response': 'request method should be GET' })) email = request.GET.get('user') if not email: return HttpResponse( dumps({ 'code': code.NOT_VALID, 'response': 'user not correct' })) response = get_detail_user(email) if response is not None: response = str(dumps({'code': code.OK, "response": response})) return HttpResponse(response) else: return HttpResponse( dumps({ 'code': code.NOT_FOUND, "response": "user doesnt exist" }))
def get_detail_forum(short_name, related): cursor = connection.cursor() code = Code try: query = get_query_forum_by_short_name(short_name) cursor.execute(query.get()) if not cursor.rowcount: cursor.close() return {'code': code.NOT_FOUND, 'response': 'forum not found'} except Exception as e: print str(e) cursor.close() return {'code': code.UNKNOWN_ERROR, 'response': 'failed select forum'} forum = cursor.fetchone() response = { "id": forum[0], "short_name": forum[1], "name": forum[2], "user": forum[3] } if related: if related != 'user': cursor.close() return {'code': code.NOT_CORRECT, 'response': 'incorrect related parameter: {}'.format(related)} response['user'] = get_detail_user(forum[3]) cursor.close() return {'code': code.OK, 'response': response}
def get_list_of_threads(data, related): if related is None: related = [] cursor = connection.cursor() code = Code if "since" in data: data["since"] = validate_date(data["since"]) try: if "forum" in related: has_forum = True else: has_forum = False query = get_query_list_threads(data, has_forum) cursor.execute(query.get()) if not cursor.rowcount: cursor.close() return {'code': code.OK, 'response': []} except Exception as e: print str(e) cursor.close() return { 'code': code.UNKNOWN_ERROR, 'response': 'failed select threads' } response = [] for thread in cursor.fetchall(): if has_forum: forum = { "id": thread[13], "name": thread[14], "short_name": thread[15], "user": thread[16] } else: forum = thread[11] if "user" in related: user = get_detail_user(thread[12]) else: user = thread[12] response.append({ "id": thread[0], "title": thread[1], "slug": thread[2], "message": thread[3], "date": str(thread[4]), "posts": thread[5], "likes": thread[6], "dislikes": thread[7], "points": thread[8], "isClosed": bool(thread[9]), "isDeleted": bool(thread[10]), "forum": forum, "user": user }) return {'code': code.OK, 'response': response}
def get_detail_thread(id_thread, related=None): if related is None: related = [] cursor = connection.cursor() code = Code try: if "forum" in related: has_forum = True else: has_forum = False query = get_query_detail_thread_by_id(id_thread, has_forum) cursor.execute(query.get()) if not cursor.rowcount: cursor.close() return {'code': code.NOT_FOUND, 'response': 'thread not found'} except Exception as e: print str(e) cursor.close() return {'code': code.UNKNOWN_ERROR, 'response': 'failed select thread'} thread = cursor.fetchone() response = { "id": thread[0], "title": thread[1], "slug": thread[2], "message": thread[3], "date": str(thread[4]), "posts": thread[5], "likes": thread[6], "dislikes": thread[7], "points": thread[8], "isClosed": bool(thread[9]), "isDeleted": bool(thread[10]), "forum": thread[11], "user": thread[12] } if "user" in related: response["user"] = get_detail_user(thread[12]) if "forum" in related: response["forum"] = { "name": thread[13], "short_name": thread[14], "user": thread[15], "id": thread[16] } cursor.close() return {'code': code.OK, 'response': response}
def update_user(data): cursor = connection.cursor() code = Code user = data["user"] try: query = get_query_update_user(user, data["about"], data["name"]) cursor.execute(query.get()) except Exception as e: print str(e) cursor.close() return { 'code': code.UNKNOWN_ERROR, "response": "update user delete failed" } response = get_detail_user(user) cursor.close() return response
def follow_user(follower, followee): cursor = connection.cursor() code = Code try: query = Query() query.add_insert("follow", (("follower", follower), ("following", followee))) cursor.execute(query.get()) except Exception as e: print str(e) cursor.close() return { 'code': code.NOT_FOUND, "response": "insert failed, user or thread not found" } cursor.close() response = get_detail_user(follower) return {'code': code.OK, "response": response}
def unfollow_user(follower, followee): cursor = connection.cursor() code = Code try: query = Query() query.add_delete("follow") query.add_where_condition( " follower = \"{}\" and following = \"{}\" ".format( follower, followee)) cursor.execute(query.get()) except Exception as e: print str(e) cursor.close() return { 'code': code.NOT_FOUND, "response": "delete failed, user not found" } cursor.close() response = get_detail_user(follower) return {'code': code.OK, "response": response}
def get_list_of_posts(data, related): if related is None: related = [] cursor = connection.cursor() code = Code if "since" in data: data["since"] = validate_date(data["since"]) try: if "forum" in related: has_forum = True else: has_forum = False if "thread" in related: has_thread = True else: has_thread = False query = get_query_list_posts_by_forum(data, has_forum, has_thread) cursor.execute(query.get()) if not cursor.rowcount: cursor.close() return {'code': code.OK, 'response': []} except Exception as e: print str(e) cursor.close() return {'code': code.UNKNOWN_ERROR, 'response': 'failed select posts'} response = [] for post in cursor.fetchall(): if has_forum: number_of_columns_of_forum = 0 forum = { "id": post[15], "name": post[16], "short_name": post[17], "user": post[18] } else: number_of_columns_of_forum = 4 forum = post[8] if has_thread: i = number_of_columns_of_forum thread = { "id": post[19 - i], "title": post[20 - i], "slug": post[21 - i], "message": post[22 - i], "date": str(post[23 - i]), "posts": post[24 - i], "likes": post[25 - i], "dislikes": post[26 - i], "points": post[27 - i], "isClosed": bool(post[28 - i]), "isDeleted": bool(post[29 - i]), "forum": post[30 - i], "user": post[31 - i] } else: thread = post[9] if "user" in related: user = get_detail_user(post[10]) else: user = post[10] response.append({ "id": post[0], "message": post[1], "date": str(post[2]), "isApproved": post[3], "isHighlighted": bool(post[4]), "isEdited": bool(post[5]), "isSpam": bool(post[6]), "isDeleted": bool(post[7]), "forum": forum, "thread": thread, "user": user, "dislikes": post[11], "likes": post[12], "points": post[13], "parent": post[14] }) return {'code': code.OK, 'response': response}
def get_detail_post(post, related=None): if related is None: related = [] cursor = connection.cursor() code = Code try: if "forum" in related: has_forum = True else: has_forum = False if "thread" in related: has_thread = True else: has_thread = False query = get_query_detail_post_by_id(post, has_forum, has_thread) cursor.execute(query.get()) if not cursor.rowcount: cursor.close() return {'code': code.NOT_FOUND, 'response': 'post not found'} except Exception as e: print str(e) cursor.close() return {'code': code.UNKNOWN_ERROR, 'response': 'failed select post'} post = cursor.fetchone() response = { "id": post[0], "message": post[1], "date": str(post[2]), "isApproved": post[3], "isHighlighted": bool(post[4]), "isEdited": bool(post[5]), "isSpam": bool(post[6]), "isDeleted": bool(post[7]), "forum": post[8], "thread": post[9], "user": post[10], "dislikes": post[11], "likes": post[12], "points": post[13], "parent": post[14] } if has_forum: dif = 0 else: dif = 4 if "forum" in related: response["forum"] = { "name": post[15], "short_name": post[16], "user": post[17], "id": post[18] } if "thread" in related: response["thread"] = { "id": post[19 - dif], "forum": post[20 - dif], "title": post[21 - dif], "isClosed": post[22 - dif], "user": post[23 - dif], "date": str(post[24 - dif]), "message": post[25 - dif], "slug": post[26 - dif], "isDeleted": post[27 - dif], "posts": post[28 - dif], "likes": post[29 - dif], "dislikes": post[30 - dif], "points": post[31 - dif] } if "user" in related: response["user"] = get_detail_user(response["user"]) cursor.close() return {'code': code.OK, 'response': response}