Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/swh_templatetags.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import json | import json | ||||
import re | import re | ||||
from django import template | from django import template | ||||
from django.core.serializers.json import DjangoJSONEncoder | from django.core.serializers.json import DjangoJSONEncoder | ||||
from django.utils.safestring import mark_safe | from django.utils.safestring import mark_safe | ||||
from docutils.core import publish_parts | from docutils.core import publish_parts | ||||
from docutils.writers.html4css1 import Writer, HTMLTranslator | from docutils.writers.html4css1 import Writer, HTMLTranslator | ||||
from inspect import cleandoc | from inspect import cleandoc | ||||
from swh.web.common.origin_save import get_savable_origin_types | from swh.web.common.origin_save import get_savable_visit_types | ||||
register = template.Library() | register = template.Library() | ||||
class NoHeaderHTMLTranslator(HTMLTranslator): | class NoHeaderHTMLTranslator(HTMLTranslator): | ||||
""" | """ | ||||
Docutils translator subclass to customize the generation of HTML | Docutils translator subclass to customize the generation of HTML | ||||
from reST-formatted docstrings | from reST-formatted docstrings | ||||
▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | """Django template filter to get a value in a dictionary. | ||||
Returns: | Returns: | ||||
The requested value in the dictionary | The requested value in the dictionary | ||||
""" | """ | ||||
return dict[key] | return dict[key] | ||||
@register.filter | @register.filter | ||||
def origin_type_savable(origin_type): | def visit_type_savable(visit_type): | ||||
"""Django template filter to check if a save request can be | """Django template filter to check if a save request can be | ||||
created for a given origin type. | created for a given visit type. | ||||
Args: | Args: | ||||
origin_type (str): the type of software origin | visit_type (str): the type of visit | ||||
Returns: | Returns: | ||||
If the origin type is saveable or not | If the visit type is saveable or not | ||||
""" | """ | ||||
return origin_type in get_savable_origin_types() | return visit_type in get_savable_visit_types() | ||||
@register.filter | @register.filter | ||||
def split(value, arg): | def split(value, arg): | ||||
"""Django template filter to split a string. | """Django template filter to split a string. | ||||
Args: | Args: | ||||
value (str): the string to split | value (str): the string to split | ||||
arg (str): the split separator | arg (str): the split separator | ||||
Returns: | Returns: | ||||
list: the split string parts | list: the split string parts | ||||
""" | """ | ||||
return value.split(arg) | return value.split(arg) |