Advertisement
Guest User

Untitled

a guest
Jan 11th, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1.     def get_queryset(self):
  2.         user = self.request.user
  3.  
  4.         queryset = Notification.objects.annotate(Count('album'), Count('video'))
  5.         notifs_of_albums = queryset.filter(album__count=1)
  6.         notifs_of_videos = queryset.filter(video__count=1)
  7.  
  8.         notifs_of_albums_to_user_ids = []
  9.         notifs_of_videos_to_user_ids = []
  10.         for subscription in user.subscriptions.all():
  11.             if subscription.content_type.model == 'artist':
  12.                 notif = notifs_of_albums.filter(
  13.                     album__artist=subscription.artist.first(),
  14.                     album__release_date__gte=subscription.datetime_committed
  15.                 ).first()
  16.  
  17.                 try:
  18.                     notifs_of_albums_to_user_ids.append(notif.pk)
  19.                 except AttributeError:
  20.                     pass
  21.  
  22.             elif subscription.content_type.model == 'channel':
  23.                 notif = notifs_of_videos.filter(
  24.                     video__channel=subscription.channel.first(),
  25.                     video__release_datetime__gte=subscription.datetime_committed
  26.                 ).first()
  27.  
  28.                 try:
  29.                     notifs_of_videos_to_user_ids.append(notif.pk)
  30.                 except AttributeError:
  31.                     pass
  32.  
  33.         notifs_of_albums_to_user = notifs_of_albums.filter(
  34.             pk__in=notifs_of_albums_to_user_ids
  35.         )
  36.         notifs_of_videos_to_user = notifs_of_videos.filter(
  37.             pk__in=notifs_of_videos_to_user_ids
  38.         )
  39.  
  40.         all_notifs_to_user = notifs_of_albums_to_user.union(notifs_of_videos_to_user)
  41.         return all_notifs_to_user.order_by('-creation_date_time')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement