def getDetailedForum(short_name, related): cursor = connection.cursor() try: cursor.execute( "SELECT " "forums.id AS id, " "forums.name AS name, " "forums.short_name AS short_name, " "users.email AS user " "FROM forums JOIN users ON forums.user_id = users.id " "WHERE forums.short_name = %s """, (short_name,) ) forum = dictfetchone(cursor) cursor.close() if not forum: raise Exception("Forum doesn't exists") if 'user' in related: from API.requests import user_requests forum['user'] = user_requests.getDetailedUser(forum['user']) except Exception: raise else: return forum finally: cursor.close()
def getThreadsList(short_name, since, order, limit, related): cursor = connection.cursor() try: query = ( "SELECT " "threads.date AS date, " "threads.dislikes AS dislikes, " "forums.short_name AS forum, " "threads.id AS id, " "threads.isClosed AS isClosed, " "threads.isDeleted AS isDeleted, " "threads.likes AS likes, " "threads.message AS message, " "threads.likes - threads.dislikes AS points, " "COUNT(posts.id) AS posts, " "threads.slug AS slug, " "threads.title AS title, " "users.email AS user " "FROM forums LEFT JOIN threads ON forums.id = threads.forum_id " "JOIN users ON threads.user_id = users.id " "LEFT JOIN posts ON posts.thread_id = threads.id " "WHERE forums.short_name= %r " % str(short_name)) if since: query += "AND threads.date >= %r " % str(since) query += "GROUP BY forums.short_name, threads.slug " if order: query += "ORDER BY threads.date %s " % order if limit: query += "LIMIT %s " % limit cursor.execute(query) threads = dictfetchall(cursor) cursor.close() for thread in threads: thread['date'] = thread['date'].strftime("%Y-%m-%d %H:%M:%S") thread['isClosed'] = bool(thread['isClosed']) thread['isDeleted'] = bool(thread['isDeleted']) if 'user' in related: for thread in threads: from API.requests import user_requests thread['user'] = user_requests.getDetailedUser(thread['user']) if 'forum' in related: for thread in threads: thread['forum'] = getDetailedForum(thread['forum'], []) except Exception: raise else: return threads finally: cursor.close()
def getDetailedPost(id, related): cursor = connection.cursor() try: cursor.execute( "SELECT " "posts.date AS date, " "posts.dislikes AS dislikes, " "forums.short_name AS forum, " "posts.id AS id, " "posts.isApproved AS isApproved, " "posts.isDeleted AS isDeleted, " "posts.isEdited AS isEdited, " "posts.isHighlighted AS isHighlighted, " "posts.isSpam AS isSpam, " "posts.likes AS likes, " "posts.message AS message, " "posts.post_id AS parent, " "posts.likes - posts.dislikes AS points, " "posts.thread_id AS thread, " "users.email AS user " "FROM posts RIGHT JOIN threads ON posts.thread_id = threads.id " "RIGHT JOIN forums ON threads.forum_id = forums.id " "JOIN users ON posts.user_id = users.id " "WHERE posts.id = %s """, (id,)) post = dictfetchone(cursor) cursor.close() if not post: raise Exception("Post doesn't exist") if 'user' in related: from API.requests import user_requests post['user'] = user_requests.getDetailedUser(post['user']) if 'thread' in related: from API.requests import thread_requests post['thread'] = thread_requests.getDetailedThread(post['thread'],[]) if 'forum' in related: from API.requests import forum_requests post['forum'] = forum_requests.getDetailedForum(post['forum'], []) post['isApproved'] = bool(post['isApproved']) post['isDeleted'] = bool(post['isDeleted']) post['isEdited'] = bool(post['isEdited']) post['isHighlighted'] = bool(post['isHighlighted']) post['isSpam'] = bool(post['isSpam']) post['date'] = post['date'].strftime("%Y-%m-%d %H:%M:%S") except Exception: raise else: return post finally: cursor.close()
def details(request): try: parameters = request.GET.dict() validate_required_parameters(parameters, ['user']) user = user_requests.getDetailedUser(parameters['user']) response_json = { 'code': 0, 'response': user, } except Exception as e: response_json = { 'code': 1, 'response': str(e), } return HttpResponse(json.dumps(response_json, ensure_ascii=False), content_type='application/json')
def getDetailedThread(id, related): cursor = connection.cursor() try: cursor.execute( "SELECT " "threads.date AS date, " "threads.dislikes AS dislikes, " "forums.short_name AS forum, " "threads.id AS id, " "threads.isClosed AS isClosed, " "threads.isDeleted AS isDeleted, " "threads.likes AS likes, " "threads.message AS message, " "threads.likes - threads.dislikes AS points, " "COUNT(posts.id) AS posts, " "threads.slug AS slug, " "threads.title AS title, " "users.email AS user " "FROM forums JOIN threads ON forums.id = threads.forum_id " "JOIN users ON threads.user_id = users.id " "JOIN posts ON posts.thread_id = threads.id " "WHERE threads.id = %s" "", (id,), ) thread = dictfetchone(cursor) cursor.close() thread["isClosed"] = bool(thread["isClosed"]) thread["isDeleted"] = bool(thread["isDeleted"]) thread["date"] = thread["date"].strftime("%Y-%m-%d %H:%M:%S") if "user" in related: from API.requests import user_requests thread["user"] = user_requests.getDetailedUser(thread["user"]) if "forum" in related: from API.requests import forum_requests thread["forum"] = forum_requests.getDetailedForum(thread["forum"], []) except Exception: raise else: return thread finally: cursor.close()
def follow(request): try: parameters = json.loads(request.body, encoding='utf-8') validate_required_parameters(parameters, ['follower', 'followee']) user_requests.followUser(parameters['follower'], parameters['followee']) user = user_requests.getDetailedUser(parameters['follower']) response_json = { 'code': 0, 'response': user, } except Exception as e: response_json = { 'code': 1, 'response': str(e), } return HttpResponse(json.dumps(response_json, ensure_ascii=False), content_type='application/json')
def getPostsList(short_name, since, order, limit, related): cursor = connection.cursor() try: query = ( "SELECT " "posts.date AS date, " "posts.dislikes AS dislikes, " "forums.short_name AS forum, " "posts.id AS id, " "posts.isApproved AS isApproved, " "posts.isDeleted AS isDeleted, " "posts.isEdited AS isEdited, " "posts.isHighlighted AS isHighlighted, " "posts.isSpam AS isSpam, " "posts.likes AS likes, " "posts.message AS message, " "posts.post_id as parent, " "posts.likes - posts.dislikes AS points, " "threads.id AS thread, users.email AS user " "FROM posts RIGHT JOIN threads ON posts.thread_id = threads.id " "RIGHT JOIN forums ON threads.forum_id = forums.id " "JOIN users ON posts.user_id = users.id " "WHERE forums.short_name=%r " % (str(short_name))) if since: query += "AND posts.date >= %r " % str(since) if order: query += "ORDER BY posts.date %s " % order if limit: query += "LIMIT %s " % limit cursor.execute(query) posts = dictfetchall(cursor) cursor.close() for post in posts: post['isApproved'] = bool(post['isApproved']) post['isDeleted'] = bool(post['isDeleted']) post['isEdited'] = bool(post['isEdited']) post['isHighlighted'] = bool(post['isHighlighted']) post['isSpam'] = bool(post['isSpam']) post['date'] = post['date'].strftime("%Y-%m-%d %H:%M:%S") if 'user' in related: for post in posts: from API.requests import user_requests post['user'] = user_requests.getDetailedUser(post['user']) if 'thread' in related: for post in posts: from API.requests import thread_requests post['thread'] = thread_requests.getDetailedThread(post['thread'], []) if 'forum' in related: for post in posts: post['forum'] = getDetailedForum(post['forum'], []) except Exception: raise else: return posts finally: cursor.close()