fixed appending
This commit is contained in:
@@ -42,6 +42,9 @@ class Collection:
|
||||
|
||||
def map_element(self, element: DatabaseObject):
|
||||
for name, value in element.indexing_values:
|
||||
if value is None:
|
||||
continue
|
||||
|
||||
self._attribute_to_object_map[name][value] = element
|
||||
|
||||
def append(self, element: DatabaseObject, merge_on_conflict: bool = True):
|
||||
@@ -57,12 +60,13 @@ class Collection:
|
||||
|
||||
for name, value in element.indexing_values:
|
||||
if value in self._attribute_to_object_map[name]:
|
||||
# if the object does already exist
|
||||
# thus merging and don't add it afterwards
|
||||
existing_object = self._attribute_to_object_map[name][value]
|
||||
existing_object.merge(element)
|
||||
# in case any relevant data has been added (e.g. it remaps the old object)
|
||||
self.map_element(existing_object)
|
||||
if merge_on_conflict:
|
||||
# if the object does already exist
|
||||
# thus merging and don't add it afterwards
|
||||
existing_object = self._attribute_to_object_map[name][value]
|
||||
existing_object.merge(element)
|
||||
# in case any relevant data has been added (e.g. it remaps the old object)
|
||||
self.map_element(existing_object)
|
||||
return
|
||||
|
||||
self._data.append(element)
|
||||
|
||||
@@ -56,12 +56,12 @@ class DatabaseObject:
|
||||
return list()
|
||||
|
||||
def merge(self, other, override: bool = False):
|
||||
if isinstance(other, type(self)):
|
||||
if not isinstance(other, type(self)):
|
||||
LOGGER.warning(f"can't merge \"{type(other)}\" into \"{type(self)}\"")
|
||||
return
|
||||
|
||||
for collection in type(self).COLLECTION_ATTRIBUTES:
|
||||
getattr(self, collection).extend(collection)
|
||||
getattr(self, collection).extend(getattr(other, collection))
|
||||
|
||||
for simple_attribute in type(self).SIMPLE_ATTRIBUTES:
|
||||
if getattr(other, simple_attribute) is None:
|
||||
|
||||
@@ -87,7 +87,7 @@ class Song(MainObject):
|
||||
return [
|
||||
('id', self.id),
|
||||
('title', self.unified_title),
|
||||
('isrc', self.isrc.strip()),
|
||||
('isrc', self.isrc),
|
||||
*[('url', source.url) for source in self.source_collection]
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user