Beispiel #1
0
    def apply_sync_actions(self, device, sync_actions):
        """ Applies the sync-actions to the device """

        add, rem = sync_actions

        podcasts = podcasts_to_dict(add + rem)

        for podcast_id in add:
            podcast = podcasts.get(podcast_id, None)
            if podcast is None:
                continue
            try:
                podcast.subscribe(self, device)
            except SubscriptionException as e:
                logger.warn('Web: %(username)s: cannot sync device: %(error)s' %
                    dict(username=self.username, error=repr(e)))

        for podcast_id in rem:
            podcast = podcasts.get(podcast_id, None)
            if not podcast:
                continue

            try:
                podcast.unsubscribe(self, device)
            except SubscriptionException as e:
                logger.warn('Web: %(username)s: cannot sync device: %(error)s' %
                    dict(username=self.username, error=repr(e)))
Beispiel #2
0
def episode_toplist(request, num=100):
    lang = process_lang_params(request)

    toplist = EpisodeToplist(language=lang)
    entries = list(map(proxy_object, toplist[:num]))

    # load podcast objects
    podcast_ids = [e.podcast for e in entries]
    podcasts = podcasts_to_dict(podcast_ids, True)
    for entry in entries:
        entry.podcast = podcasts.get(entry.podcast, None)

    current_site = RequestSite(request)

    # Determine maximum listener amount (or 0 if no entries exist)
    max_listeners = max([0]+[e.listeners for e in entries])

    languages = get_podcast_languages()
    all_langs = get_language_names(languages)

    return render(request, 'episode_toplist.html', {
        'entries': entries,
        'max_listeners': max_listeners,
        'url': current_site,
        'language': lang,
        'all_languages': all_langs,
    })
Beispiel #3
0
def list_favorites(request):
    user = request.user
    site = RequestSite(request)

    episodes = favorite_episodes_for_user(user)

    recently_listened = get_latest_episodes(user)

    podcast_ids = [episode.podcast for episode in episodes + recently_listened]
    podcasts = podcasts_to_dict(podcast_ids)

    recently_listened = fetch_episode_data(recently_listened, podcasts=podcasts)
    episodes = fetch_episode_data(episodes, podcasts=podcasts)

    favfeed = FavoriteFeed(user)
    feed_url = favfeed.get_public_url(site.domain)

    podcast = podcast_for_url(feed_url)

    token = request.user.favorite_feeds_token

    return render(request, 'favorites.html', {
        'episodes': episodes,
        'feed_token': token,
        'site': site,
        'podcast': podcast,
        'recently_listened': recently_listened,
        })
Beispiel #4
0
def podcasts_for_states(podcast_states):
    """ returns the podcasts corresponding to the podcast states """

    podcast_ids = [state.podcast for state in podcast_states]
    podcasts = podcasts_to_dict(podcast_ids)

    for state in podcast_states:
        podcast = proxy_object(podcasts[state.podcast], url=state.ref_url)
        podcasts[state.podcast] = podcast

    return podcasts.values()
Beispiel #5
0
def fetch_episode_data(episodes, podcasts={}):

    if not podcasts:
        podcast_ids = [episode.podcast for episode in episodes]
        podcasts = podcasts_to_dict(podcast_ids)

    def set_podcast(episode):
        episode = proxy_object(episode)
        episode.podcast = podcasts.get(episode.podcast, None)
        return episode

    return map(set_podcast, episodes)
Beispiel #6
0
def privacy(request):
    site = RequestSite(request)

    subscriptions = subscriptions_by_user(request.user)
    podcasts = podcasts_to_dict([x[1] for x in subscriptions])

    subs = set((podcasts.get(x[1], None), not x[0]) for x in subscriptions)
    subs = sorted(subs, key=lambda (p, _): PODCAST_SORT(p))

    return render(request, 'privacy.html', {
        'private_subscriptions': not request.user.get_wksetting(PUBLIC_SUB_USER),
        'subscriptions': subs,
        'domain': site.domain,
        })
Beispiel #7
0
    def get_subscribed_podcasts(self, public=None):
        """ Returns all subscribed podcasts for the user

        The attribute "url" contains the URL that was used when subscribing to
        the podcast """

        from mygpo.db.couchdb.podcast_state import get_subscribed_podcast_states_by_user
        states = get_subscribed_podcast_states_by_user(self, public)
        podcast_ids = [state.podcast for state in states]
        podcasts = podcasts_to_dict(podcast_ids)

        for state in states:
            podcast = podcasts.get(state.podcast, None)
            if podcast is None:
                continue

            podcast = proxy_object(podcast, url=state.ref_url)
            podcasts[state.podcast] = podcast

        return set(podcasts.values())
Beispiel #8
0
    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 = podcasts_to_dict(podcast_ids)

        if episodes is None:
            from mygpo.db.couchdb.episode import episodes_to_dict
            # load episode data
            episode_ids = [getattr(x, 'episode_id', None) for x in entries]
            episode_ids = filter(None, episode_ids)
            episodes = episodes_to_dict(episode_ids)

        # 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 = dict([ (id, user.get_device(id)) for id in device_ids])


        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
Beispiel #9
0
def create_subscriptionlist(request):
    user = request.user
    subscriptions = subscriptions_by_user(user)

    if not subscriptions:
        return []

    # Load all Podcasts and Devices first to ensure that they are
    # only loaded once, not for each occurance in a subscription
    public, podcast_ids, device_ids = unzip(subscriptions)
    podcast_ids= list(set(podcast_ids))
    device_ids = list(set(device_ids))

    podcasts = podcasts_to_dict(podcast_ids)
    devices = user.get_devices_by_id(device_ids)

    subscription_list = {}
    for public, podcast_id, device_id in subscriptions:
        device = devices.get(device_id)
        if not podcast_id in subscription_list:
            podcast = podcasts.get(podcast_id, None)
            if podcast is None:
                continue

            subscription_list[podcast_id] = {
                'podcast': podcasts[podcast_id],
                'devices': [device] if device else [],
                'episodes': podcast.episode_count,
            }
        else:
            if device:
                subscription_list[podcast_id]['devices'].append(device)

    subscriptions = subscription_list.values()
    sort_key = lambda s: s['podcast'].latest_episode_timestamp or datetime.utcnow()
    subscriptions = sorted(subscriptions, key=sort_key, reverse=True)
    return subscriptions