from flask import render_template, Blueprint, request

from app.utils.search import MySQLClient
from app.utils.preprocessor import TextPreprocessor


mainbp = Blueprint("main", __name__)


@mainbp.route("/search", methods=["GET"])
@mainbp.route("/", methods=["GET"])
def home():
    stores_by_page = 10
    topic = request.args.get("topic")
    category = request.args.get("category")
    daily_visitors = request.args.get("dailyvisitors")
    alexa_rank = request.args.get("alexarank")
    page = request.args.get("page") or 0

    if all([topic is None, category is None, daily_visitors is None, alexa_rank is None]):
        stores = MySQLClient.random_stores(page * stores_by_page, stores_by_page)
    else:
        stores = MySQLClient.search_stores(category, daily_visitors, alexa_rank, topic, page * stores_by_page, stores_by_page)

    stores = [
        {
            "url": store.url,
            "description": TextPreprocessor.clean_str(store.description),
            "title": TextPreprocessor.clean_str(store.title),
            "alexa_rank": store.alexa_rank,
            "category": store.category,
            "average_product_price": store.average_product_price,
            "daily_visitors": store.daily_visitors
        } for store in stores
    ]

    return render_template("search/index.html", stores=stores)


@mainbp.route("/search/topics", methods=["GET"])
def search_topics():
    substring = request.args.get("q")
    return [
        {
            "id": topic.id,
            "text": topic.text
        } for topic in MySQLClient.search_topic_by_substring(substring)
    ]



[((159, 186), 'flask.Blueprint', 'Blueprint', (['"""main"""', '__name__'], {}), "('main', __name__)\n", (168, 186), False, 'from flask import render_template, Blueprint, request\n'), ((315, 340), 'flask.request.args.get', 'request.args.get', (['"""topic"""'], {}), "('topic')\n", (331, 340), False, 'from flask import render_template, Blueprint, request\n'), ((356, 384), 'flask.request.args.get', 'request.args.get', (['"""category"""'], {}), "('category')\n", (372, 384), False, 'from flask import render_template, Blueprint, request\n'), ((406, 439), 'flask.request.args.get', 'request.args.get', (['"""dailyvisitors"""'], {}), "('dailyvisitors')\n", (422, 439), False, 'from flask import render_template, Blueprint, request\n'), ((457, 486), 'flask.request.args.get', 'request.args.get', (['"""alexarank"""'], {}), "('alexarank')\n", (473, 486), False, 'from flask import render_template, Blueprint, request\n'), ((1280, 1331), 'flask.render_template', 'render_template', (['"""search/index.html"""'], {'stores': 'stores'}), "('search/index.html', stores=stores)\n", (1295, 1331), False, 'from flask import render_template, Blueprint, request\n'), ((1420, 1441), 'flask.request.args.get', 'request.args.get', (['"""q"""'], {}), "('q')\n", (1436, 1441), False, 'from flask import render_template, Blueprint, request\n'), ((498, 522), 'flask.request.args.get', 'request.args.get', (['"""page"""'], {}), "('page')\n", (514, 522), False, 'from flask import render_template, Blueprint, request\n'), ((637, 701), 'app.utils.search.MySQLClient.random_stores', 'MySQLClient.random_stores', (['(page * stores_by_page)', 'stores_by_page'], {}), '(page * stores_by_page, stores_by_page)\n', (662, 701), False, 'from app.utils.search import MySQLClient\n'), ((729, 842), 'app.utils.search.MySQLClient.search_stores', 'MySQLClient.search_stores', (['category', 'daily_visitors', 'alexa_rank', 'topic', '(page * stores_by_page)', 'stores_by_page'], {}), '(category, daily_visitors, alexa_rank, topic, page *\n    stores_by_page, stores_by_page)\n', (754, 842), False, 'from app.utils.search import MySQLClient\n'), ((922, 967), 'app.utils.preprocessor.TextPreprocessor.clean_str', 'TextPreprocessor.clean_str', (['store.description'], {}), '(store.description)\n', (948, 967), False, 'from app.utils.preprocessor import TextPreprocessor\n'), ((990, 1029), 'app.utils.preprocessor.TextPreprocessor.clean_str', 'TextPreprocessor.clean_str', (['store.title'], {}), '(store.title)\n', (1016, 1029), False, 'from app.utils.preprocessor import TextPreprocessor\n'), ((1547, 1595), 'app.utils.search.MySQLClient.search_topic_by_substring', 'MySQLClient.search_topic_by_substring', (['substring'], {}), '(substring)\n', (1584, 1595), False, 'from app.utils.search import MySQLClient\n')]