<commit_before>from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import get_object_or_404, render
from django.http import Http404
from django.utils.http import urlencode

from wagtail.wagtailadmin.modal_workflow import render_modal_workflow
from wagtail.wagtailadmin.forms import SearchForm, ExternalLinkChooserForm, ExternalLinkChooserWithLinkTextForm, EmailLinkChooserForm, EmailLinkChooserWithLinkTextForm

from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.utils import resolve_model_string


def get_querystring(request):
    return urlencode({
        'page_type': request.GET.get('page_type', ''),
        'allow_external_link': request.GET.get('allow_external_link', ''),
        'allow_email_link': request.GET.get('allow_email_link', ''),
        'prompt_for_link_text': request.GET.get('prompt_for_link_text', ''),
    })


def shared_context(request, extra_context={}):
    context = {
        'allow_external_link': request.GET.get('allow_external_link'),
        'allow_email_link': request.GET.get('allow_email_link'),
        'querystring': get_querystring(request),
    }
    context.update(extra_context)
    return context


def page_model_from_string(string):
    page_model = resolve_model_string(string)

    if not issubclass(page_model, Page):
        raise ValueError("Model is not a page")

    return page_model


def browse(request, parent_page_id=None):
    # Find parent page
    if parent_page_id:
        parent_page = get_object_or_404(Page, id=parent_page_id)
    else:
        parent_page = Page.get_first_root_node()

    # Get children of parent page
    pages = parent_page.get_children()

    # Filter them by page type
    page_type_string = request.GET.get('page_type', 'wagtailcore.page')
    if page_type_string != 'wagtailcore.page':
        try:
            desired_class = page_model_from_string(page_type_string)
        except (ValueError, LookupError):
            raise Http404

        # restrict the page listing to just those pages that:
        # - are of the given content type (taking into account class inheritance)
        # - or can be navigated into (i.e. have children)
        choosable_pages = pages.type(desired_class)
        descendable_pages = pages.filter(numchild__gt=0)
        pages = choosable_pages | descendable_pages
    else:
        desired_class = Page

    # Parent page can be chosen if it is a instance of desired_class
    parent_page.can_choose = issubclass(parent_page.specific_class, desired_class)

    # Pagination
    # We apply pagination first so we don't need to walk the entire list
    # in the block below
    p = request.GET.get('p', 1)
    paginator = Paginator(pages, 25)
    try:
        pages = paginator.page(p)
    except PageNotAnInteger:
        pages = paginator.page(1)
    except EmptyPage:
        pages = paginator.page(paginator.num_pages)

    # Annotate each page with can_choose/can_decend flags
    for page in pages:
        if desired_class == Page:
            page.can_choose = True
        else:
            page.can_choose = issubclass(page.specific_class, desired_class)

        page.can_descend = page.get_children_count()<commit_msg>Put Page class fallback back<commit_after>from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import get_object_or_404, render
from django.http import Http404
from django.utils.http import urlencode

from wagtail.wagtailadmin.modal_workflow import render_modal_workflow
from wagtail.wagtailadmin.forms import SearchForm, ExternalLinkChooserForm, ExternalLinkChooserWithLinkTextForm, EmailLinkChooserForm, EmailLinkChooserWithLinkTextForm

from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.utils import resolve_model_string


def get_querystring(request):
    return urlencode({
        'page_type': request.GET.get('page_type', ''),
        'allow_external_link': request.GET.get('allow_external_link', ''),
        'allow_email_link': request.GET.get('allow_email_link', ''),
        'prompt_for_link_text': request.GET.get('prompt_for_link_text', ''),
    })


def shared_context(request, extra_context={}):
    context = {
        'allow_external_link': request.GET.get('allow_external_link'),
        'allow_email_link': request.GET.get('allow_email_link'),
        'querystring': get_querystring(request),
    }
    context.update(extra_context)
    return context


def page_model_from_string(string):
    page_model = resolve_model_string(string)

    if not issubclass(page_model, Page):
        raise ValueError("Model is not a page")

    return page_model


def browse(request, parent_page_id=None):
    # Find parent page
    if parent_page_id:
        parent_page = get_object_or_404(Page, id=parent_page_id)
    else:
        parent_page = Page.get_first_root_node()

    # Get children of parent page
    pages = parent_page.get_children()

    # Filter them by page type
    page_type_string = request.GET.get('page_type', 'wagtailcore.page')
    if page_type_string != 'wagtailcore.page':
        try:
            desired_class = page_model_from_string(page_type_string)
        except (ValueError, LookupError):
            raise Http404

        # restrict the page listing to just those pages that:
        # - are of the given content type (taking into account class inheritance)
        # - or can be navigated into (i.e. have children)
        choosable_pages = pages.type(desired_class)
        descendable_pages = pages.filter(numchild__gt=0)
        pages = choosable_pages | descendable_pages
    else:
        desired_class = Page

    # Parent page can be chosen if it is a instance of desired_class
    parent_page.can_choose = issubclass(parent_page.specific_class or Page, desired_class)

    # Pagination
    # We apply pagination first so we don't need to walk the entire list
    # in the block below
    p = request.GET.get('p', 1)
    paginator = Paginator(pages, 25)
    try:
        pages = paginator.page(p)
    except PageNotAnInteger:
        pages = paginator.page(1)
    except EmptyPage:
        pages = paginator.page(paginator.num_pages)

    # Annotate each page with can_choose/can_decend flags
    for page in pages:
        if desired_class == Page:
            page.can_choose = True
        else:
            page.can_choose = issubclass(page.specific_class or Page, desired_class)

        page.can_descend = page.get_children_count()
{'fraction_non_alphanumeric': 0.08285670119029216, 'fraction_numerical': 0.005255835523264802, 'mean_word_length': 3.5213137665967853, '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': '2541295', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1929, 'n_words': 465}