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, 'flask.Blueprint', 'Blueprint', False, 'from flask import Blueprint, render_template\n'), (16, 'critiquebrainz.db.review.get_popular', 'db_review.get_popular', True, 'import critiquebrainz.db.review as db_review\n'), (23, 'critiquebrainz.db.review.list_reviews', 'db_review.list_reviews', True, 'import critiquebrainz.db.review as db_review\n'), (29, 'flask.render_template', 'render_template', False, 'from flask import Blueprint, render_template\n'), (35, 'flask.render_template', 'render_template', False, 'from flask import Blueprint, render_template\n'), (40, 'flask.render_template', 'render_template', False, 'from flask import Blueprint, render_template\n'), (19, 'markdown.markdown', 'markdown', False, 'from markdown import markdown\n'), (26, 'critiquebrainz.db.review.get_count', 'db_review.get_count', True, 'import critiquebrainz.db.review as db_review\n'), (27, 'critiquebrainz.db.users.total_count', 'db_users.total_count', True, 'import critiquebrainz.db.users as db_users\n'), (20, 'bs4.BeautifulSoup', 'BeautifulSoup', False, 'from bs4 import BeautifulSoup\n')]