@@ -227,7 +227,10 @@ def paginate(self, collection):
227
227
f"page={ page } " , "page={}" .format (page + 1 )
228
228
)
229
229
# 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
+ ]
231
234
return data , next_url
232
235
233
236
def get_included (self , data , include_str ):
@@ -262,17 +265,25 @@ def get_included(self, data, include_str):
262
265
if res_type == "track" :
263
266
track_id = int (identifier ["id" ])
264
267
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
+ )
266
271
elif res_type == "album" :
267
272
album_id = int (identifier ["id" ])
268
273
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
+ )
270
277
elif res_type == "artist" :
271
278
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
+ )
273
282
elif res_type == "image" :
274
283
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
+ )
276
287
else :
277
288
raise ValueError (f"Invalid resource type: { res_type } " )
278
289
return included
@@ -360,7 +371,7 @@ def get_attribute_converter(self, beets_attr):
360
371
return converter
361
372
362
373
@staticmethod
363
- def resource_object ( track ):
374
+ def get_resource_object ( lib : Library , track ):
364
375
"""Construct a JSON:API resource object from a beets Item.
365
376
366
377
Args:
@@ -407,7 +418,9 @@ def single_resource(self, track_id):
407
418
track_id
408
419
),
409
420
)
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
+ )
411
424
412
425
413
426
class AlbumDocument (AURADocument ):
@@ -441,7 +454,7 @@ def get_attribute_converter(self, beets_attr):
441
454
return converter
442
455
443
456
@staticmethod
444
- def resource_object ( album ):
457
+ def get_resource_object ( lib : Library , album ):
445
458
"""Construct a JSON:API resource object from a beets Album.
446
459
447
460
Args:
@@ -460,7 +473,7 @@ def resource_object(album):
460
473
# track number. Sorting is not required but it's nice.
461
474
query = MatchQuery ("album_id" , album .id )
462
475
sort = FixedFieldSort ("track" , ascending = True )
463
- tracks = current_app . config [ " lib" ] .items (query , sort )
476
+ tracks = lib .items (query , sort )
464
477
# JSON:API one-to-many relationship to tracks on the album
465
478
relationships = {
466
479
"tracks" : {
@@ -505,7 +518,9 @@ def single_resource(self, album_id):
505
518
album_id
506
519
),
507
520
)
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
+ )
509
524
510
525
511
526
class ArtistDocument (AURADocument ):
@@ -546,15 +561,15 @@ def get_attribute_converter(self, beets_attr):
546
561
return converter
547
562
548
563
@staticmethod
549
- def resource_object ( artist_id ):
564
+ def get_resource_object ( lib : Library , artist_id ):
550
565
"""Construct a JSON:API resource object for the given artist.
551
566
552
567
Args:
553
568
artist_id: A string which is the artist's name.
554
569
"""
555
570
# Get tracks where artist field exactly matches artist_id
556
571
query = MatchQuery ("artist" , artist_id )
557
- tracks = current_app . config [ " lib" ] .items (query )
572
+ tracks = lib .items (query )
558
573
if not tracks :
559
574
return None
560
575
@@ -576,7 +591,7 @@ def resource_object(artist_id):
576
591
}
577
592
}
578
593
album_query = MatchQuery ("albumartist" , artist_id )
579
- albums = current_app . config [ " lib" ] .albums (query = album_query )
594
+ albums = lib .albums (query = album_query )
580
595
if len (albums ) != 0 :
581
596
relationships ["albums" ] = {
582
597
"data" : [{"type" : "album" , "id" : str (a .id )} for a in albums ]
@@ -595,7 +610,7 @@ def single_resource(self, artist_id):
595
610
Args:
596
611
artist_id: A string which is the artist's name.
597
612
"""
598
- artist_resource = self .resource_object ( artist_id )
613
+ artist_resource = self .get_resource_object ( self . lib , artist_id )
599
614
if not artist_resource :
600
615
return self .error (
601
616
"404 Not Found" ,
@@ -629,7 +644,7 @@ class ImageDocument(AURADocument):
629
644
"""Class for building documents for /images/(id) endpoints."""
630
645
631
646
@staticmethod
632
- def get_image_path (image_id ):
647
+ def get_image_path (lib : Library , image_id ):
633
648
"""Works out the full path to the image with the given id.
634
649
635
650
Returns None if there is no such image.
@@ -651,7 +666,7 @@ def get_image_path(image_id):
651
666
652
667
# Get the path to the directory parent's images are in
653
668
if parent_type == "album" :
654
- album = current_app . config [ " lib" ] .get_album (int (parent_id ))
669
+ album = lib .get_album (int (parent_id ))
655
670
if not album or not album .artpath :
656
671
return None
657
672
# Cut the filename off of artpath
@@ -671,7 +686,7 @@ def get_image_path(image_id):
671
686
return None
672
687
673
688
@staticmethod
674
- def resource_object ( image_id ):
689
+ def get_resource_object ( lib : Library , image_id ):
675
690
"""Construct a JSON:API resource object for the given image.
676
691
677
692
Args:
@@ -680,7 +695,7 @@ def resource_object(image_id):
680
695
"""
681
696
# Could be called as a static method, so can't use
682
697
# self.get_image_path()
683
- image_path = ImageDocument .get_image_path (image_id )
698
+ image_path = ImageDocument .get_image_path (lib , image_id )
684
699
if not image_path :
685
700
return None
686
701
@@ -720,7 +735,7 @@ def single_resource(self, image_id):
720
735
image_id: A string in the form
721
736
"<parent_type>-<parent_id>-<img_filename>".
722
737
"""
723
- image_resource = self .resource_object ( image_id )
738
+ image_resource = self .get_resource_object ( self . lib , image_id )
724
739
if not image_resource :
725
740
return self .error (
726
741
"404 Not Found" ,
@@ -889,7 +904,7 @@ def image_file(image_id):
889
904
image_id: The id of the image provided in the URL. A string in
890
905
the form "<parent_type>-<parent_id>-<img_filename>".
891
906
"""
892
- img_path = ImageDocument .get_image_path (image_id )
907
+ img_path = ImageDocument .get_image_path (current_app . config [ "lib" ], image_id )
893
908
if not img_path :
894
909
return AURADocument .error (
895
910
"404 Not Found" ,
0 commit comments