Skip to content

Commit

Permalink
Do small optimization on Album.cover()
Browse files Browse the repository at this point in the history
  • Loading branch information
ColonelPhantom committed Jan 15, 2025
1 parent aab4490 commit 6b45d5b
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions website/photos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,15 @@ def cover(self):
If a cover is not set, return a random photo or None if there are no photos.
"""
cover = None
if self._cover is not None:
return self._cover

# Not prefetched because this should be rare and is a lot of data
# `exists` is faster in theory, but requires everything to be fetched later anyways
if self.photo_set.exists():
r = random.Random(self.dirname)
cover = r.choice(self.photo_set.all())
return cover
r = random.Random(self.dirname)
try:
return r.choice(self.photo_set.all())
except IndexError:
return None

def __str__(self):
"""Get string representation of Album."""
Expand Down

0 comments on commit 6b45d5b

Please sign in to comment.