<commit_before># -*- coding: utf-8 -*-

from flask import current_app, json, request, Response
from sqlalchemy.sql.expression import and_
from sqlalchemy.sql import text

from web import app
from models import (RatebeerBeer, RatebeerBrewery, RbPolBeerMapping, PoletBeer,
                    PolStock, PolShop)


api_prefix = '/api/v1'


def get_limit():
    limit = request.args.get('limit', 10, type=int)
    return limit if limit <= 100 else 100


@app.route(api_prefix + '/search/autocomplete/')
def autocomplete_search():
    name = request.args.get('name', None)

    query = current_app.db_session.query(RatebeerBeer)\
        .join(PoletBeer)\
        .filter(RatebeerBeer.name.ilike('%' + name + '%'))\
        .limit(10)
    beer_list = [b.get_list_response() for b in query.all()]
    return Response(json.dumps(beer_list), content_type='application/json')


def get_int_arr_from_request(key):
    data = request.args.get(key, None)
    if data is not None:
        return [int(d) for d in data.split(',') if d != '']
    return []


@app.route(api_prefix + '/search/full/')
def full_search():
    query = current_app.db_session.query(RatebeerBeer)
    query = query.join(PoletBeer)

    name = request.args.get('name', None)
    if name is not None:
        query = query.filter(RatebeerBeer.name.ilike('%' + name + '%'))

    styles = get_int_arr_from_request('style')
    if len(styles) > 0:
        query = query.filter(RatebeerBeer.style_id.in_(styles))

    overall_score_limit = get_int_arr_from_request('overallScore')
    if len(overall_score_limit) == 2:
        query = query.filter(and_(
            RatebeerBeer.score_overall >= overall_score_limit[0],
            RatebeerBeer.score_overall <= overall_score_limit[1]
        ))

    style_score_limit = get_int_arr_from_request('styleScore')
    if len(style_score_limit) == 2:
        query = query.filter(and_(
            RatebeerBeer.score_style >= style_score_limit[0],
            RatebeerBeer.score_style <= style_score_limit[1]
        ))

    price_limit = get_int_arr_from_request('price')
    if len(price_limit) == 2:
        query = query.filter(and_(
            PoletBeer.price >= price_limit[0],
            PoletBeer.price <= price_limit[1]
        ))

    abv_limit = get_int_arr_from_request('abv')
    if len(abv_limit) == 2:
        query = query.filter(and_(
            RatebeerBeer.abv >= abv_limit[0],
            RatebeerBeer.abv <= abv_limit[1]
        ))

    pol = get_int_arr_from_request('pol')
    if len(pol) > 0:
        query = query.join(PolStock)\
            .filter(PolStock.shop_id.in_(pol))\
            .filter(PolStock.stock > 0)

    query = query.order_by(RatebeerBeer.name)

    beer_list = [b.get_list_response() for b in query.all()]
    return Response(json.dumps(beer_list), content_type='application/json')


@app.route(api_prefix + '/search/brewery/')
def search_brewery():
    query = request.args.get('q')
    db = current_app.db_session
    res = db.query(RatebeerBrewery).filter(
        RatebeerBrewery.name.ilike('%' + query + '%')
    )
    x = res.limit(get_limit()).all()<commit_msg>fix search on .5 abv<commit_after># -*- coding: utf-8 -*-

from flask import current_app, json, request, Response
from sqlalchemy.sql.expression import and_
from sqlalchemy.sql import text

from web import app
from models import (RatebeerBeer, RatebeerBrewery, RbPolBeerMapping, PoletBeer,
                    PolStock, PolShop)


api_prefix = '/api/v1'


def get_limit():
    limit = request.args.get('limit', 10, type=int)
    return limit if limit <= 100 else 100


@app.route(api_prefix + '/search/autocomplete/')
def autocomplete_search():
    name = request.args.get('name', None)

    query = current_app.db_session.query(RatebeerBeer)\
        .join(PoletBeer)\
        .filter(RatebeerBeer.name.ilike('%' + name + '%'))\
        .limit(10)
    beer_list = [b.get_list_response() for b in query.all()]
    return Response(json.dumps(beer_list), content_type='application/json')


def get_int_arr_from_request(key):
    data = request.args.get(key, None)
    if data is not None:
        return [int(d) for d in data.split(',') if d != '']
    return []


def get_float_arr_from_request(key):
    data = request.args.get(key, None)
    if data is not None:
        return [float(d) for d in data.split(',') if d != '']
    return []


@app.route(api_prefix + '/search/full/')
def full_search():
    query = current_app.db_session.query(RatebeerBeer)
    query = query.join(PoletBeer)

    name = request.args.get('name', None)
    if name is not None:
        query = query.filter(RatebeerBeer.name.ilike('%' + name + '%'))

    styles = get_int_arr_from_request('style')
    if len(styles) > 0:
        query = query.filter(RatebeerBeer.style_id.in_(styles))

    overall_score_limit = get_int_arr_from_request('overallScore')
    if len(overall_score_limit) == 2:
        query = query.filter(and_(
            RatebeerBeer.score_overall >= overall_score_limit[0],
            RatebeerBeer.score_overall <= overall_score_limit[1]
        ))

    style_score_limit = get_int_arr_from_request('styleScore')
    if len(style_score_limit) == 2:
        query = query.filter(and_(
            RatebeerBeer.score_style >= style_score_limit[0],
            RatebeerBeer.score_style <= style_score_limit[1]
        ))

    price_limit = get_int_arr_from_request('price')
    if len(price_limit) == 2:
        query = query.filter(and_(
            PoletBeer.price >= price_limit[0],
            PoletBeer.price <= price_limit[1]
        ))

    abv_limit = get_float_arr_from_request('abv')
    if len(abv_limit) == 2:
        query = query.filter(and_(
            RatebeerBeer.abv >= abv_limit[0],
            RatebeerBeer.abv <= abv_limit[1]
        ))

    pol = get_int_arr_from_request('pol')
    if len(pol) > 0:
        query = query.join(PolStock)\
            .filter(PolStock.shop_id.in_(pol))\
            .filter(PolStock.stock > 0)

    query = query.order_by(RatebeerBeer.name)

    beer_list = [b.get_list_response() for b in query.all()]
    return Response(json.dumps(beer_list), content_type='application/json')


@app.route(api_prefix + '/search/brewery/')
def search_brewery():
    query = request.args.get('q')
    db = current_app.db_session
    res = db.query(RatebeerBrewery).filter(
        RatebeerBrewery.name.ilike('%' + query + '%')
    )
    x = res.limit(get_limit()).all()
{'fraction_non_alphanumeric': 0.13278331257783313, 'fraction_numerical': 0.008561643835616438, 'mean_word_length': 3.030740276035132, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6216936', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2215, 'n_words': 406}