Coverage for CIResults/templatetags/helpers.py: 100%
54 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-19 09:20 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-19 09:20 +0000
1from django import template
2from django.template.loader import render_to_string
3from django.utils.safestring import mark_safe
4from django.utils.timesince import timesince
5from simplediff import diff
6import json
9# Define some filters for django's
10register = template.Library()
13@register.filter
14def howlongago(value):
15 if value is None:
16 return "Never"
17 else:
18 return "{} ago".format(timesince(value))
21# The point of this filter is to convert a list of db objects into a coma-separated
22# list of values which have been transformed using a template.
23#
24# Use this filter like this: {{mylist|csl:"obj:path/to/template"}} with obj being
25# the name expected by the template
26@register.filter
27def csl(value, args=None):
28 obj_name, template = args.split(':')
29 return mark_safe(", ".join([render_to_string(template, {obj_name: obj}) for obj in value]))
32@register.filter
33def to_json(obj):
34 return json.dumps(obj)
37@register.filter
38def filters_model_to_completions(filters_model):
39 completions = []
40 for obj_name, filter in filters_model.filter_objects_to_db.items():
41 completions.append({
42 'text': obj_name,
43 'help': filter.description,
44 })
45 return completions
48@register.filter
49def list_custom_fields(bugs):
50 fields = set()
51 for bug in bugs:
52 fields |= set(bug.custom_fields.keys())
54 return sorted(fields)
57@register.filter
58def issues_list_custom_fields(issues):
59 bugs = set()
60 for issue in issues:
61 bugs |= set(issue.bugs_cached)
62 return list_custom_fields(bugs)
65@register.filter
66def lookup(value, arg):
67 return value.get(arg)
70@register.filter
71def markdown_escape(value):
72 s = value
73 for r in ['\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '#', '+', '-', '.', '!']:
74 s = s.replace(r, '\\' + r)
75 return s
78@register.filter
79def markdown_diff(before, after):
80 out = ""
81 for status, value in diff(markdown_escape(before), markdown_escape(after)):
82 if status == '=':
83 out += value
84 else:
85 out += "{" + status + value + status + "}"
86 return out