<commit_before>from functools import wraps, partial

from django.core.urlresolvers import reverse
from django.http import HttpResponseBadRequest, HttpResponseRedirect, Http404
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import RequestSite
from django.utils.translation import ugettext as _
from django.contrib import messages
from django.views.decorators.vary import vary_on_cookie
from django.views.decorators.cache import never_cache, cache_control

from mygpo.core.models import PodcastGroup, SubscriptionException
from mygpo.core.proxy import proxy_object
from mygpo.core.tasks import flattr_thing
from mygpo.utils import normalize_feed_url
from mygpo.users.settings import PUBLIC_SUB_PODCAST, FLATTR_TOKEN
from mygpo.publisher.utils import check_publisher_permission
from mygpo.users.models import HistoryEntry, DeviceDoesNotExist, SubscriptionAction
from mygpo.web.forms import SyncForm
from mygpo.decorators import allowed_methods, repeat_on_conflict
from mygpo.web.utils import get_podcast_link_target, get_page_list
from mygpo.db.couchdb.episode import episodes_for_podcast
from mygpo.db.couchdb.podcast import podcast_for_slug, podcast_for_slug_id, \
         podcast_for_oldid, podcast_for_url
from mygpo.db.couchdb.podcast_state import podcast_state_for_user_podcast, \
         add_subscription_action
from mygpo.db.couchdb.episode_state import get_podcasts_episode_states, \
         episode_listener_counts
from mygpo.db.couchdb.directory import tags_for_user, tags_for_podcast

import logging
logger = logging.getLogger(__name__)


MAX_TAGS_ON_PAGE=50


@repeat_on_conflict(['state'])
def update_podcast_settings(state, is_public):
    state.settings[PUBLIC_SUB_PODCAST.name] = is_public
    state.save()


@vary_on_cookie
@cache_control(private=True)
@allowed_methods(['GET'])
def show_slug(request, slug):
    podcast = podcast_for_slug(slug)

    if slug != podcast.slug:
        target = reverse('podcast_slug', args=[podcast.slug])
        return HttpResponseRedirect(target)

    return show(request, podcast.oldid)


@vary_on_cookie
@cache_control(private=True)
@allowed_methods(['GET'])
def show(request, podcast):
    """ Shows a podcast detail page """

    current_site = RequestSite(request)
    episodes = episode_list(podcast, request.user, limit=20)
    user = request.user

    max_listeners = max([e.listeners for e in episodes] + [0])

    episode = None

    if episodes:
        episode = episodes[0]
        episodes = episodes[1:]

    if podcast.group:
        group = PodcastGroup.get(podcast.group)
        rel_podcasts = filter(lambda x: x != podcast, group.podcasts)
    else:
        rel_podcasts = []

    tags = get_tags(podcast, user)

    if user.is_authenticated():
        state = podcast_state_for_user_podcast(user, podcast)
        subscribed_devices = state.get_subscribed_device_ids()
        subscribed_devices = [user.get_device(x) for x in subscribed_devices]

        subscribe_targets = podcast.subscribe_targets(user)

        has_history = bool(state.actions)
        is_public = state.settings.get('public_subscription', True)
        can_flattr = request.user.get_wksetting(FLATTR_TOKEN) and podcast.flattr_url

    else:
        has_history = False
        is_public = False
        subscribed_devices = []
        subscribe_targets = []
        can_flattr = False

    is_publisher = check_publisher_permission(user, podcast)

    return render(request, 'podcast.html', {
        'tags': tags,
        'url': current_site,
        'has_history': has_history,
        'podcast': podcast,
        'is_public': is_public,
        'devices': subscribed_devices,
        'related_podcasts': rel_podcasts,
        'can_subscribe': len(subscribe_targets) > 0,
        'subscribe_targets': subscribe_targets,
        'episode': episode,
        'episodes': episodes,
        'max_listeners': max_listeners,
        'can_flattr': can_flattr,
        'is_publisher': is_publisher,
    })


def get_tags(podcast, user):<commit_msg>add episode pagination to podcast page<commit_after>from functools import wraps, partial

from django.core.urlresolvers import reverse
from django.http import HttpResponseBadRequest, HttpResponseRedirect, Http404
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import RequestSite
from django.utils.translation import ugettext as _
from django.contrib import messages
from django.views.decorators.vary import vary_on_cookie
from django.views.decorators.cache import never_cache, cache_control

