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

zostera / django-bootstrap4 / 10873049980

15 Sep 2024 05:41PM CUT coverage: 90.935%. First build
10873049980

Pull #745

github

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

Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 2.1.0 to 2.2.0.
- [Release notes](https://github.com/dependabot/fetch-metadata/releases)
- [Commits](https://github.com/dependabot/fetch-metadata/compare/v2.1.0...v2.2.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 #745: Bump dependabot/fetch-metadata from 2.1.0 to 2.2.0

311 of 384 branches covered (80.99%)

Branch coverage included in aggregate %.

1274 of 1359 relevant lines covered (93.75%)

13.12 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
14✔
2
from collections.abc import Mapping
14✔
3
from urllib.parse import parse_qs, urlparse, urlunparse
14✔
4

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

13
from .text import text_value
14✔
14

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

18

19
def handle_var(value, context):
14✔
20
    """Handle template tag variable."""
21
    # Resolve FilterExpression and Variable immediately
22
    if isinstance(value, FilterExpression) or isinstance(value, Variable):
14!
23
        return value.resolve(context)
14✔
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):
14✔
37
    """Parse template tag contents."""
38
    bits = token.split_contents()
14✔
39
    tag = bits.pop(0)
14✔
40
    args = []
14✔
41
    kwargs = {}
14✔
42
    asvar = None
14✔
43
    if len(bits) >= 2 and bits[-2] == "as":
14!
44
        asvar = bits[-1]
×
45
        bits = bits[:-2]
×
46
    if len(bits):
14!
47
        for bit in bits:
14✔
48
            match = kwarg_re.match(bit)
14✔
49
            if not match:
14!
50
                raise TemplateSyntaxError(f'Malformed arguments to tag "{tag}"')
×
51
            name, value = match.groups()
14✔
52
            if name:
14!
53
                kwargs[name] = parser.compile_filter(value)
14✔
54
            else:
55
                args.append(parser.compile_filter(value))
×
56
    return {"tag": tag, "args": args, "kwargs": kwargs, "asvar": asvar}
14✔
57

58

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

64

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

75

76
def remove_css_class(css_classes, css_class):
14✔
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):
14✔
84
    """Build a script tag."""
85
    url_dict = sanitize_url_dict(url)
14✔
86
    url_dict.setdefault("src", url_dict.pop("url", None))
14✔
87
    return render_tag("script", url_dict)
14✔
88

89

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

99

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

107

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

114

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

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

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

126
    return mark_safe(
14✔
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"):
14✔
141
    """Sanitize url dict as used in django-bootstrap4 settings."""
142
    if isinstance(url, str):
14✔
143
        return {url_attr: url}
14✔
144
    return url.copy()
14✔
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