<commit_before>#!/usr/bin/env python3
#
# Copyright 2016 Red Hat, Inc.
#
# Authors:
#     <NAME> <<EMAIL>>
#
# This work is licensed under the MIT License.  Please see the LICENSE file or
# http://opensource.org/licenses/MIT.
import urllib

from django.shortcuts import render
from django.http import HttpResponse, Http404
from django.db.models import Exists, OuterRef
from django.urls import reverse
from django.utils.html import format_html
from django.conf import settings
import api
from mod import dispatch_module_hook
import subprocess

PAGE_SIZE = 50


def try_get_git_head():
    try:
        return (
            "-"
            + subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).decode()
        )
    except Exception:
        return ""


def render_page(request, template_name, **data):
    data["patchew_version"] = settings.VERSION + try_get_git_head()
    dispatch_module_hook("render_page_hook", request=request, context_data=data)
    return render(request, template_name, context=data)


def prepare_message(request, project, m, detailed):
    name, addr = m.sender
    m.sender_full_name = "%s <%s>" % (name, addr)
    m.sender_display_name = name or addr
    m.age = m.get_age()
    m.url = reverse(
        "series_detail", kwargs={"project": project.name, "message_id": m.message_id}
    )
    m.status_tags = []
    m.extra_links = []
    if m.is_series_head:
        m.num_patches = m.get_num_patches()
        m.total_patches = m.get_total_patches()
        if m.num_patches < m.total_patches:
            missing = m.total_patches - m.num_patches
            m.status_tags.append(
                {
                    "title": "Series not complete (%d %s not received)"
                    % (missing, "patches" if missing > 1 else "patch"),
                    "type": "warning",
                    "char": "?",
                }
            )

    # hook points for plugins
    m.has_other_revisions = False
    m.extra_status = []
    m.extra_ops = []
    dispatch_module_hook(
        "prepare_message_hook", request=request, message=m, detailed=detailed
    )
    if m.is_merged:
        m.status_tags = [
            {"title": "Series merged", "type": "success", "char": "Merged"}
        ]
    return m


def prepare_patches(request, m, max_depth=None):
    if m.total_patches == 1:
        return []
    replies = m.get_replies().filter(is_patch=True)
    commit_replies = api.models.Message.objects.filter(
        in_reply_to=OuterRef("message_id")
    )
    replies = replies.annotate(has_replies=Exists(commit_replies))
    project = m.project
    return [prepare_message(request, project, x, True) for x in replies]


def prepare_series(request, s, skip_patches=False):
    r = []
    project = s.project

    def add_msg_recurse(m, skip_patches, depth=0):
        a = prepare_message(request, project, m, True)
        a.indent_level = min(depth, 4)
        r.append(a)
        replies = m.get_replies()
        non_patches = [x for x in replies if not x.is_patch]
        patches = []
        if not skip_patches:
            patches = [x for x in replies if x.is_patch]
        for x in non_patches + patches:
            add_msg_recurse(x, False, depth + 1)

    add_msg_recurse(s, skip_patches)
    return r


def prepare_results(request, obj):
    rendered_results = []
    for result in obj.results.all():
        html = result.render()
        if html is None:
            continue
        result.html = html
        rendered_results.append(result)
    return rendered_results


def prepare_series_list(request, sl):
    return [prepare_message(request, s.project, s, False) for s in sl]


def prepare_projects():
    return api.models.Project.objects.filter(parent_project=None).order_by(
        "-display_order", "name"
    )


def view_project_list(request):
    return render_page(request, "project-list.html", projects=prepare_projects())


def gen_page_links(total, cur_page, pagesize, extra_params):
    max_page = int((total + pagesize - 1) / pagesize)
    ret = []
    ddd = False
    for i in range(1, max_page + 1):
        if i == cur_page:
            ret.append(
                {
                    "title": str(i),
                    "url": "?page=" + str(i) + extra_params,
                    "class": "active",
                }
            )
            ddd = False
        elif i < 10 or abs(i - cur_page) < 3 or max_page - i < 3:
            ret.append({"title": str(i), "url": "?page=" + str(i) + extra_params})
            ddd = False
        else:
            if not ddd:
                ret.append({"title": "...", "class": "disabled", "url": "#"})
                ddd = True

    return ret


def get_page_from_request(request):
    try:
        return int(request.GET["page"])
    except Exception:
        return 1


def prepare_navigate_list(cur, *path):
    """ each path is (view_name, kwargs, title) """
    r = [{"url": reverse("project_list"), "title": "Patchew"}]
    for it in path:
        r.append({"url": reverse(it[0], kwargs=it[1]), "title": it[2]})
    r.append({"title": cur, "url": "", "class": "active"})
    return r


def render_series_list_page(request, query, search=None, project=None, keywords=[]):
    sort = request.GET.get("sort")
    if sort == "replied":
        sortfield = "-last_reply_date"
        order_by_reply = True
    else:
        sortfield = "-date"
        order_by_reply = False
    if sortfield:
        query = query.order_by(sortfield)
    cur_page = get_page_from_request(request)
    start = (cur_page - 1) * PAGE_SIZE
    series = query[start : start + PAGE_SIZE]
    params = ""
    if sort:
        params += "&" + urllib.parse.urlencode({"sort": sort})<commit_msg>return 404 when after the last page<commit_after>#!/usr/bin/env python3
#
# Copyright 2016 Red Hat, Inc.
#
# Authors:
#     <NAME> <<EMAIL>>
#
# This work is licensed under the MIT License.  Please see the LICENSE file or
# http://opensource.org/licenses/MIT.
import urllib

from django.shortcuts import render
from django.http import HttpResponse, Http404
from django.db.models import Exists, OuterRef
from django.urls import reverse
from django.utils.html import format_html
from django.conf import settings
import api
from mod import dispatch_module_hook
import subprocess

PAGE_SIZE = 50


def try_get_git_head():
    try:
        return (
            "-"
            + subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).decode()
        )
    except Exception:
        return ""


def render_page(request, template_name, **data):
    data["patchew_version"] = settings.VERSION + try_get_git_head()
    dispatch_module_hook("render_page_hook", request=request, context_data=data)
    return render(request, template_name, context=data)


def prepare_message(request, project, m, detailed):
    name, addr = m.sender
    m.sender_full_name = "%s <%s>" % (name, addr)
    m.sender_display_name = name or addr
    m.age = m.get_age()
    m.url = reverse(
        "series_detail", kwargs={"project": project.name, "message_id": m.message_id}
    )
    m.status_tags = []
    m.extra_links = []
    if m.is_series_head:
        m.num_patches = m.get_num_patches()
        m.total_patches = m.get_total_patches()
        if m.num_patches < m.total_patches:
            missing = m.total_patches - m.num_patches
            m.status_tags.append(
                {
                    "title": "Series not complete (%d %s not received)"
                    % (missing, "patches" if missing > 1 else "patch"),
                    "type": "warning",
                    "char": "?",
                }
            )

    # hook points for plugins
    m.has_other_revisions = False
    m.extra_status = []
    m.extra_ops = []
    dispatch_module_hook(
        "prepare_message_hook", request=request, message=m, detailed=detailed
    )
    if m.is_merged:
        m.status_tags = [
            {"title": "Series merged", "type": "success", "char": "Merged"}
        ]
    return m


def prepare_patches(request, m, max_depth=None):
    if m.total_patches == 1:
        return []
    replies = m.get_replies().filter(is_patch=True)
    commit_replies = api.models.Message.objects.filter(
        in_reply_to=OuterRef("message_id")
    )
    replies = replies.annotate(has_replies=Exists(commit_replies))
    project = m.project
    return [prepare_message(request, project, x, True) for x in replies]


def prepare_series(request, s, skip_patches=False):
    r = []
    project = s.project

    def add_msg_recurse(m, skip_patches, depth=0):
        a = prepare_message(request, project, m, True)
        a.indent_level = min(depth, 4)
        r.append(a)
        replies = m.get_replies()
        non_patches = [x for x in replies if not x.is_patch]
        patches = []
        if not skip_patches:
            patches = [x for x in replies if x.is_patch]
        for x in non_patches + patches:
            add_msg_recurse(x, False, depth + 1)

    add_msg_recurse(s, skip_patches)
    return r


def prepare_results(request, obj):
    rendered_results = []
    for result in obj.results.all():
        html = result.render()
        if html is None:
            continue
        result.html = html
        rendered_results.append(result)
    return rendered_results


def prepare_series_list(request, sl):
    return [prepare_message(request, s.project, s, False) for s in sl]


def prepare_projects():
    return api.models.Project.objects.filter(parent_project=None).order_by(
        "-display_order", "name"
    )


def view_project_list(request):
    return render_page(request, "project-list.html", projects=prepare_projects())


def gen_page_links(total, cur_page, pagesize, extra_params):
    max_page = int((total + pagesize - 1) / pagesize)
    ret = []
    ddd = False
    for i in range(1, max_page + 1):
        if i == cur_page:
            ret.append(
                {
                    "title": str(i),
                    "url": "?page=" + str(i) + extra_params,
                    "class": "active",
                }
            )
            ddd = False
        elif i < 10 or abs(i - cur_page) < 3 or max_page - i < 3:
            ret.append({"title": str(i), "url": "?page=" + str(i) + extra_params})
            ddd = False
        else:
            if not ddd:
                ret.append({"title": "...", "class": "disabled", "url": "#"})
                ddd = True

    return ret


def get_page_from_request(request):
    try:
        return int(request.GET["page"])
    except Exception:
        return 1


def prepare_navigate_list(cur, *path):
    """ each path is (view_name, kwargs, title) """
    r = [{"url": reverse("project_list"), "title": "Patchew"}]
    for it in path:
        r.append({"url": reverse(it[0], kwargs=it[1]), "title": it[2]})
    r.append({"title": cur, "url": "", "class": "active"})
    return r


def render_series_list_page(request, query, search=None, project=None, keywords=[]):
    sort = request.GET.get("sort")
    if sort == "replied":
        sortfield = "-last_reply_date"
        order_by_reply = True
    else:
        sortfield = "-date"
        order_by_reply = False
    if sortfield:
        query = query.order_by(sortfield)
    cur_page = get_page_from_request(request)
    start = (cur_page - 1) * PAGE_SIZE
    series = query[start : start + PAGE_SIZE]
    if not series and cur_page > 1:
        raise Http404("Page not found")
    params = ""
    if sort:
        params += "&" + urllib.parse.urlencode({"sort": sort})
{'fraction_non_alphanumeric': 0.1246311404270092, 'fraction_numerical': 0.0052942197535150145, 'mean_word_length': 2.4376491646778042, 'pattern_counts': {'":': 50, '<': 19, '<?xml version=': 0, '>': 14, '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': '3982683', 'n_tokens_mistral': 3757, 'n_tokens_neox': 3660, 'n_words': 897}