Skip to content

Commit

Permalink
Always return type information without an include parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
alastair committed Mar 15, 2022
1 parent b6a8346 commit a26ea01
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 51 deletions.
13 changes: 3 additions & 10 deletions brainzutils/musicbrainz_db/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ def fetch_multiple_artists(mbids, includes=None):
check_includes('artist', includes)

with mb_session() as db:
query = db.query(models.Artist)

if 'type' in includes:
query = query.options(joinedload('type'))
query = db.query(models.Artist).options(joinedload('type'))

artists = get_entities_by_gids(
query=query,
Expand All @@ -74,9 +71,5 @@ def fetch_multiple_artists(mbids, includes=None):
includes_data=includes_data,
)

if 'type' in includes:
for artist in artists.values():
includes_data[artist.id]['type'] = artist.type

artists = {str(mbid): serialize_artists(artist, includes_data[artist.id]) for mbid, artist in artists.items()}
return artists
artists = {str(mbid): serialize_artists(artist, includes_data[artist.id]) for mbid, artist in artists.items()}
return artists
3 changes: 2 additions & 1 deletion brainzutils/musicbrainz_db/event.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import defaultdict
from sqlalchemy.orm import joinedload
from mbdata import models
from brainzutils.musicbrainz_db import mb_session
from brainzutils.musicbrainz_db.utils import get_entities_by_gids
Expand Down Expand Up @@ -42,7 +43,7 @@ def fetch_multiple_events(mbids, includes=None):
includes_data = defaultdict(dict)
check_includes('event', includes)
with mb_session() as db:
query = db.query(models.Event)
query = db.query(models.Event).options(joinedload('type'))
events = get_entities_by_gids(
query=query,
entity_type='event',
Expand Down
2 changes: 1 addition & 1 deletion brainzutils/musicbrainz_db/includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'release': [
"artists", "labels", "recordings", "release-groups", "media", "annotation", "aliases"
] + TAG_INCLUDES + RELATION_INCLUDES,
'artist': ["recordings", "releases", "media", "aliases", "annotation", "type"] + RELATION_INCLUDES + TAG_INCLUDES,
'artist': ["recordings", "releases", "media", "aliases", "annotation"] + RELATION_INCLUDES + TAG_INCLUDES,
'label': ["area", "aliases", "annotation"] + RELATION_INCLUDES + TAG_INCLUDES,
'work': ["artists", "recordings", "aliases", "annotation"] + RELATION_INCLUDES + TAG_INCLUDES,
'editor': [], # TODO: List includes here (BU-18)
Expand Down
6 changes: 1 addition & 5 deletions brainzutils/musicbrainz_db/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,4 @@ def fetch_multiple_labels(mbids, includes=None):
includes_data=includes_data,
)

for label in labels.values():
includes_data[label.id]['type'] = label.type
includes_data[label.id]['area'] = label.area

return {str(mbid): serialize_labels(label, includes_data[label.id]) for mbid, label in labels.items()}
return {str(mbid): serialize_labels(label, includes_data[label.id]) for mbid, label in labels.items()}
4 changes: 0 additions & 4 deletions brainzutils/musicbrainz_db/place.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,5 @@ def fetch_multiple_places(mbids, includes=None):
includes_data=includes_data,
)

for place in places.values():
includes_data[place.id]['area'] = place.area
includes_data[place.id]['type'] = place.type

places = {str(mbid): serialize_places(place, includes_data[place.id]) for mbid, place in places.items()}
return places
12 changes: 6 additions & 6 deletions brainzutils/musicbrainz_db/release_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ def fetch_multiple_release_groups(mbids, includes=None):

for release_group in release_groups.values():
includes_data[release_group.id]['meta'] = release_group.meta
includes_data[release_group.id]['type'] = release_group.type
release_groups = {str(mbid): serialize_release_groups(release_group, includes_data[release_group.id])
for mbid, release_group in release_groups.items()}
return release_groups
Expand Down Expand Up @@ -149,11 +148,12 @@ def get_release_groups_for_artist(artist_id, release_types=None, limit=None, off
case([(models.ReleaseGroupMeta.first_release_date_year.is_(None), 1)], else_=0),
models.ReleaseGroupMeta.first_release_date_year.desc()
).limit(limit).offset(offset).all()
for release_group in release_groups:
includes_data[release_group.id]['meta'] = release_group.meta
release_groups = ([serialize_release_groups(release_group, includes_data[release_group.id])
for release_group in release_groups], count)
return release_groups

for release_group in release_groups:
includes_data[release_group.id]['meta'] = release_group.meta
release_groups = ([serialize_release_groups(release_group, includes_data[release_group.id])
for release_group in release_groups], count)
return release_groups


