|
5 | 5 | from django.utils import timezone
|
6 | 6 |
|
7 | 7 | from app import media_type_config
|
8 |
| -from app.models import Item, Media, MediaTypes, Season |
| 8 | +from app.models import TV, Item, Media, MediaTypes, Season |
9 | 9 |
|
10 | 10 | # Statuses that represent inactive tracking
|
11 | 11 | # will be ignored when creating events
|
@@ -68,38 +68,48 @@ def get_user_events(self, user, first_day, last_day):
|
68 | 68 | return self.sort_with_sentinel_last(queryset)
|
69 | 69 |
|
70 | 70 | def _build_tv_query(self, user, enabled_types):
|
71 |
| - """Build query for TV shows based on latest season status.""" |
| 71 | + """Build query for TV shows based on TV status and season statuses.""" |
72 | 72 | if not (
|
73 | 73 | MediaTypes.TV.value in enabled_types
|
74 | 74 | or MediaTypes.SEASON.value in enabled_types
|
75 | 75 | ):
|
76 | 76 | return Q()
|
77 | 77 |
|
78 |
| - user_seasons = Season.objects.filter(user=user).select_related("item") |
79 |
| - latest_seasons = {} |
80 |
| - |
81 |
| - for season in user_seasons: |
82 |
| - tv_id = season.item.media_id |
83 |
| - season_number = season.item.season_number |
| 78 | + active_tv_shows = ( |
| 79 | + TV.objects.filter( |
| 80 | + user=user, |
| 81 | + item__media_type=MediaTypes.TV.value, |
| 82 | + ) |
| 83 | + .exclude( |
| 84 | + status__in=INACTIVE_TRACKING_STATUSES, |
| 85 | + ) |
| 86 | + .values_list("item__media_id", flat=True) |
| 87 | + ) |
84 | 88 |
|
85 |
| - if ( |
86 |
| - tv_id not in latest_seasons |
87 |
| - or season_number > latest_seasons[tv_id].item.season_number |
88 |
| - ): |
89 |
| - latest_seasons[tv_id] = season |
| 89 | + if not active_tv_shows: |
| 90 | + return Q() |
90 | 91 |
|
91 |
| - tv_shows_with_active_latest_season = { |
92 |
| - tv_id |
93 |
| - for tv_id, season in latest_seasons.items() |
94 |
| - if season.status not in INACTIVE_TRACKING_STATUSES |
95 |
| - } |
| 92 | + inactive_seasons = Season.objects.filter( |
| 93 | + user=user, |
| 94 | + status__in=INACTIVE_TRACKING_STATUSES, |
| 95 | + item__media_id__in=active_tv_shows, |
| 96 | + ).select_related("item") |
96 | 97 |
|
97 |
| - if not tv_shows_with_active_latest_season: |
98 |
| - return Q() |
| 98 | + # Build a query that excludes specific inactive seasons |
| 99 | + exclude_query = Q() |
| 100 | + for season in inactive_seasons: |
| 101 | + exclude_query |= Q( |
| 102 | + item__media_type=MediaTypes.SEASON.value, |
| 103 | + item__media_id=season.item.media_id, |
| 104 | + item__season_number=season.item.season_number, |
| 105 | + ) |
99 | 106 |
|
100 |
| - return Q( |
101 |
| - item__media_type=MediaTypes.SEASON.value, |
102 |
| - item__media_id__in=tv_shows_with_active_latest_season, |
| 107 | + return ( |
| 108 | + Q( |
| 109 | + item__media_type=MediaTypes.SEASON.value, |
| 110 | + item__media_id__in=active_tv_shows, |
| 111 | + ) |
| 112 | + & ~exclude_query |
103 | 113 | )
|
104 | 114 |
|
105 | 115 | def sort_with_sentinel_last(self, queryset):
|
|
0 commit comments