[videos] Fix saving imdb_id duplicates
All checks were successful
build & deploy / test (push) Successful in 2m1s
build & deploy / deploy (push) Successful in 22s

This commit is contained in:
2026-03-21 14:35:39 -04:00
parent a36abacfbd
commit 190f486c49
5 changed files with 88 additions and 68 deletions

View File

@ -178,17 +178,13 @@ class Series(TimeStampedModel):
def last_scrobbled_episode(self, user_id: int) -> Optional["Video"]:
episode = None
last_scrobble = self.scrobbles_for_user(
user_id, include_playing=True
).first()
last_scrobble = self.scrobbles_for_user(user_id, include_playing=True).first()
if last_scrobble:
episode = last_scrobble.media_obj
return episode
def is_episode_playing(self, user_id: int) -> bool:
last_scrobble = self.scrobbles_for_user(
user_id, include_playing=True
).first()
last_scrobble = self.scrobbles_for_user(user_id, include_playing=True).first()
return not last_scrobble.played_to_completion
def fix_metadata(self, force_update=False):
@ -265,7 +261,7 @@ class Video(ScrobblableMixin):
season_number = models.IntegerField(**BNULL)
episode_number = models.IntegerField(**BNULL)
next_imdb_id = models.CharField(max_length=20, **BNULL)
imdb_id = models.CharField(max_length=20, **BNULL)
imdb_id = models.CharField(max_length=20, **BNULL, unique=True)
imdb_rating = models.FloatField(**BNULL)
tmdb_rating = models.FloatField(**BNULL)
cover_image = models.ImageField(upload_to="videos/video/", **BNULL)
@ -288,9 +284,6 @@ class Video(ScrobblableMixin):
plot = models.TextField(**BNULL)
upload_date = models.DateField(**BNULL)
class Meta:
unique_together = [["title", "imdb_id"]]
def __str__(self):
if not self.title:
return self.youtube_id or self.imdb_id
@ -363,9 +356,7 @@ class Video(ScrobblableMixin):
self.cover_image.save(fname, ContentFile(r.content), save=True)
@classmethod
def get_from_youtube_id(
cls, youtube_id: str, overwrite: bool = False
) -> "Video":
def get_from_youtube_id(cls, youtube_id: str, overwrite: bool = False) -> "Video":
video, created = cls.objects.get_or_create(youtube_id=youtube_id)
if not created and not overwrite:
return video
@ -385,9 +376,7 @@ class Video(ScrobblableMixin):
return video
@classmethod
def get_from_imdb_id(
cls, imdb_id: str, overwrite: bool = False
) -> "Video":
def get_from_imdb_id(cls, imdb_id: str, overwrite: bool = False) -> "Video":
video, created = cls.objects.get_or_create(imdb_id=imdb_id)
if not created and not overwrite:
return video
@ -414,14 +403,16 @@ class Video(ScrobblableMixin):
@classmethod
def find_or_create(
cls, source_id: str, overwrite: bool = False
) -> "Video":
cls, source_id: Optional[str], overwrite: bool = False
) -> Optional["Video"]:
if not source_id:
logger.warning("No source_id provided for Video.find_or_create")
return None
if "tt" in source_id:
return cls.get_from_imdb_id(source_id, overwrite)
if bool(YOUTUBE_ID_PATTERN.match(source_id)):
return cls.get_from_youtube_id(source_id, overwrite)
# TODO scrobble but without a video obj?
logger.warning("Video ID not recognized, not scrobbling")
return
return None