def _get_release_groups_for_artist_query(db, artist_id, release_types):
Expand Down
34 changes: 17 additions & 17 deletions brainzutils/musicbrainz_db/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ def serialize_places(place, includes=None):
if place.comment:
data['comment'] = place.comment

if 'type' in includes and includes['type']:
data['type'] = includes['type'].name
if place.type:
data['type'] = place.type.name

if place.area:
data['area'] = serialize_areas(place.area)

if place.coordinates:
data['coordinates'] = {
Expand All @@ -158,9 +161,6 @@ def serialize_places(place, includes=None):
if dates:
data['life-span'] = dates

if 'area' in includes and includes['area']:
data['area'] = serialize_areas(includes['area'])

if 'relationship_objs' in includes:
serialize_relationships(data, place, includes['relationship_objs'])
return data
Expand All @@ -181,11 +181,11 @@ def serialize_labels(label, includes=None):
if dates:
data['life-span'] = dates

if 'type' in includes and includes['type']:
data['type'] = includes['type'].name
if label.type:
data['type'] = label.type.name

if 'area' in includes and includes['area']:
data['area'] = includes['area'].name
if label.area:
data['area'] = label.area.name

if getattr(label, 'rating', None):
data['rating'] = label.rating
Expand All @@ -212,7 +212,7 @@ def serialize_artists(artist, includes=None):
if dates:
data['life-span'] = dates

if 'type' in includes:
if artist.type:
data['type'] = artist.type.name

if getattr(artist, 'rating', None):
Expand Down Expand Up @@ -246,16 +246,16 @@ def serialize_release_groups(release_group, includes=None):
if release_group.comment:
data['comment'] = release_group.comment

if 'type' in includes and includes['type']:
data['type'] = includes['type'].name
if release_group.type:
data['type'] = release_group.type.name

if getattr(release_group, 'rating', None):
data['rating'] = release_group.rating

if 'artist-credit-phrase' in includes:
data['artist-credit-phrase'] = includes['artist-credit-phrase']

if 'meta' in includes and includes['meta'] and includes['meta'].first_release_date_year:
if 'meta' in includes and includes['meta'].first_release_date_year:
data['first-release-year'] = includes['meta'].first_release_date_year

if 'artist-credit-names' in includes:
Expand Down Expand Up @@ -351,8 +351,8 @@ def serialize_events(event, includes=None):
if dates:
data['life-span'] = dates

if 'type' in includes and includes['type']:
data['type'] = includes['type'].name
if event.type:
data['type'] = event.type.name

if getattr(event, 'rating', None):
data['rating'] = event.rating
Expand Down Expand Up @@ -386,8 +386,8 @@ def serialize_works(work, includes=None):
if work.comment:
data['comment'] = work.comment

if 'type' in includes and includes['type']:
data['type'] = includes['type'].name
if work.type:
data['type'] = work.type.name

if getattr(work, 'rating', None):
data['rating'] = work.rating
Expand Down
7 changes: 5 additions & 2 deletions brainzutils/musicbrainz_db/tests/test_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_get_artist_by_mbid(self, engine):
"comment": "American rock band",
"life-span": {"begin": "1999"},
"rating": 86,
"type": "Group",
}

def test_get_artist_by_mbid_redirect(self, engine):
Expand All @@ -27,13 +28,14 @@ def test_get_artist_by_mbid_redirect(self, engine):
"comment": "American singer-songwriter, actress, businesswoman, “Queen of Pop”",
"life-span": {"begin": "1958-08-16"},
"rating": 88,
"type": "Person",
}

def test_fetch_multiple_artists(self, engine):
artists = mb_artist.fetch_multiple_artists([
"f59c5520-5f46-4d2c-b2c4-822eabf53419",
"f82bcf78-5b69-4622-a5ef-73800768d9ac",
], includes=["type"])
])
assert artists["f82bcf78-5b69-4622-a5ef-73800768d9ac"] == {
"mbid": "f82bcf78-5b69-4622-a5ef-73800768d9ac",
"name": "JAY‐Z",
Expand Down Expand Up @@ -63,7 +65,8 @@ def test_fetch_multiple_artists_redirect(self, engine):
"sort_name": "Linkin Park",
"comment": "American rock band",
"life-span": {"begin": "1999"},
"rating": 86
"rating": 86,
"type": "Group"
}

