diff --git a/CHANGELOG.md b/CHANGELOG.md index 25b1d39..fca4acb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,10 @@ Section Order: ### Security --> +### Changed + +- Code for the template tag has been improved + ## [3.5.1] - 2025-02-21 ### Fixed diff --git a/fleetpings/constants.py b/fleetpings/constants.py index 461ecb2..80b3d7f 100644 --- a/fleetpings/constants.py +++ b/fleetpings/constants.py @@ -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) diff --git a/fleetpings/helper/static_files.py b/fleetpings/helper/static_files.py index 3031854..692578a 100644 --- a/fleetpings/helper/static_files.py +++ b/fleetpings/helper/static_files.py @@ -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 diff --git a/fleetpings/templatetags/fleetpings.py b/fleetpings/templatetags/fleetpings.py index a07e619..b8583fc 100644 --- a/fleetpings/templatetags/fleetpings.py +++ b/fleetpings/templatetags/fleetpings.py @@ -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__) @@ -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 @@ -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 @@ -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'') + return_value = mark_safe( + f'' + ) # 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'' ) - return None + return return_value diff --git a/fleetpings/tests/test_templatetags.py b/fleetpings/tests/test_templatetags.py index 50f251d..19cf12e 100644 --- a/fleetpings/tests/test_templatetags.py +++ b/fleetpings/tests/test_templatetags.py @@ -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 @@ -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" @@ -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)