From fac70231e84fc808fc9dbbdfeaa916f0dec7ab6b Mon Sep 17 00:00:00 2001 From: Felix Stupp Date: Sun, 30 Oct 2022 22:46:33 +0100 Subject: [PATCH] MediaCollection.add_episde: detect change & act accordingly - only apply changes if episode is new or new data is given - flush only if changes are given - return link only on changes / creation --- server/entertainment_decider/models.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/server/entertainment_decider/models.py b/server/entertainment_decider/models.py index 161cc43..5f43305 100644 --- a/server/entertainment_decider/models.py +++ b/server/entertainment_decider/models.py @@ -777,15 +777,21 @@ class MediaCollection(db.Entity, Tagable): media: MediaElement, season: int = 0, episode: int = 0, - ) -> MediaCollectionLink: + ) -> Optional[MediaCollectionLink]: link: MediaCollectionLink = MediaCollectionLink.get( collection=self, element=media ) + change = False if link is None: + change = True link = MediaCollectionLink(collection=self, element=media) - link.season, link.episode = season, episode - orm.flush() - return link + if (link.season, link.episode) != (season, episode): + change = True + link.season, link.episode = season, episode + if change: + orm.flush() + return link + return None def add_single_uri(self, uri: str) -> bool: mapping: CollectionUriMapping = CollectionUriMapping.get(uri=uri)