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')

[((293, 324), 'flask.Blueprint', 'Blueprint', (['"""frontend"""', '__name__'], {}), "('frontend', __name__)\n", (302, 324), False, 'from flask import Blueprint, render_template\n'), ((408, 432), 'critiquebrainz.db.review.get_popular', 'db_review.get_popular', (['(6)'], {}), '(6)\n', (429, 432), True, 'import critiquebrainz.db.review as db_review\n'), ((708, 755), 'critiquebrainz.db.review.list_reviews', 'db_review.list_reviews', ([], {'sort': '"""created"""', 'limit': '(9)'}), "(sort='created', limit=9)\n", (730, 755), True, 'import critiquebrainz.db.review as db_review\n'), ((911, 1071), 'flask.render_template', 'render_template', (['"""index/index.html"""'], {'popular_reviews': 'popular_reviews', 'recent_reviews': 'recent_reviews', 'reviews_total': 'review_count', 'users_total': 'user_count'}), "('index/index.html', popular_reviews=popular_reviews,\n    recent_reviews=recent_reviews, reviews_total=review_count, users_total=\n    user_count)\n", (926, 1071), False, 'from flask import Blueprint, render_template\n'), ((1145, 1180), 'flask.render_template', 'render_template', (['"""index/about.html"""'], {}), "('index/about.html')\n", (1160, 1180), False, 'from flask import Blueprint, render_template\n'), ((1246, 1286), 'flask.render_template', 'render_template', (['"""index/guidelines.html"""'], {}), "('index/guidelines.html')\n", (1261, 1286), False, 'from flask import Blueprint, render_template\n'), ((523, 567), 'markdown.markdown', 'markdown', (["review['text']"], {'safe_mode': '"""escape"""'}), "(review['text'], safe_mode='escape')\n", (531, 567), False, 'from markdown import markdown\n'), ((807, 842), 'critiquebrainz.db.review.get_count', 'db_review.get_count', ([], {'is_draft': '(False)'}), '(is_draft=False)\n', (826, 842), True, 'import critiquebrainz.db.review as db_review\n'), ((875, 897), 'critiquebrainz.db.users.total_count', 'db_users.total_count', ([], {}), '()\n', (895, 897), True, 'import critiquebrainz.db.users as db_users\n'), ((604, 641), 'bs4.BeautifulSoup', 'BeautifulSoup', (['preview', '"""html.parser"""'], {}), "(preview, 'html.parser')\n", (617, 641), False, 'from bs4 import BeautifulSoup\n')]