from flask import Blueprint, render_template
from flask_babel import format_number
import critiquebrainz.db.users as db_users
import critiquebrainz.db.review as db_review
from bs4 import BeautifulSoup
from markdown import markdown

DEFAULT_CACHE_EXPIRATION = 10 * 60  # seconds

frontend_bp = Blueprint('frontend', __name__)


@frontend_bp.route('/')
def index():
    # Popular reviews
    popular_reviews = db_review.get_popular(6)
    for review in popular_reviews:
        # Preparing text for preview
        preview = markdown(review['text'], safe_mode="escape")
        review['preview'] = ''.join(BeautifulSoup(preview, "html.parser").findAll(text=True))

    # Recent reviews
    recent_reviews, _ = db_review.list_reviews(sort='created', limit=9)

    # Statistics
    review_count = format_number(db_review.get_count(is_draft=False))
    user_count = format_number(db_users.total_count())

    return render_template('index/index.html', popular_reviews=popular_reviews, recent_reviews=recent_reviews,
                           reviews_total=review_count, users_total=user_count)


@frontend_bp.route('/about')
def about():
    return render_template('index/about.html')


@frontend_bp.route('/guidelines')
def guidelines():
    return render_template('index/guidelines.html')

[((10, 14, 10, 45), 'flask.Blueprint', 'Blueprint', ({(10, 24, 10, 34): '"""frontend"""', (10, 36, 10, 44): '__name__'}, {}), "('frontend', __name__)", False, 'from flask import Blueprint, render_template\n'), ((16, 22, 16, 46), 'critiquebrainz.db.review.get_popular', 'db_review.get_popular', ({(16, 44, 16, 45): '6'}, {}), '(6)', True, 'import critiquebrainz.db.review as db_review\n'), ((23, 24, 23, 71), 'critiquebrainz.db.review.list_reviews', 'db_review.list_reviews', (), '', True, 'import critiquebrainz.db.review as db_review\n'), ((29, 11, 30, 78), 'flask.render_template', 'render_template', (), '', False, 'from flask import Blueprint, render_template\n'), ((35, 11, 35, 46), 'flask.render_template', 'render_template', ({(35, 27, 35, 45): '"""index/about.html"""'}, {}), "('index/about.html')", False, 'from flask import Blueprint, render_template\n'), ((40, 11, 40, 51), 'flask.render_template', 'render_template', ({(40, 27, 40, 50): '"""index/guidelines.html"""'}, {}), "('index/guidelines.html')", False, 'from flask import Blueprint, render_template\n'), ((19, 18, 19, 62), 'markdown.markdown', 'markdown', (), '', False, 'from markdown import markdown\n'), ((26, 33, 26, 68), 'critiquebrainz.db.review.get_count', 'db_review.get_count', (), '', True, 'import critiquebrainz.db.review as db_review\n'), ((27, 31, 27, 53), 'critiquebrainz.db.users.total_count', 'db_users.total_count', ({}, {}), '()', True, 'import critiquebrainz.db.users as db_users\n'), ((20, 36, 20, 73), 'bs4.BeautifulSoup', 'BeautifulSoup', ({(20, 50, 20, 57): 'preview', (20, 59, 20, 72): '"""html.parser"""'}, {}), "(preview, 'html.parser')", False, 'from bs4 import BeautifulSoup\n')]