feat: syncing artists between song and album

This commit is contained in:
Hazel 2024-04-18 17:20:30 +02:00
parent 3cd9daf512
commit 3532fea36c
2 changed files with 11 additions and 1 deletions

View File

@ -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:

View File

@ -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
}