from mygpo.core.models import PodcastGroup, SubscriptionException
from mygpo.core.proxy import proxy_object
from mygpo.core.tasks import flattr_thing
from mygpo.utils import normalize_feed_url
from mygpo.users.settings import PUBLIC_SUB_PODCAST, FLATTR_TOKEN
from mygpo.publisher.utils import check_publisher_permission
from mygpo.users.models import HistoryEntry, DeviceDoesNotExist, SubscriptionAction
from mygpo.web.forms import SyncForm
from mygpo.decorators import allowed_methods, repeat_on_conflict
from mygpo.web.utils import get_podcast_link_target, get_page_list
from mygpo.db.couchdb.episode import episodes_for_podcast
from mygpo.db.couchdb.podcast import podcast_for_slug, podcast_for_slug_id, \
         podcast_for_oldid, podcast_for_url
from mygpo.db.couchdb.podcast_state import podcast_state_for_user_podcast, \
         add_subscription_action
from mygpo.db.couchdb.episode_state import get_podcasts_episode_states, \
         episode_listener_counts
from mygpo.db.couchdb.directory import tags_for_user, tags_for_podcast

import logging
logger = logging.getLogger(__name__)


MAX_TAGS_ON_PAGE=50


@repeat_on_conflict(['state'])
def update_podcast_settings(state, is_public):
    state.settings[PUBLIC_SUB_PODCAST.name] = is_public
    state.save()


@vary_on_cookie
@cache_control(private=True)
@allowed_methods(['GET'])
def show_slug(request, slug):
    podcast = podcast_for_slug(slug)

    if slug != podcast.slug:
        target = reverse('podcast_slug', args=[podcast.slug])
        return HttpResponseRedirect(target)

    return show(request, podcast.oldid)


@vary_on_cookie
@cache_control(private=True)
@allowed_methods(['GET'])
def show(request, podcast):
    """ Shows a podcast detail page """

    current_site = RequestSite(request)
    num_episodes = 20
    episodes = episode_list(podcast, request.user, limit=num_episodes)
    user = request.user

    max_listeners = max([e.listeners for e in episodes] + [0])

    episode = None

    if episodes:
        episode = episodes[0]
        episodes = episodes[1:]

    if podcast.group:
        group = PodcastGroup.get(podcast.group)
        rel_podcasts = filter(lambda x: x != podcast, group.podcasts)
    else:
        rel_podcasts = []

    tags = get_tags(podcast, user)

    if user.is_authenticated():
        state = podcast_state_for_user_podcast(user, podcast)
        subscribed_devices = state.get_subscribed_device_ids()
        subscribed_devices = [user.get_device(x) for x in subscribed_devices]

        subscribe_targets = podcast.subscribe_targets(user)

        has_history = bool(state.actions)
        is_public = state.settings.get('public_subscription', True)
        can_flattr = request.user.get_wksetting(FLATTR_TOKEN) and podcast.flattr_url

    else:
        has_history = False
        is_public = False
        subscribed_devices = []
        subscribe_targets = []
        can_flattr = False

    is_publisher = check_publisher_permission(user, podcast)

    episodes_total = podcast.episode_count or 0
    num_pages = episodes_total / num_episodes
    page_list = get_page_list(1, num_pages, 1, 15)

    return render(request, 'podcast.html', {
        'tags': tags,
        'url': current_site,
        'has_history': has_history,
        'podcast': podcast,
        'is_public': is_public,
        'devices': subscribed_devices,
        'related_podcasts': rel_podcasts,
        'can_subscribe': len(subscribe_targets) > 0,
        'subscribe_targets': subscribe_targets,
        'episode': episode,
        'episodes': episodes,
        'max_listeners': max_listeners,
        'can_flattr': can_flattr,
        'is_publisher': is_publisher,
        'page_list': page_list,
        'current_page': 1,
    })


def get_tags(podcast, user):
{'fraction_non_alphanumeric': 0.08273809523809524, 'fraction_numerical': 0.0033333333333333335, 'mean_word_length': 4.1225609756097565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '633620', 'n_tokens_mistral': 2671, 'n_tokens_neox': 2596, 'n_words': 558}