Skip to content

Commit b96c8ae

Browse files
committed
fixup! Feed in app context and args into Document to allow testing
1 parent c602c52 commit b96c8ae

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

beetsplug/aura.py

+35-20
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ def paginate(self, collection):
227227
f"page={page}", "page={}".format(page + 1)
228228
)
229229
# Get only the items in the page range
230-
data = [self.resource_object(collection[i]) for i in range(start, end)]
230+
data = [
231+
self.get_resource_object(self.lib, collection[i])
232+
for i in range(start, end)
233+
]
231234
return data, next_url
232235

233236
def get_included(self, data, include_str):
@@ -262,17 +265,25 @@ def get_included(self, data, include_str):
262265
if res_type == "track":
263266
track_id = int(identifier["id"])
264267
track = self.lib.get_item(track_id)
265-
included.append(TrackDocument.resource_object(track))
268+
included.append(
269+
TrackDocument.get_resource_object(self.lib, track)
270+
)
266271
elif res_type == "album":
267272
album_id = int(identifier["id"])
268273
album = self.lib.get_album(album_id)
269-
included.append(AlbumDocument.resource_object(album))
274+
included.append(
275+
AlbumDocument.get_resource_object(self.lib, album)
276+
)
270277
elif res_type == "artist":
271278
artist_id = identifier["id"]
272-
included.append(ArtistDocument.resource_object(artist_id))
279+
included.append(
280+
ArtistDocument.get_resource_object(self.lib, artist_id)
281+
)
273282
elif res_type == "image":
274283
image_id = identifier["id"]
275-
included.append(ImageDocument.resource_object(image_id))
284+
included.append(
285+
ImageDocument.get_resource_object(self.lib, image_id)
286+
)
276287
else:
277288
raise ValueError(f"Invalid resource type: {res_type}")
278289
return included
@@ -360,7 +371,7 @@ def get_attribute_converter(self, beets_attr):
360371
return converter
361372

362373
@staticmethod
363-
def resource_object(track):
374+
def get_resource_object(lib: Library, track):
364375
"""Construct a JSON:API resource object from a beets Item.
365376
366377
Args:
@@ -407,7 +418,9 @@ def single_resource(self, track_id):
407418
track_id
408419
),
409420
)
410-
return self.single_resource_document(self.resource_object(track))
421+
return self.single_resource_document(
422+
self.get_resource_object(self.lib, track)
423+
)
411424

412425

413426
class AlbumDocument(AURADocument):
@@ -441,7 +454,7 @@ def get_attribute_converter(self, beets_attr):
441454
return converter
442455

443456
@staticmethod
444-
def resource_object(album):
457+
def get_resource_object(lib: Library, album):
445458
"""Construct a JSON:API resource object from a beets Album.
446459
447460
Args:
@@ -460,7 +473,7 @@ def resource_object(album):
460473
# track number. Sorting is not required but it's nice.
461474
query = MatchQuery("album_id", album.id)
462475
sort = FixedFieldSort("track", ascending=True)
463-
tracks = current_app.config["lib"].items(query, sort)
476+
tracks = lib.items(query, sort)
464477
# JSON:API one-to-many relationship to tracks on the album
465478
relationships = {
466479
"tracks": {
@@ -505,7 +518,9 @@ def single_resource(self, album_id):
505518
album_id
506519
),
507520
)
508-
return self.single_resource_document(self.resource_object(album))
521+
return self.single_resource_document(
522+
self.get_resource_object(self.lib, album)
523+
)
509524

510525

511526
class ArtistDocument(AURADocument):
@@ -546,15 +561,15 @@ def get_attribute_converter(self, beets_attr):
546561
return converter
547562

548563
@staticmethod
549-
def resource_object(artist_id):
564+
def get_resource_object(lib: Library, artist_id):
550565
"""Construct a JSON:API resource object for the given artist.
551566
552567
Args:
553568
artist_id: A string which is the artist's name.
554569
"""
555570
# Get tracks where artist field exactly matches artist_id
556571
query = MatchQuery("artist", artist_id)
557-
tracks = current_app.config["lib"].items(query)
572+
tracks = lib.items(query)
558573
if not tracks:
559574
return None
560575

@@ -576,7 +591,7 @@ def resource_object(artist_id):
576591
}
577592
}
578593
album_query = MatchQuery("albumartist", artist_id)
579-
albums = current_app.config["lib"].albums(query=album_query)
594+
albums = lib.albums(query=album_query)
580595
if len(albums) != 0:
581596
relationships["albums"] = {
582597
"data": [{"type": "album", "id": str(a.id)} for a in albums]
@@ -595,7 +610,7 @@ def single_resource(self, artist_id):
595610
Args:
596611
artist_id: A string which is the artist's name.
597612
"""
598-
artist_resource = self.resource_object(artist_id)
613+
artist_resource = self.get_resource_object(self.lib, artist_id)
599614
if not artist_resource:
600615
return self.error(
601616
"404 Not Found",
@@ -629,7 +644,7 @@ class ImageDocument(AURADocument):
629644
"""Class for building documents for /images/(id) endpoints."""
630645

631646
@staticmethod
632-
def get_image_path(image_id):
647+
def get_image_path(lib: Library, image_id):
633648
"""Works out the full path to the image with the given id.
634649
635650
Returns None if there is no such image.
@@ -651,7 +666,7 @@ def get_image_path(image_id):
651666

652667
# Get the path to the directory parent's images are in
653668
if parent_type == "album":
654-
album = current_app.config["lib"].get_album(int(parent_id))
669+
album = lib.get_album(int(parent_id))
655670
if not album or not album.artpath:
656671
return None
657672
# Cut the filename off of artpath
@@ -671,7 +686,7 @@ def get_image_path(image_id):
671686
return None
672687

673688
@staticmethod
674-
def resource_object(image_id):
689+
def get_resource_object(lib: Library, image_id):
675690
"""Construct a JSON:API resource object for the given image.
676691
677692
Args:
@@ -680,7 +695,7 @@ def resource_object(image_id):
680695
"""
681696
# Could be called as a static method, so can't use
682697
# self.get_image_path()
683-
image_path = ImageDocument.get_image_path(image_id)
698+
image_path = ImageDocument.get_image_path(lib, image_id)
684699
if not image_path:
685700
return None
686701

@@ -720,7 +735,7 @@ def single_resource(self, image_id):
720735
image_id: A string in the form
721736
"<parent_type>-<parent_id>-<img_filename>".
722737
"""
723-
image_resource = self.resource_object(image_id)
738+
image_resource = self.get_resource_object(self.lib, image_id)
724739
if not image_resource:
725740
return self.error(
726741
"404 Not Found",
@@ -889,7 +904,7 @@ def image_file(image_id):
889904
image_id: The id of the image provided in the URL. A string in
890905
the form "<parent_type>-<parent_id>-<img_filename>".
891906
"""
892-
img_path = ImageDocument.get_image_path(image_id)
907+
img_path = ImageDocument.get_image_path(current_app.config["lib"], image_id)
893908
if not img_path:
894909
return AURADocument.error(
895910
"404 Not Found",

0 commit comments

Comments
 (0)