Skip to content

Commit a26ea01

Browse files
committed
Always return type information without an include parameter
1 parent b6a8346 commit a26ea01

12 files changed

Lines changed: 51 additions & 51 deletions

File tree

brainzutils/musicbrainz_db/artist.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ def fetch_multiple_artists(mbids, includes=None):
4444
check_includes('artist', includes)
4545

4646
with mb_session() as db:
47-
query = db.query(models.Artist)
48-
49-
if 'type' in includes:
50-
query = query.options(joinedload('type'))
47+
query = db.query(models.Artist).options(joinedload('type'))
5148

5249
artists = get_entities_by_gids(
5350
query=query,
@@ -74,9 +71,5 @@ def fetch_multiple_artists(mbids, includes=None):
7471
includes_data=includes_data,
7572
)
7673

77-
if 'type' in includes:
78-
for artist in artists.values():
79-
includes_data[artist.id]['type'] = artist.type
80-
81-
artists = {str(mbid): serialize_artists(artist, includes_data[artist.id]) for mbid, artist in artists.items()}
82-
return artists
74+
artists = {str(mbid): serialize_artists(artist, includes_data[artist.id]) for mbid, artist in artists.items()}
75+
return artists

brainzutils/musicbrainz_db/event.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections import defaultdict
2+
from sqlalchemy.orm import joinedload
23
from mbdata import models
34
from brainzutils.musicbrainz_db import mb_session
45
from brainzutils.musicbrainz_db.utils import get_entities_by_gids
@@ -42,7 +43,7 @@ def fetch_multiple_events(mbids, includes=None):
4243
includes_data = defaultdict(dict)
4344
check_includes('event', includes)
4445
with mb_session() as db:
45-
query = db.query(models.Event)
46+
query = db.query(models.Event).options(joinedload('type'))
4647
events = get_entities_by_gids(
4748
query=query,
4849
entity_type='event',

brainzutils/musicbrainz_db/includes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
'release': [
2929
"artists", "labels", "recordings", "release-groups", "media", "annotation", "aliases"
3030
] + TAG_INCLUDES + RELATION_INCLUDES,
31-
'artist': ["recordings", "releases", "media", "aliases", "annotation", "type"] + RELATION_INCLUDES + TAG_INCLUDES,
31+
'artist': ["recordings", "releases", "media", "aliases", "annotation"] + RELATION_INCLUDES + TAG_INCLUDES,
3232
'label': ["area", "aliases", "annotation"] + RELATION_INCLUDES + TAG_INCLUDES,
3333
'work': ["artists", "recordings", "aliases", "annotation"] + RELATION_INCLUDES + TAG_INCLUDES,
3434
'editor': [], # TODO: List includes here (BU-18)

brainzutils/musicbrainz_db/label.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,4 @@ def fetch_multiple_labels(mbids, includes=None):
7070
includes_data=includes_data,
7171
)
7272

73-
for label in labels.values():
74-
includes_data[label.id]['type'] = label.type
75-
includes_data[label.id]['area'] = label.area
76-
77-
return {str(mbid): serialize_labels(label, includes_data[label.id]) for mbid, label in labels.items()}
73+
return {str(mbid): serialize_labels(label, includes_data[label.id]) for mbid, label in labels.items()}

brainzutils/musicbrainz_db/place.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,5 @@ def fetch_multiple_places(mbids, includes=None):
7878
includes_data=includes_data,
7979
)
8080

81-
for place in places.values():
82-
includes_data[place.id]['area'] = place.area
83-
includes_data[place.id]['type'] = place.type
84-
8581
places = {str(mbid): serialize_places(place, includes_data[place.id]) for mbid, place in places.items()}
8682
return places

brainzutils/musicbrainz_db/release_group.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ def fetch_multiple_release_groups(mbids, includes=None):
109109

