converters.py 3.76 KB
Newer Older
1
from datetime import datetime
2
import json
3
import shlex
4

5
from django import template
6
from django.utils.safestring import mark_safe
7
from django.utils.timesince import timesince
BERJON Matthieu's avatar
BERJON Matthieu committed
8
from misaka import Markdown, HtmlRenderer, EXT_SUPERSCRIPT, EXT_FENCED_CODE, EXT_AUTOLINK
9

10
11
from main.models import Job

12
13
register = template.Library()

BERJON Matthieu's avatar
BERJON Matthieu committed
14
@register.filter(name='markdown')
BERJON Matthieu's avatar
BERJON Matthieu committed
15
def markdown_filter(value):
16
    """ Convert markdown content into HTML. """
BERJON Matthieu's avatar
BERJON Matthieu committed
17
18
19
    renderer = HtmlRenderer()
    markdown = Markdown(
            renderer,
BERJON Matthieu's avatar
BERJON Matthieu committed
20
21
            extensions=EXT_SUPERSCRIPT | EXT_FENCED_CODE )
    return markdown.render(value)
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40


@register.filter(name='email2name')
def email2name(email):
    """ Convert INRIA/IRISA emails into firstname name pattern
    """
    value = email.split('@')
    if '.' in value[0]:
        data = value[0].split('.')
        contact = data[0] + ' ' + data[1]
        return email2name(contact.title())
    else:
        return value[0].capitalize()


@register.filter(name='fancy_webapp_name')
def fancy_name(webapp_name):
    """ Replace underscores by spaces """
    return webapp_name.replace('_', ' ')
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@register.filter(name='fancy_job_queue')
def fancy_job_queue(queue):
    """Display the queue name with its timeout (if any)"""
    if queue.timeout is None:
        return queue.name
    if queue.timeout < 60:
        return "%s (<%d seconds)" % (queue.name, queue.timeout)
    else:
        return "%s (<%s)" % (queue.name, timesince(
            datetime.fromtimestamp(0),
            datetime.fromtimestamp(queue.timeout)))





58
59
60
61
62

_JOB_STATUS_RENDER_VALUES = {
        "NEW":      ("New job",         "fas fa-plus  picto-green"),
        "WAITING":  ("Waiting job",     "fas fa-clock picto-yellow"),
        "RUNNING":  ("Job running",     "fas fa-play  picto-blue"),
BAIRE Anthony's avatar
BAIRE Anthony committed
63
        "ABORTING": ("Job aborting",    "fas fa-play  picto-yellow"),
64
65
        "SUCCESS":  ("Job success",     "fas fa-check picto-green"),
        "ERROR":    ("Job error",       "fas fa-times picto-red"),
BAIRE Anthony's avatar
BAIRE Anthony committed
66
        "ABORTED":  ("Job aborted",     "fas fa-hand-paper picto-orange"),
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        "TIMEOUT":  ("Job timeout",     "fas fa-stopwatch picto-orange"),
        }

@register.filter(name='status_icon')
def status_icon(obj):
    """ Renders a status icon for a model """

    def render_job_status(status):
        txt, cls = _JOB_STATUS_RENDER_VALUES.get(status,
                ("Unknown status", "fas fa-question"))
        return ('<i class="{cls}" data-toggle="tooltip" data-placement="top"'
                ' title="{txt}"></i><span class="text-hide">{txt}</span>'
                .format(txt=txt, cls=cls))

    if isinstance(obj, Job):
        return mark_safe(render_job_status(obj.status))
83
84
85
    elif obj == "job-status-dict":
        return json.dumps({status: render_job_status(status)
            for status in _JOB_STATUS_RENDER_VALUES})
86
87

    raise TypeError(type(obj))
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122


@register.filter(name='command_multiline')
def command_multiline(cmd):
    """Format a shell command over multiple lines (for display)

    `cmd` is the command to be formatted, provided as a list of strings (the
    actual arguments) or the `None` value (for inserting a newline).
    """

    lst = []
    sep = ""
    for arg in cmd:
        if arg is None:
            sep = "\n  "
        else:
            lst.append(sep)
            lst.append(shlex.quote(arg))
            sep = " "
    return "".join(lst)

@register.filter(name='command_oneline')
def command_oneline(cmd):
    """Format a shell command as a single line (for the clipboard)

    `cmd` is the command to be formatted, provided as a list of strings (the
    actual arguments) or the `None` value (for inserting a newline).

    This converter ignores the `None` elements, thus the command is formatted
    as a single line.
    """
    return " ".join(shlex.quote(arg) for arg in cmd if arg is not None)