<commit_before>from django import models
from django.core.template import loader
from django.utils.httpwrappers import HttpResponse
from django.core.xheaders import populate_xheaders
from django.core.extensions import DjangoContext
from django.core.paginator import ObjectPaginator, InvalidPage
from django.core.exceptions import Http404, ObjectDoesNotExist

def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False,
        template_name=None, template_loader=loader, extra_lookup_kwargs={},
        extra_context={}, context_processors=None):
    """
    Generic list of objects.

    Templates: ``<app_label>/<module_name>_list``
    Context:
        object_list
            list of objects
        is_paginated
            are the results paginated?
        results_per_page
            number of objects per page (if paginated)
        has_next
            is there a next page?
        has_previous
            is there a prev page?
        page
            the current page
        next
            the next page
        previous
            the previous page
        pages
            number of pages, total
        hits
            number of objects, total
    """
    mod = models.get_module(app_label, module_name)
    lookup_kwargs = extra_lookup_kwargs.copy()
    if paginate_by:
        paginator = ObjectPaginator(mod, lookup_kwargs, paginate_by)
        page = request.GET.get('page', 0)
        try:
            object_list = paginator.get_page(page)
        except InvalidPage:
            if page == 0 and allow_empty:
                object_list = []
            else:
                raise Http404
        page = int(page)
        c = DjangoContext(request, {
            'object_list': object_list,
            'is_paginated': paginator.pages > 1,
            'results_per_page': paginate_by,
            'has_next': paginator.has_next_page(page),
            'has_previous': paginator.has_previous_page(page),
            'page': page + 1,
            'next': page + 1,
            'previous': page - 1,
            'pages': paginator.pages,
            'hits' : paginator.hits,
        }, context_processors)
    else:
        object_list = mod.get_list(**lookup_kwargs)
        c = DjangoContext(request, {
            'object_list': object_list,
            'is_paginated': False
        }, context_processors)
        if len(object_list) == 0 and not allow_empty:
            raise Http404
    for key, value in extra_context.items():
        if callable(value):
            c[key] = value()
        else:
            c[key] = value
    if not template_name:
        template_name = "%s/%s_list" % (app_label, module_name)
    t = template_loader.get_template(template_name)
    return HttpResponse(t.render(c))

def object_detail(request, app_label, module_name, object_id=None, slug=None,
        slug_field=None, template_name=None, template_name_field=None,
        template_loader=loader, extra_lookup_kwargs={}, extra_context={},
        context_processors=None):
    """
    Generic list of objects.<commit_msg>Fixed #1033 -- pagination in object_list generic views now use 1-indexed page numbers in the URL.  Thanks, <NAME>.<commit_after>from django import models
from django.core.template import loader
from django.utils.httpwrappers import HttpResponse
from django.core.xheaders import populate_xheaders
from django.core.extensions import DjangoContext
from django.core.paginator import ObjectPaginator, InvalidPage
from django.core.exceptions import Http404, ObjectDoesNotExist

def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False,
        template_name=None, template_loader=loader, extra_lookup_kwargs={},
        extra_context={}, context_processors=None):
    """
    Generic list of objects.

    Templates: ``<app_label>/<module_name>_list``
    Context:
        object_list
            list of objects
        is_paginated
            are the results paginated?
        results_per_page
            number of objects per page (if paginated)
        has_next
            is there a next page?
        has_previous
            is there a prev page?
        page
            the current page
        next
            the next page
        previous
            the previous page
        pages
            number of pages, total
        hits
            number of objects, total
    """
    mod = models.get_module(app_label, module_name)
    lookup_kwargs = extra_lookup_kwargs.copy()
    if paginate_by:
        paginator = ObjectPaginator(mod, lookup_kwargs, paginate_by)
        page = request.GET.get('page', 1)
        try:
            page = int(page)
            object_list = paginator.get_page(page - 1)
        except (InvalidPage, ValueError):
            if page == 1 and allow_empty:
                object_list = []
            else:
                raise Http404
        c = DjangoContext(request, {
            'object_list': object_list,
            'is_paginated': paginator.pages > 1,
            'results_per_page': paginate_by,
            'has_next': paginator.has_next_page(page - 1),
            'has_previous': paginator.has_previous_page(page - 1),
            'page': page,
            'next': page + 1,
            'previous': page - 1,
            'pages': paginator.pages,
            'hits' : paginator.hits,
        }, context_processors)
    else:
        object_list = mod.get_list(**lookup_kwargs)
        c = DjangoContext(request, {
            'object_list': object_list,
            'is_paginated': False
        }, context_processors)
        if len(object_list) == 0 and not allow_empty:
            raise Http404
    for key, value in extra_context.items():
        if callable(value):
            c[key] = value()
        else:
            c[key] = value
    if not template_name:
        template_name = "%s/%s_list" % (app_label, module_name)
    t = template_loader.get_template(template_name)
    return HttpResponse(t.render(c))

def object_detail(request, app_label, module_name, object_id=None, slug=None,
        slug_field=None, template_name=None, template_name_field=None,
        template_loader=loader, extra_lookup_kwargs={}, extra_context={},
        context_processors=None):
    """
    Generic list of objects.
{'fraction_non_alphanumeric': 0.08448908018491949, 'fraction_numerical': 0.006217120994739359, 'mean_word_length': 2.2848167539267017, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, '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': '4105150', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1747, 'n_words': 446}