def _get_post(post): if post is None: return errors.not_found() if post.is_draft: user = get_user_from_request() if user is None: return errors.no_access() if post.creator != user: return errors.no_access() user = get_user_from_request() if post.blog is not None: # workaround, delete later. Sometime in the past you can save post # without blog, so this check will fail. has_access = Blog.has_access(post.blog, user) if not has_access: return errors.no_access() post_dict = post.to_json() post_dict = Vote.add_votes_info(post_dict, 3, user) entries = JamEntry.get_entries_for_post(post) post_dict["jam_entries"] = [e.to_json() for e in entries] return jsonify({"success": 1, "post": post_dict})
def posts(url): """Получить список постов для блога""" blog = Blog.get_or_none(Blog.url == url) if blog is None: return errors.not_found() user = get_user_from_request() has_access = Blog.has_access(blog, user) if not has_access: return errors.no_access() query = Post.get_posts_for_blog(blog) limit = max(1, min(int(request.args.get("limit") or 20), 100)) paginated_query = PaginatedQuery(query, paginate_by=limit) posts = [p.to_json() for p in paginated_query.get_object_list()] posts = [Vote.add_votes_info(p, 3, user) for p in posts] return jsonify( { "success": 1, "posts": posts, "meta": {"page_count": paginated_query.get_page_count()}, } )
def get_single_blog(url): """Получить блог по указанному url""" blog = Blog.get_or_none(Blog.url == url) if blog is None: return errors.not_found() user = get_user_from_request() has_access = Blog.has_access(blog, user) if not has_access: return errors.no_access() blog_dict = blog.to_json() blog_dict = Vote.add_votes_info(blog_dict, 2, user) return jsonify({"success": 1, "blog": blog_dict})
def readers(url): """Получить список читателей блога""" blog = Blog.get_or_none(Blog.url == url) if blog is None: return errors.not_foun user = get_user_from_request() has_access = Blog.has_access(blog, user) if not has_access: return errors.no_access() query = Blog.get_readers(blog) limit = max(1, min(int(request.args.get("limit") or 20), 100)) paginated_query = PaginatedQuery(query, paginate_by=limit) return jsonify( { "success": 1, "readers": [u.to_json() for u in paginated_query.get_object_list()], "meta": {"page_count": paginated_query.get_page_count()}, } )