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



[((7, 9, 7, 36), 'flask.Blueprint', 'Blueprint', ({(7, 19, 7, 25): '"""main"""', (7, 27, 7, 35): '__name__'}, {}), "('main', __name__)", False, 'from flask import render_template, Blueprint, request\n'), ((14, 12, 14, 37), 'flask.request.args.get', 'request.args.get', ({(14, 29, 14, 36): '"""topic"""'}, {}), "('topic')", False, 'from flask import render_template, Blueprint, request\n'), ((15, 15, 15, 43), 'flask.request.args.get', 'request.args.get', ({(15, 32, 15, 42): '"""category"""'}, {}), "('category')", False, 'from flask import render_template, Blueprint, request\n'), ((16, 21, 16, 54), 'flask.request.args.get', 'request.args.get', ({(16, 38, 16, 53): '"""dailyvisitors"""'}, {}), "('dailyvisitors')", False, 'from flask import render_template, Blueprint, request\n'), ((17, 17, 17, 46), 'flask.request.args.get', 'request.args.get', ({(17, 34, 17, 45): '"""alexarank"""'}, {}), "('alexarank')", False, 'from flask import render_template, Blueprint, request\n'), ((37, 11, 37, 62), 'flask.render_template', 'render_template', (), '', False, 'from flask import render_template, Blueprint, request\n'), ((42, 16, 42, 37), 'flask.request.args.get', 'request.args.get', ({(42, 33, 42, 36): '"""q"""'}, {}), "('q')", False, 'from flask import render_template, Blueprint, request\n'), ((18, 11, 18, 35), 'flask.request.args.get', 'request.args.get', ({(18, 28, 18, 34): '"""page"""'}, {}), "('page')", False, 'from flask import render_template, Blueprint, request\n'), ((21, 17, 21, 81), 'app.utils.search.MySQLClient.random_stores', 'MySQLClient.random_stores', ({(21, 43, 21, 64): 'page * stores_by_page', (21, 66, 21, 80): 'stores_by_page'}, {}), '(page * stores_by_page, stores_by_page)', False, 'from app.utils.search import MySQLClient\n'), ((23, 17, 23, 126), 'app.utils.search.MySQLClient.search_stores', 'MySQLClient.search_stores', ({(23, 43, 23, 51): 'category', (23, 53, 23, 67): 'daily_visitors', (23, 69, 23, 79): 'alexa_rank', (23, 81, 23, 86): 'topic', (23, 88, 23, 109): 'page * stores_by_page', (23, 111, 23, 125): 'stores_by_page'}, {}), '(category, daily_visitors, alexa_rank, topic, page *\n    stores_by_page, stores_by_page)', False, 'from app.utils.search import MySQLClient\n'), ((28, 27, 28, 72), 'app.utils.preprocessor.TextPreprocessor.clean_str', 'TextPreprocessor.clean_str', ({(28, 54, 28, 71): 'store.description'}, {}), '(store.description)', False, 'from app.utils.preprocessor import TextPreprocessor\n'), ((29, 21, 29, 60), 'app.utils.preprocessor.TextPreprocessor.clean_str', 'TextPreprocessor.clean_str', ({(29, 48, 29, 59): 'store.title'}, {}), '(store.title)', False, 'from app.utils.preprocessor import TextPreprocessor\n'), ((47, 23, 47, 71), 'app.utils.search.MySQLClient.search_topic_by_substring', 'MySQLClient.search_topic_by_substring', ({(47, 61, 47, 70): 'substring'}, {}), '(substring)', False, 'from app.utils.search import MySQLClient\n')]