<commit_before>from django.http import HttpResponseNotFound
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.shortcuts import render_to_response
from django.template import RequestContext

from mygpo.data.models import DirectoryEntry
from mygpo.directory.models import Category
from mygpo.web import utils
from mygpo.migrate import use_couchdb


@use_couchdb()
def browse(request, num_categories=10, num_tags_cloud=90, podcasts_per_category=10):
    total = int(num_categories) + int(num_tags_cloud)
    categories = Category.top_categories(total).all()

    disp_categories = []
    for category in categories[:num_categories]:
        entries = DirectoryEntry.objects.podcasts_for_category(category.get_tags())[:podcasts_per_category]
        disp_categories.append({
            'tag': category.label,
            'entries': entries
            })

    tag_cloud = categories[num_categories:]

    tag_cloud.sort(key = lambda x: x.label.lower())
    max_entries = max([t.weight for t in tag_cloud])

    return render_to_response('directory.html', {
        'categories': disp_categories,
        'tag_cloud': tag_cloud,
        'max_entries': max_entries,
        }, context_instance=RequestContext(request))


@use_couchdb()
def category(request, category, page_size=20):
    category = Category.for_tag(category)
    if not category:
        return HttpResponseNotFound()
    entries = DirectoryEntry.objects.podcasts_for_category(category.get_tags())

    paginator = Paginator(entries, page_size)

    # Make sure page request is an int. If not, deliver first page.
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    # If page request (9999) is out of range, deliver last page of results.
    try:<commit_msg>prevent bug when directory is empty<commit_after>from django.http import HttpResponseNotFound
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.shortcuts import render_to_response
from django.template import RequestContext

from mygpo.data.models import DirectoryEntry
from mygpo.directory.models import Category
from mygpo.web import utils
from mygpo.migrate import use_couchdb


@use_couchdb()
def browse(request, num_categories=10, num_tags_cloud=90, podcasts_per_category=10):
    total = int(num_categories) + int(num_tags_cloud)
    categories = Category.top_categories(total).all()

    disp_categories = []
    for category in categories[:num_categories]:
        entries = DirectoryEntry.objects.podcasts_for_category(category.get_tags())[:podcasts_per_category]
        disp_categories.append({
            'tag': category.label,
            'entries': entries
            })

    tag_cloud = categories[num_categories:]

    tag_cloud.sort(key = lambda x: x.label.lower())
    max_entries = max([t.weight for t in tag_cloud] + [0])

    return render_to_response('directory.html', {
        'categories': disp_categories,
        'tag_cloud': tag_cloud,
        'max_entries': max_entries,
        }, context_instance=RequestContext(request))


@use_couchdb()
def category(request, category, page_size=20):
    category = Category.for_tag(category)
    if not category:
        return HttpResponseNotFound()
    entries = DirectoryEntry.objects.podcasts_for_category(category.get_tags())

    paginator = Paginator(entries, page_size)

    # Make sure page request is an int. If not, deliver first page.
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    # If page request (9999) is out of range, deliver last page of results.
    try:
{'fraction_non_alphanumeric': 0.0934399117971334, 'fraction_numerical': 0.007993384785005513, 'mean_word_length': 3.8908355795148246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2816588', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1031, 'n_words': 263}