コード例 #1
0
def community_search():
    db_query = Community.select(Community.name, Community.description)

    search_query = request.args.get('q')
    if search_query:
        db_query = db_query.where(Community.prefix_name.contains(search_query))

    db_query = db_query.order_by(Community.name).limit(30)

    # hits = [model_to_dict(hit, recurse=True) for hit in db_query]

    result = []
    for hit in db_query:
        result.append({
            "id": hit.name_with_prefix,
            "text": hit.name_with_prefix
        })

    feeds = [{
        "id": "",
        "text": "Frontpage"
    }, {
        "id": "f/popular",
        "text": "Popular"
    }, {
        "id": "f/new",
        "text": "New"
    }, {
        "id": "f/upvote",
        "text": "Most upvoted"
    }]

    # print(json.dumps({"suggestions": feeds + hits}, indent=4))

    if request.args.get('s'):
        return jsonify({"results": result})

    return jsonify({
        "results": [{
            "text": "Feeds",
            "children": feeds,
        }, {
            "text": "Other",
            "children": result,
        }]
    })