Merge pull request #839 from tcely/patch-19

Increase episode number calculation speed
This commit is contained in:
meeb authored and GitHub committed 2025-03-13 18:28:59 +11:00
commit 5e1cd7f5f1
1 file changed
+27 -9
+27 -9
View File
@@ -1507,17 +1507,35 @@ class Media(models.Model):
def calculate_episode_number(self): def calculate_episode_number(self):
if self.source.is_playlist: if self.source.is_playlist:
sorted_media = Media.objects.filter(source=self.source) sorted_media = Media.objects.filter(
source=self.source,
metadata__isnull=False,
).order_by(
'published',
'created',
'key',
)
else: else:
self_year = self.upload_date.year if self.upload_date else self.created.year self_year = self.created.year # unlikely to be accurate
filtered_media = Media.objects.filter(source=self.source, published__year=self_year) if self.published:
filtered_media = [m for m in filtered_media if m.upload_date is not None] self_year = self.published.year
sorted_media = sorted(filtered_media, key=lambda x: (x.upload_date, x.key)) elif self.has_metadata and self.upload_date:
position_counter = 1 self_year = self.upload_date.year
for media in sorted_media: elif self.download_date:
# also, unlikely to be accurate
self_year = self.download_date.year
sorted_media = Media.objects.filter(
source=self.source,
metadata__isnull=False,
published__year=self_year,
).order_by(
'published',
'created',
'key',
)
for counter, media in enumerate(sorted_media, start=1):
if media == self: if media == self:
return position_counter return counter
position_counter += 1
def get_episode_str(self, use_padding=False): def get_episode_str(self, use_padding=False):
episode_number = self.calculate_episode_number() episode_number = self.calculate_episode_number()