[templates] Try using defensive checks for missing media
This commit is contained in:
@ -75,6 +75,16 @@ class Channel(TimeStampedModel):
|
||||
url = self.cover_image_medium.url
|
||||
return url
|
||||
|
||||
@property
|
||||
def safe_cover_image_url(self) -> str:
|
||||
if self.cover_image:
|
||||
try:
|
||||
if self.cover_image.storage.exists(self.cover_image.name):
|
||||
return self.cover_medium.url
|
||||
except Exception:
|
||||
pass
|
||||
return "/static/images/not-found.jpg"
|
||||
|
||||
def scrobbles_for_user(self, user_id: int, include_playing=False):
|
||||
from scrobbles.models import Scrobble
|
||||
|
||||
@ -137,6 +147,16 @@ class Series(TimeStampedModel):
|
||||
url = self.cover_image_medium.url
|
||||
return url
|
||||
|
||||
@property
|
||||
def safe_cover_image_url(self) -> str:
|
||||
if self.cover_image:
|
||||
try:
|
||||
if self.cover_image.storage.exists(self.cover_image.name):
|
||||
return self.cover_medium.url
|
||||
except Exception:
|
||||
pass
|
||||
return "/static/images/not-found.jpg"
|
||||
|
||||
def save_image_from_url(self, url: str, force_update: bool = False):
|
||||
if not self.cover_image or (force_update and url):
|
||||
r = requests.get(url)
|
||||
@ -158,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):
|
||||
@ -321,6 +337,16 @@ class Video(ScrobblableMixin):
|
||||
url = self.cover_image_medium.url
|
||||
return url
|
||||
|
||||
@property
|
||||
def safe_cover_image_url(self) -> str:
|
||||
if self.cover_image:
|
||||
try:
|
||||
if self.cover_image.storage.exists(self.cover_image.name):
|
||||
return self.cover_image_medium.url
|
||||
except Exception:
|
||||
pass
|
||||
return "/static/images/not-found.jpg"
|
||||
|
||||
@property
|
||||
def strings(self) -> ScrobblableConstants:
|
||||
return ScrobblableConstants(verb="Watching", tags="movie_camera")
|
||||
@ -333,9 +359,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
|
||||
@ -355,9 +379,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
|
||||
|
||||
Reference in New Issue
Block a user