def test_fetch_multiple_artists_missing(self, engine):
Expand Down
3 changes: 3 additions & 0 deletions brainzutils/musicbrainz_db/tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def test_get_event_by_mbid(self, engine):
'mbid': 'd4921d43-bf92-464e-aef4-bba8540fc5bd',
'name': 'Butterfly Whirl 2015',
'life-span': {'begin': '2015-05-22', 'end': '2015-05-25'},
'type': 'Festival'
}

def test_get_event_by_mbid_redirect(self, engine):
Expand All @@ -22,6 +23,7 @@ def test_get_event_by_mbid_redirect(self, engine):
'name': '1995-10-11: Riverport Amphitheatre, Maryland Heights, Missouri',
'life-span': {'begin': '1995-10-11', 'end': '1995-10-11'},
'rating': 100,
'type': 'Concert',
}

def test_fetch_multiple_events(self, engine):
Expand All @@ -41,6 +43,7 @@ def test_fetch_multiple_events_redirect(self, engine):
'name': '1995-10-11: Riverport Amphitheatre, Maryland Heights, Missouri',
'life-span': {'begin': '1995-10-11', 'end': '1995-10-11'},
'rating': 100,
'type': 'Concert',
}}

def test_fetch_multiple_events_empty(self, engine):
Expand Down
8 changes: 8 additions & 0 deletions brainzutils/musicbrainz_db/tests/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_get_release_by_mbid(self, engine):
'name': 'Metallica',
'sort_name': 'Metallica',
'life-span': {'begin': '1981-10-28'},
'type': 'Group',
}
}
],
Expand All @@ -49,6 +50,7 @@ def test_get_release_by_mbid(self, engine):
'name': 'Metallica',
'sort_name': 'Metallica',
'life-span': {'begin': '1981-10-28'},
'type': 'Group',
}
}
],
Expand All @@ -70,6 +72,7 @@ def test_get_release_by_mbid(self, engine):
'name': 'Metallica',
'sort_name': 'Metallica',
'life-span': {'begin': '1981-10-28'},
'type': 'Group',
}
}
],
Expand All @@ -90,6 +93,7 @@ def test_get_release_by_mbid(self, engine):
'name': 'Metallica',
'sort_name': 'Metallica',
'life-span': {'begin': '1981-10-28'},
'type': 'Group',
}
}
],
Expand All @@ -111,6 +115,7 @@ def test_get_release_by_mbid(self, engine):
'name': 'Metallica',
'sort_name': 'Metallica',
'life-span': {'begin': '1981-10-28'},
'type': 'Group',
}
}
],
Expand All @@ -131,6 +136,7 @@ def test_get_release_by_mbid(self, engine):
'name': 'Metallica',
'sort_name': 'Metallica',
'life-span': {'begin': '1981-10-28'},
'type': 'Group',
}
}
],
Expand All @@ -152,6 +158,7 @@ def test_get_release_by_mbid(self, engine):
'name': 'Metallica',
'sort_name': 'Metallica',
'life-span': {'begin': '1981-10-28'},
'type': 'Group',
}
}
],
Expand All @@ -173,6 +180,7 @@ def test_get_release_by_mbid(self, engine):
'name': 'Metallica',
'sort_name': 'Metallica',
'life-span': {'begin': '1981-10-28'},
'type': 'Group',
}
}
],
Expand Down
4 changes: 3 additions & 1 deletion brainzutils/musicbrainz_db/tests/test_release_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_get_release_group_by_mbid(self, engine):
'name': 'Led Zeppelin',
'sort_name': 'Led Zeppelin',
'life-span': {'begin': '1968', 'end': '1980-09-25'},
'type': 'Group',
}
}

Expand Down Expand Up @@ -72,7 +73,8 @@ def test_fetch_get_release_groups_for_artist(self, engine):
{
'mbid': '07f5e633-8846-3fe7-8e68-472b54dba159',
'title': 'This Is What the Edge of Your Seat Was Made For',
'first-release-year': 2004
'first-release-year': 2004,
'type': 'EP',
}
]
assert release_groups[1] == 1
6 changes: 2 additions & 4 deletions brainzutils/musicbrainz_db/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def fetch_multiple_works(mbids, includes=None):
includes_data = defaultdict(dict)
check_includes('work', includes)
with mb_session() as db:
query = db.query(models.Work).\
options(joinedload("type"))
query = db.query(models.Work).options(joinedload("type"))

works = get_entities_by_gids(
query=query,
entity_type='work',
Expand All @@ -70,6 +70,4 @@ def fetch_multiple_works(mbids, includes=None):
includes_data=includes_data,
)

for work in works.values():
includes_data[work.id]['type'] = work.type
return {str(mbid): serialize_works(work, includes_data[work.id]) for mbid, work in works.items()}

0 comments on commit a26ea01

Please sign in to comment.