(cls, user, entries, podcasts=None, episodes=None)
return entries
Efficiently loads additional data for a number of entries
Efficiently loads additional data for a number of entries
Efficiently loads additional data for a number of entries
def fetch_data(cls, user, entries, podcasts=None, episodes=None):
        """Efficiently loads additional data for a number of entries"""

        if podcasts is None:
            # load podcast data
            podcast_ids = [getattr(x, "podcast_id", None) for x in entries]
            podcast_ids = filter(None, podcast_ids)
            podcasts = Podcast.objects.filter(id__in=podcast_ids).prefetch_related(
                "slugs"
            )
            podcasts = {podcast.id.hex: podcast for podcast in podcasts}

        if episodes is None:
            # load episode data
            episode_ids = [getattr(x, "episode_id", None) for x in entries]
            episode_ids = filter(None, episode_ids)
            episodes = (
                Episode.objects.filter(id__in=episode_ids)
                .select_related("podcast")
                .prefetch_related("slugs", "podcast__slugs")
            )
            episodes = {episode.id.hex: episode for episode in episodes}

        # load device data
        # does not need pre-populated data because no db-access is required
        device_ids = [getattr(x, "device_id", None) for x in entries]
        device_ids = filter(None, device_ids)
        devices = {client.id.hex: client for client in user.client_set.all()}

        for entry in entries:
            podcast_id = getattr(entry, "podcast_id", None)
            entry.podcast = podcasts.get(podcast_id, None)

            episode_id = getattr(entry, "episode_id", None)
            entry.episode = episodes.get(episode_id, None)

            if hasattr(entry, "user"):
                entry.user = user

            device = devices.get(getattr(entry, "device_id", None), None)
            entry.device = device

        return entries
def fetch_data ( cls , user , entries , podcasts = None , episodes = None ) : if podcasts is None : # load podcast data podcast_ids = [ getattr ( x , "podcast_id" , None ) for x in entries ] podcast_ids = filter ( None , podcast_ids ) podcasts = Podcast . objects . filter ( id__in = podcast_ids ) . prefetch_related ( "slugs" ) podcasts = { podcast . id . hex : podcast for podcast in podcasts } if episodes is None : # load episode data episode_ids = [ getattr ( x , "episode_id" , None ) for x in entries ] episode_ids = filter ( None , episode_ids ) episodes = ( Episode . objects . filter ( id__in = episode_ids ) . select_related ( "podcast" ) . prefetch_related ( "slugs" , "podcast__slugs" ) ) episodes = { episode . id . hex : episode for episode in episodes } # load device data # does not need pre-populated data because no db-access is required device_ids = [ getattr ( x , "device_id" , None ) for x in entries ] device_ids = filter ( None , device_ids ) devices = { client . id . hex : client for client in user . client_set . all ( ) } for entry in entries : podcast_id = getattr ( entry , "podcast_id" , None ) entry . podcast = podcasts . get ( podcast_id , None ) episode_id = getattr ( entry , "episode_id" , None ) entry . episode = episodes . get ( episode_id , None ) if hasattr ( entry , "user" ) : entry . user = user device = devices . get ( getattr ( entry , "device_id" , None ) , None ) entry . device = device return entries