Exemple #1
0
def index(hearing_id):
    hearing = Hearing.query.get_or_404(hearing_id)

    # Fetching comments.
    comments = (
        hearing
        .all_comments
        .options(db.joinedload(Comment.comment))
        .options(db.joinedload(Comment.image))
        .options(db.joinedload(Comment.alternative))
        .options(db.joinedload(Comment.section))
    )

    if not (
        current_user.is_authenticated() and
        (current_user.is_official or current_user.is_admin)
    ):
        comments = comments.filter_by(is_hidden=False)

    # Pagination.
    page = request.args.get('page', 1, type=int)
    per_page = request.args.get('per_page', 20, type=int)
    order_by = request.args.get('order_by', 'created_at')

    if order_by == 'like_count':
        comments = (
            comments
            .filter(Comment.like_count > 0)
            .order_by(
                db.desc(Comment.like_count),
                db.desc(Comment.id)
            )
        )
    else:
        comments = comments.order_by(db.desc(Comment.created_at))

    pagination = comments.paginate(page, per_page)

    # Serialization
    serialized = CommentSchema(
        pagination.items,
        exclude=('object_type', 'object_id'),
        many=True
    )

    return jsonify({
        'comments': serialized.data,
        'page': page,
        'per_page': per_page
    }), 200
def index():
    featured_hearing = (
        Hearing.query
        .filter(Hearing.is_open)
        .order_by(db.asc(Hearing.closes_at))
        .limit(1)
        .first()
    )

    latest_hearings = (
        Hearing.query
        .filter(Hearing.published)
        .order_by(db.desc(Hearing.opens_at))
        .limit(40)
    )

    return render_template(
        'frontpage/index.html',
        featured_hearing=featured_hearing,
        latest_hearings=latest_hearings,
    )