Skip to content

Commit

Permalink
Merge pull request #260 from ppfeufer/improve-templatetag
Browse files Browse the repository at this point in the history
[CHANGE] Template tag improved
  • Loading branch information
ppfeufer authored Mar 2, 2025
2 parents b74cc91 + bfc5a34 commit b63e71f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Section Order:
### Security
-->

### Changed

- Code for the template tag has been improved

## [3.5.1] - 2025-02-21

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion fleetpings/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import os

APP_NAME = "aa-fleetpings"
PACKAGE_NAME = "fleetpings"
GITHUB_URL = f"https://github.com/ppfeufer/{APP_NAME}"

DISCORD_WEBHOOK_REGEX = r"https:\/\/discord\.com\/api\/webhooks\/[\d]+\/[a-zA-Z0-9_-]+$"

AA_FLEETPINGS_BASE_DIR = os.path.join(os.path.dirname(__file__))
AA_FLEETPINGS_STATIC_DIR = os.path.join(AA_FLEETPINGS_BASE_DIR, "static", "fleetpings")
AA_FLEETPINGS_STATIC_DIR = os.path.join(AA_FLEETPINGS_BASE_DIR, "static", PACKAGE_NAME)
3 changes: 2 additions & 1 deletion fleetpings/helper/static_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
def calculate_integrity_hash(relative_file_path: str) -> str:
"""
Calculates the integrity hash for a given static file
:param self:
:type self:
:param relative_file_path: The file path relative to the `aa-fleetpings/fleetpings/static/fleetpings` folder
:param relative_file_path: The file path relative to the `{APP_NAME}/{PACKAGE_NAME}/static/{PACKAGE_NAME}` folder
:type relative_file_path: str
:return: The integrity hash
:rtype: str
Expand Down
15 changes: 10 additions & 5 deletions fleetpings/templatetags/fleetpings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

# AA Fleet Pings
from fleetpings import __title__, __version__
from fleetpings.constants import PACKAGE_NAME
from fleetpings.helper.static_files import calculate_integrity_hash

logger = LoggerAddTag(my_logger=get_extension_logger(__name__), prefix=__title__)
Expand All @@ -46,7 +47,7 @@ def fleetpings_static(relative_file_path: str, script_type: str = None) -> str |
"""
Versioned static URL
:param relative_file_path: The file path relative to the `aa-fleetpings/fleetpings/static/fleetpings` folder
:param relative_file_path: The file path relative to the `{APP_NAME}/{PACKAGE_NAME}/static/{PACKAGE_NAME}` folder
:type relative_file_path: str
:param script_type: The script type
:type script_type: str
Expand All @@ -64,7 +65,7 @@ def fleetpings_static(relative_file_path: str, script_type: str = None) -> str |
if file_type not in ["css", "js"]:
raise ValueError(f"Unsupported file type: {file_type}")

static_file_path = os.path.join("fleetpings", relative_file_path)
static_file_path = os.path.join(PACKAGE_NAME, relative_file_path)
static_url = static(static_file_path)

# Integrity hash calculation only for non-debug mode
Expand All @@ -83,16 +84,20 @@ def fleetpings_static(relative_file_path: str, script_type: str = None) -> str |
else static_url + "?v=" + __version__
)

return_value = None

# Return the versioned URL with integrity hash for CSS
if file_type == "css":
return mark_safe(f'<link rel="stylesheet" href="{versioned_url}"{sri_string}>')
return_value = mark_safe(
f'<link rel="stylesheet" href="{versioned_url}"{sri_string}>'
)

# Return the versioned URL with integrity hash for JS files
if file_type == "js":
js_type = f' type="{script_type}"' if script_type else ""

return mark_safe(
return_value = mark_safe(
f'<script{js_type} src="{versioned_url}"{sri_string}></script>'
)

return None
return return_value
7 changes: 4 additions & 3 deletions fleetpings/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# AA Fleet Pings
from fleetpings import __version__
from fleetpings.constants import PACKAGE_NAME
from fleetpings.helper.static_files import calculate_integrity_hash


Expand Down Expand Up @@ -40,13 +41,13 @@ def test_versioned_static(self):
rendered_template = template_to_render.render(context=context)

expected_static_css_src = (
f'/static/fleetpings/css/fleetpings.min.css?v={context["version"]}'
f'/static/{PACKAGE_NAME}/css/fleetpings.min.css?v={context["version"]}'
)
expected_static_css_src_integrity = calculate_integrity_hash(
"css/fleetpings.min.css"
)
expected_static_js_src = (
f'/static/fleetpings/js/fleetpings.min.js?v={context["version"]}'
f'/static/{PACKAGE_NAME}/js/fleetpings.min.js?v={context["version"]}'
)
expected_static_js_src_integrity = calculate_integrity_hash(
"js/fleetpings.min.js"
Expand Down Expand Up @@ -81,7 +82,7 @@ def test_versioned_static_with_debug_enabled(self) -> None:
rendered_template = template_to_render.render(context=context)

expected_static_css_src = (
f'/static/fleetpings/css/fleetpings.min.css?v={context["version"]}'
f'/static/{PACKAGE_NAME}/css/fleetpings.min.css?v={context["version"]}'
)

self.assertIn(member=expected_static_css_src, container=rendered_template)
Expand Down

0 comments on commit b63e71f

Please sign in to comment.