• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zostera / django-bootstrap4 / 8830227319

25 Apr 2024 09:28AM CUT coverage: 90.904%. First build
8830227319

Pull #724

github

web-flow
Bump dependabot/fetch-metadata from 2.0.0 to 2.1.0

Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 2.0.0 to 2.1.0.
- [Release notes](https://github.com/dependabot/fetch-metadata/releases)
- [Commits](https://github.com/dependabot/fetch-metadata/compare/v2.0.0...v2.1.0)

---
updated-dependencies:
- dependency-name: dependabot/fetch-metadata
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #724: Bump dependabot/fetch-metadata from 2.0.0 to 2.1.0

315 of 388 branches covered (81.19%)

Branch coverage included in aggregate %.

1274 of 1360 relevant lines covered (93.68%)

9.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.45
/src/bootstrap4/utils.py
1
import re
10✔
2
from collections.abc import Mapping
10✔
3
from urllib.parse import parse_qs, urlparse, urlunparse
10✔
4

5
from django.forms.utils import flatatt
10✔
6
from django.template.base import FilterExpression, TemplateSyntaxError, Variable, VariableDoesNotExist, kwarg_re
10✔
7
from django.template.loader import get_template
10✔
8
from django.utils.encoding import force_str
10✔
9
from django.utils.html import format_html
10✔
10
from django.utils.http import urlencode
10✔
11
from django.utils.safestring import mark_safe
10✔
12

13
from .text import text_value
10✔
14

15
# RegEx for quoted string
16
QUOTED_STRING = re.compile(r'^["\'](?P<noquotes>.+)["\']$')
10✔
17

18

19
def handle_var(value, context):
10✔
20
    """Handle template tag variable."""
21
    # Resolve FilterExpression and Variable immediately
22
    if isinstance(value, FilterExpression) or isinstance(value, Variable):
10!
23
        return value.resolve(context)
10✔
24
    # Return quoted strings unquoted
25
    # http://djangosnippets.org/snippets/886
26
    stringval = QUOTED_STRING.search(value)
×
27
    if stringval:
×
28
        return stringval.group("noquotes")
×
29
    # Resolve variable or return string value
30
    try:
×
31
        return Variable(value).resolve(context)
×
32
    except VariableDoesNotExist:
×
33
        return value
×
34

35

36
def parse_token_contents(parser, token):
10✔
37
    """Parse template tag contents."""
38
    bits = token.split_contents()
10✔
39
    tag = bits.pop(0)
10✔
40
    args = []
10✔
41
    kwargs = {}
10✔
42
    asvar = None
10✔
43
    if len(bits) >= 2 and bits[-2] == "as":
10!
44
        asvar = bits[-1]
×
45
        bits = bits[:-2]
×
46
    if len(bits):
10!
47
        for bit in bits:
10✔
48
            match = kwarg_re.match(bit)
10✔
49
            if not match:
10!
50
                raise TemplateSyntaxError(f'Malformed arguments to tag "{tag}"')
×
51
            name, value = match.groups()
10✔
52
            if name:
10!
53
                kwargs[name] = parser.compile_filter(value)
10✔
54
            else:
55
                args.append(parser.compile_filter(value))
×
56
    return {"tag": tag, "args": args, "kwargs": kwargs, "asvar": asvar}
10✔
57

58

59
def split_css_classes(css_classes):
10✔
60
    """Turn string into a list of CSS classes."""
61
    classes_list = text_value(css_classes).split(" ")
10✔
62
    return [c for c in classes_list if c]
10✔
63

64

65
def add_css_class(css_classes, css_class, prepend=False):
10✔
66
    """Add a CSS class to a string of CSS classes."""
67
    classes_list = split_css_classes(css_classes)
10✔
68
    classes_to_add = [c for c in split_css_classes(css_class) if c not in classes_list]
10✔
69
    if prepend:
10✔
70
        classes_list = classes_to_add + classes_list
10✔
71
    else:
72
        classes_list += classes_to_add
10✔
73
    return " ".join(classes_list)
10✔
74

75

76
def remove_css_class(css_classes, css_class):
10✔
77
    """Remove a CSS class from a string of CSS classes."""
78
    remove = set(split_css_classes(css_class))
×
79
    classes_list = [c for c in split_css_classes(css_classes) if c not in remove]
×
80
    return " ".join(classes_list)
×
81

82

83
def render_script_tag(url):
10✔
84
    """Build a script tag."""
85
    url_dict = sanitize_url_dict(url)
10✔
86
    url_dict.setdefault("src", url_dict.pop("url", None))
10✔
87
    return render_tag("script", url_dict)
10✔
88

89

90
def render_link_tag(url, rel="stylesheet", media=None):
10✔
91
    """Build a link tag."""
92
    url_dict = sanitize_url_dict(url, url_attr="href")
10✔
93
    url_dict.setdefault("href", url_dict.pop("url", None))
10✔
94
    url_dict["rel"] = rel
10✔
95
    if media:
10!
96
        url_dict["media"] = media
×
97
    return render_tag("link", attrs=url_dict, close=False)
10✔
98

99

100
def render_tag(tag, attrs=None, content=None, close=True):
10✔
101
    """Render a HTML tag."""
102
    builder = "<{tag}{attrs}>{content}"
10✔
103
    if content or close:
10✔
104
        builder += "</{tag}>"
10✔
105
    return format_html(builder, tag=tag, attrs=mark_safe(flatatt(attrs)) if attrs else "", content=text_value(content))
10✔
106

107

108
def render_template_file(template, context=None):
10✔
109
    """Render a Template to unicode."""
110
    assert isinstance(context, Mapping)
10✔
111
    template = get_template(template)
10✔
112
    return template.render(context)
10✔
113

114

115
def url_replace_param(url, name, value):
10✔
116
    """Replace a GET parameter in an URL."""
117
    url_components = urlparse(force_str(url))
10✔
118

119
    params = parse_qs(url_components.query)
10✔
120

121
    if value is None:
10✔
122
        del params[name]
10✔
123
    else:
124
        params[name] = value
10✔
125

126
    return mark_safe(
10✔
127
        urlunparse(
128
            [
129
                url_components.scheme,
130
                url_components.netloc,
131
                url_components.path,
132
                url_components.params,
133
                urlencode(params, doseq=True),
134
                url_components.fragment,
135
            ]
136
        )
137
    )
138

139

140
def sanitize_url_dict(url, url_attr="src"):
10✔
141
    """Sanitize url dict as used in django-bootstrap4 settings."""
142
    if isinstance(url, str):
10✔
143
        return {url_attr: url}
10✔
144
    return url.copy()
10✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc