summaryrefslogtreecommitdiff
path: root/patchwork/templatetags/patch.py
blob: 3837798d29d48c292d43de980c6fdabdc65551fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Patchwork - automated patch tracking system
# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
# Copyright (C) 2015 Intel Corporation
#
# SPDX-License-Identifier: GPL-2.0-or-later

from django import template
from django.utils.html import escape
from django.utils.safestring import mark_safe

from patchwork.models import Check


register = template.Library()


@register.filter(name='patch_tags')
def patch_tags(patch):
    counts = []
    titles = []
    for tag in [t for t in patch.project.tags if t.show_column]:
        count = getattr(patch, tag.attr_name)
        titles.append('%d %s' % (count, tag.name))
        if count == 0:
            counts.append("-")
        else:
            counts.append(str(count))
    return mark_safe('<span title="%s">%s</span>' % (
        ' / '.join(titles),
        ' '.join(counts)))


@register.filter(name='patch_checks')
def patch_checks(patch):
    required = [Check.STATE_SUCCESS, Check.STATE_WARNING, Check.STATE_FAIL]
    titles = ['Success', 'Warning', 'Fail']
    counts = patch.check_count

    check_elements = []
    use_color = True
    for state in required[::-1]:
        if counts[state]:
            if use_color:
                use_color = False
                color = dict(Check.STATE_CHOICES).get(state)
            else:
                color = ''
            count = str(counts[state])
        else:
            color = ''
            count = '-'

        check_elements.append(
            '<span class="patchlistchecks {}">{}</span>'.format(
                color, count))

    check_elements.reverse()

    return mark_safe('<span title="%s">%s</span>' % (
        ' / '.join(titles),
        ''.join(check_elements)))


@register.filter(name='patch_commit_display')
def patch_commit_display(patch):
    commit = patch.commit_ref
    fmt = patch.project.commit_url_format

    if not fmt:
        return escape(commit)

    return mark_safe('<a href="%s">%s</a>' % (escape(fmt.format(commit)),
                                              escape(commit)))