From 3532fea36cf40b732fb33bebeef14a267beb29e2 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Thu, 18 Apr 2024 17:20:30 +0200 Subject: [PATCH] feat: syncing artists between song and album --- music_kraken/objects/collection.py | 6 ++++++ music_kraken/objects/song.py | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/music_kraken/objects/collection.py b/music_kraken/objects/collection.py index 1e3ee89..1d62116 100644 --- a/music_kraken/objects/collection.py +++ b/music_kraken/objects/collection.py @@ -36,6 +36,7 @@ class Collection(Generic[T]): # Value: main collection to sync to self.contain_given_in_attribute: Dict[str, Collection] = contain_given_in_attribute or {} self.append_object_to_attribute: Dict[str, T] = append_object_to_attribute or {} + self.sync_on_append: Dict[str, Collection] = sync_on_append or {} self._id_to_index_values: Dict[int, set] = defaultdict(set) self._indexed_values = defaultdict(lambda: None) @@ -229,6 +230,11 @@ class Collection(Generic[T]): for attribute, new_object in self.append_object_to_attribute.items(): __object.__getattribute__(attribute).append(new_object) + + for attribute, collection in self.sync_on_append.items(): + collection.extend(__object.__getattribute__(attribute)) + __object.__setattr__(attribute, collection) + else: # merge only if the two objects are not the same if existing_object.id == __object.id: diff --git a/music_kraken/objects/song.py b/music_kraken/objects/song.py index b81d57f..e682fe1 100644 --- a/music_kraken/objects/song.py +++ b/music_kraken/objects/song.py @@ -91,6 +91,10 @@ class Song(Base): "artist_collection": self.main_artist_collection, } """ + self.album_collection.sync_on_append = { + "artist_collection": self.main_artist_collection, + } + self.album_collection.append_object_to_attribute = { "song_collection": self, } @@ -247,7 +251,7 @@ class Album(Base): self.song_collection.append_object_to_attribute = { "album_collection": self } - self.song_collection.contain_given_in_attribute = { + self.song_collection.sync_on_append = { "main_artist_collection": self.artist_collection }