Skip to content

Commit

Permalink
Allow getting just the integrity value
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed May 2, 2020
1 parent b460259 commit 19de7c5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ The SRI standard supports 3 algorithms: sha256, sha384 and sha512. By default, S

The default algorithm can be changed by setting `SRI_ALGORITHM` to the required algorithm.

### Just the integrity value

To retrieve just the integrity hash (the contents of the `integrity` attribute), you can use the `{% sri_integrity %}` tag, which supports the same arguments as the other tags.

### _"Does this work with [whitenoise](https://whitenoise.evans.io/en/stable/) or alike?"_

Yes. `django-sri` outputs the static file URL in the same way the builtin `static` template tag does. This means the correct cachebusted URLs are output.
5 changes: 5 additions & 0 deletions sri/templatetags/sri.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ def sri_css(path, algorithm=DEFAULT_ALGORITHM):
def sri(path, algorithm=DEFAULT_ALGORITHM):
extension = os.path.splitext(path)[1][1:]
return EXTENSIONS[extension](path, algorithm)


@register.simple_tag
def sri_integrity(path, algorithm=DEFAULT_ALGORITHM):
return calculate_integrity(get_static_path(path), algorithm)
5 changes: 5 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ def test_disable_sri(tag):
assert "integrity" not in tag("index.js")
finally:
templatetags.USE_SRI = original_value


@pytest.mark.parametrize("algorithm", utils.HASHERS.keys())
def test_sri_integrity(algorithm):
assert templatetags.sri_integrity("index.js", algorithm).startswith(f"{algorithm}-")

0 comments on commit 19de7c5

Please sign in to comment.