110110
for release_group in release_groups.values():
111111
includes_data[release_group.id]['meta'] = release_group.meta
112-
includes_data[release_group.id]['type'] = release_group.type
113112
release_groups = {str(mbid): serialize_release_groups(release_group, includes_data[release_group.id])
114113
for mbid, release_group in release_groups.items()}
115114
return release_groups
@@ -149,11 +148,12 @@ def get_release_groups_for_artist(artist_id, release_types=None, limit=None, off
149148
case([(models.ReleaseGroupMeta.first_release_date_year.is_(None), 1)], else_=0),
150149
models.ReleaseGroupMeta.first_release_date_year.desc()
151150
).limit(limit).offset(offset).all()
152-
for release_group in release_groups:
153-
includes_data[release_group.id]['meta'] = release_group.meta
154-
release_groups = ([serialize_release_groups(release_group, includes_data[release_group.id])
155-
for release_group in release_groups], count)
156-
return release_groups
151+
152+
for release_group in release_groups:
153+
includes_data[release_group.id]['meta'] = release_group.meta
154+
release_groups = ([serialize_release_groups(release_group, includes_data[release_group.id])
155+
for release_group in release_groups], count)
156+
return release_groups
157157

158158

159159
def _get_release_groups_for_artist_query(db, artist_id, release_types):

brainzutils/musicbrainz_db/serialize.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,11 @@ def serialize_places(place, includes=None):
145145
if place.comment:
146146
data['comment'] = place.comment
147147

148-
if 'type' in includes and includes['type']:
149-
data['type'] = includes['type'].name
148+
if place.type:
149+
data['type'] = place.type.name
150+
151+
if place.area:
152+
data['area'] = serialize_areas(place.area)
150153

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

161-
if 'area' in includes and includes['area']:
162-
data['area'] = serialize_areas(includes['area'])
163-
164164
if 'relationship_objs' in includes:
165165
serialize_relationships(data, place, includes['relationship_objs'])
166166
return data
@@ -181,11 +181,11 @@ def serialize_labels(label, includes=None):
181181
if dates:
182182
data['life-span'] = dates
183183

184-
if 'type' in includes and includes['type']:
185-
data['type'] = includes['type'].name
184+
if label.type:
185+
data['type'] = label.type.name
186186

187-
if 'area' in includes and includes['area']:
188-
data['area'] = includes['area'].name
187+
if label.area:
188+
data['area'] = label.area.name
189189

190190
if getattr(label, 'rating', None):
191191
data['rating'] = label.rating
@@ -212,7 +212,7 @@ def serialize_artists(artist, includes=None):
212212
if dates:
213213
data['life-span'] = dates
214214

215-
if 'type' in includes:
215+
if artist.type:
216216
data['type'] = artist.type.name
217217

218218
if getattr(artist, 'rating', None):
@@ -246,16 +246,16 @@ def serialize_release_groups(release_group, includes=None):
246246
if release_group.comment:
247247
data['comment'] = release_group.comment
248248

249-
if 'type' in includes and includes['type']:
250-
data['type'] = includes['type'].name
249+
if release_group.type:
250+
data['type'] = release_group.type.name
251251

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

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

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

261261
if 'artist-credit-names' in includes:
@@ -351,8 +351,8 @@ def serialize_events(event, includes=None):
351351
if dates:
352352
data['life-span'] = dates
353353

354-
if 'type' in includes and includes['type']:
355-
data['type'] = includes['type'].name
354+
if event.type:
355+
data['type'] = event.type.name
356356

357357
if getattr(event, 'rating', None):
358358
data['rating'] = event.rating
@@ -386,8 +386,8 @@ def serialize_works(work, includes=None):
386386
if work.comment:
387387
data['comment'] = work.comment
388388

389-
if 'type' in includes and includes['type']:
390-
data['type'] = includes['type'].name
389+
if work.type:
390+
data['type'] = work.type.name
391391

392392
if getattr(work, 'rating', None):
393393
data['rating'] = work.rating

brainzutils/musicbrainz_db/tests/test_artist.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def test_get_artist_by_mbid(self, engine):
1515
"comment": "American rock band",
1616
"life-span": {"begin": "1999"},
1717
"rating": 86,
18+
"type": "Group",
1819
}
1920

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

