Пример #1
0
def handle_get():
    gid = get_param('id', type='uuid', required=True)
    include = get_param('include', type='enum+', container=PlaceIncludes.parse)

    place = get_place_by_gid(query_place(g.db, include), gid)
    if place is None:
        abort(response_error(NOT_FOUND_ERROR, 'place not found'))

    if include.area:
        load_areas(g.db, [place], include.area)

    if include.relationships:
        load_links(g.db, [place], include.relationships)

    return response_ok(place=serialize_place(place, include))
Пример #2
0
def get_artists_by_ids(db, ids, include):
    if not ids:
        return {}

    artists = query_artist(db, include).filter(Artist.id.in_(ids)).all()

    if include.areas:
        load_areas(db, artists, include.areas)

    if include.relationships:
        load_links(db, artists, include.relationships)

    results = {}
    for artist in artists:
        results[artist.id] = serialize_artist(artist, include)
    return results
Пример #3
0
def test_load_areas_simple(db):
    artist = db.query(Artist).filter_by(gid='95db1c7c-21b8-4956-82ad-20217cd5d395').\
        options(noload('area')).options(noload('begin_area')).one()
    load_areas(db, [artist], AreaIncludes.parse([]))

    assert_true(artist.area)
    assert_equal(artist.area.name, 'Canada')
    assert_false(hasattr(artist.area, 'part_of'))

    assert_true(artist.begin_area)
    assert_equal(artist.begin_area.name, 'New York')
    assert_false(hasattr(artist.begin_area, 'part_of'))

    assert_true(artist.end_area)
    assert_equal(artist.end_area.name, 'Montreal')
    assert_false(hasattr(artist.end_area, 'part_of'))
Пример #4
0
def handle_get():
    gid = get_param('id', type='uuid', required=True)
    include = get_param('include',
                        type='enum+',
                        container=ArtistIncludes.parse)

    artist = get_artist_by_gid(query_artist(g.db, include), gid)
    if artist is None:
        abort(response_error(NOT_FOUND_ERROR, 'artist not found'))

    if include.areas:
        load_areas(g.db, [artist], include.areas)

    if include.relationships:
        load_links(g.db, [artist], include.relationships)

    return response_ok(artist=serialize_artist(artist, include))
Пример #5
0
def test_load_areas_recursive(db):
    artist = db.query(Artist).filter_by(gid='95db1c7c-21b8-4956-82ad-20217cd5d395').\
        options(noload('area')).options(noload('begin_area')).one()
    load_areas(db, [artist], AreaIncludes.parse(['part_of']))

    assert_true(artist.area)
    assert_equal(artist.area.name, 'Canada')
    assert_false(artist.area.part_of)

    assert_true(artist.begin_area)
    assert_equal(artist.begin_area.name, 'New York')
    assert_true(artist.begin_area.part_of)
    assert_equal(artist.begin_area.part_of.name, 'United States')
    assert_false(artist.begin_area.part_of.part_of)

    assert_true(artist.end_area)
    assert_equal(artist.end_area.name, 'Montreal')
    assert_true(artist.end_area.part_of)
    assert_equal(artist.end_area.part_of.name, 'Quebec')
    assert_true(artist.end_area.part_of.part_of)
    assert_equal(artist.end_area.part_of.part_of.name, 'Canada')
    assert_false(artist.end_area.part_of.part_of.part_of)
Пример #6
0
def handle_search():
    query = get_param('query', type='text')
    include = get_param('include',
                        type='enum+',
                        container=ArtistIncludes.parse)

    page = get_search_params()
    options = prepare_search_options(page,
                                     fields='name^1.6 sort_name^1.1 alias')
    search_results = g.solr.select(query, fq='kind:artist', **options)

    # http://127.0.0.1:8983/solr/musicbrainz/select?q=gender:male&group=true&group.field=kind&group.limit=3&defType=edismax&qf=name&uf=*

    artist_ids = []
    scores = {}
    for result in search_results:
        id = int(result['id'].split(':', 1)[1])
        artist_ids.append(id)
        scores[id] = result['score']

    artists = query_artist(g.db, include).filter(Artist.id.in_(artist_ids))
    artist_by_id = {}
    for artist in artists:
        artist_by_id[artist.id] = artist

    if include.areas:
        load_areas(g.db, artist_by_id.values(), include.areas)

    data = []
    for id in artist_ids:
        data.append({
            'score': scores[id],
            'artist': serialize_artist(artist_by_id[id], include),
        })

    return response_ok(results=data,
                       page_info=prepare_page_info(search_results, page))