3234
def test_fetch_multiple_artists(self, engine):
3335
artists = mb_artist.fetch_multiple_artists([
3436
"f59c5520-5f46-4d2c-b2c4-822eabf53419",
3537
"f82bcf78-5b69-4622-a5ef-73800768d9ac",
36-
], includes=["type"])
38+
])
3739
assert artists["f82bcf78-5b69-4622-a5ef-73800768d9ac"] == {
3840
"mbid": "f82bcf78-5b69-4622-a5ef-73800768d9ac",
3941
"name": "JAY‐Z",
@@ -63,7 +65,8 @@ def test_fetch_multiple_artists_redirect(self, engine):
6365
"sort_name": "Linkin Park",
6466
"comment": "American rock band",
6567
"life-span": {"begin": "1999"},
66-
"rating": 86
68+
"rating": 86,
69+
"type": "Group"
6770
}
6871

6972
def test_fetch_multiple_artists_missing(self, engine):

brainzutils/musicbrainz_db/tests/test_event.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def test_get_event_by_mbid(self, engine):
1212
'mbid': 'd4921d43-bf92-464e-aef4-bba8540fc5bd',
1313
'name': 'Butterfly Whirl 2015',
1414
'life-span': {'begin': '2015-05-22', 'end': '2015-05-25'},
15+
'type': 'Festival'
1516
}
1617

1718
def test_get_event_by_mbid_redirect(self, engine):
@@ -22,6 +23,7 @@ def test_get_event_by_mbid_redirect(self, engine):
2223
'name': '1995-10-11: Riverport Amphitheatre, Maryland Heights, Missouri',
2324
'life-span': {'begin': '1995-10-11', 'end': '1995-10-11'},
2425
'rating': 100,
26+
'type': 'Concert',
2527
}
2628

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

4649
def test_fetch_multiple_events_empty(self, engine):

brainzutils/musicbrainz_db/tests/test_release.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def test_get_release_by_mbid(self, engine):
2828
'name': 'Metallica',
2929
'sort_name': 'Metallica',
3030
'life-span': {'begin': '1981-10-28'},
31+
'type': 'Group',
3132
}
3233
}
3334
],
@@ -49,6 +50,7 @@ def test_get_release_by_mbid(self, engine):
4950
'name': 'Metallica',
5051
'sort_name': 'Metallica',
5152
'life-span': {'begin': '1981-10-28'},
53+
'type': 'Group',
5254
}
5355
}
5456
],
@@ -70,6 +72,7 @@ def test_get_release_by_mbid(self, engine):
7072
'name': 'Metallica',
7173
'sort_name': 'Metallica',
7274
'life-span': {'begin': '1981-10-28'},
75+
'type': 'Group',
7376
}
7477
}
7578
],
@@ -90,6 +93,7 @@ def test_get_release_by_mbid(self, engine):
9093
'name': 'Metallica',
9194
'sort_name': 'Metallica',
9295
'life-span': {'begin': '1981-10-28'},
96+
'type': 'Group',
9397
}
9498
}
9599
],
@@ -111,6 +115,7 @@ def test_get_release_by_mbid(self, engine):
111115
'name': 'Metallica',
112116
'sort_name': 'Metallica',
113117
'life-span': {'begin': '1981-10-28'},
118+
'type': 'Group',
114119
}
115120
}
116121
],
@@ -131,6 +136,7 @@ def test_get_release_by_mbid(self, engine):
131136
'name': 'Metallica',
132137
'sort_name': 'Metallica',
133138
'life-span': {'begin': '1981-10-28'},
139+
'type': 'Group',
134140
}
135141
}
136142
],
@@ -152,6 +158,7 @@ def test_get_release_by_mbid(self, engine):
152158
'name': 'Metallica',
153159
'sort_name': 'Metallica',
154160
'life-span': {'begin': '1981-10-28'},
161+
'type': 'Group',
155162
}
156163
}
157164
],
@@ -173,6 +180,7 @@ def test_get_release_by_mbid(self, engine):
173180
'name': 'Metallica',
174181
'sort_name': 'Metallica',
175182
'life-span': {'begin': '1981-10-28'},
183+
'type': 'Group',
176184
}
177185
}
178186
],

0 commit comments

Comments
 (0)