Skip to content

Commit 6a24241

Browse files
committed
[CHANGE] Improve templatetag code
1 parent 16030d7 commit 6a24241

File tree

8 files changed

+86
-20
lines changed

8 files changed

+86
-20
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Section Order:
3737

3838
### Changed
3939

40+
- Improve templatetag code
4041
- Use DataTables native translations instead of our own
4142

4243
## [2.6.0] - 2025-01-31

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ prepare-release: graph_models pot
6464
# fi; \
6565
# done; \
6666
# fi;
67-
echo "Updated version in $(TEXT_BOLD)$(package)/__init__.py$(TEXT_BOLD_END)"
67+
@echo "Updated version in $(TEXT_BOLD)$(package)/__init__.py$(TEXT_BOLD_END)"
6868

6969
# Help
7070
.PHONY: help

aa_intel_tool/app_settings.py

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
# Standard Library
66
from enum import IntEnum
7+
from re import RegexFlag
8+
9+
# Django
10+
from django.conf import settings
711

812
# Alliance Auth
913
from allianceauth.services.hooks import get_extension_logger
@@ -81,3 +85,14 @@ class UpwellStructureId(IntEnum):
8185
"""
8286

8387
ANSIBLEX_JUMP_GATE = 35841
88+
89+
90+
def debug_enabled() -> RegexFlag:
91+
"""
92+
Check if DEBUG is enabled
93+
94+
:return:
95+
:rtype:
96+
"""
97+
98+
return settings.DEBUG

aa_intel_tool/constants.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@
8080

8181
# Building our user agent for ESI calls
8282
APP_NAME = "aa-intel-tool"
83+
PACKAGE_NAME = "aa_intel_tool"
84+
APP_VERBOSE_NAME = "AA Intel Tool"
85+
APP_VERBOSE_NAME_USERAGENT = "AA-Intel-Tool"
8386
GITHUB_URL = f"https://github.com/ppfeufer/{APP_NAME}"
84-
USER_AGENT = f"{APP_NAME}/{__version__} ({GITHUB_URL}) via django-esi/{esi_version}"
87+
USER_AGENT = f"{APP_VERBOSE_NAME_USERAGENT}/{__version__} (+{GITHUB_URL}) Django-ESI/{esi_version}"
8588

86-
AA_INTEL_TOOL_BASE_DIR = os.path.join(os.path.dirname(__file__))
87-
AA_INTEL_TOOL_STATIC_DIR = os.path.join(
88-
AA_INTEL_TOOL_BASE_DIR, "static", "aa_intel_tool"
89-
)
89+
APP_BASE_DIR = os.path.join(os.path.dirname(__file__))
90+
APP_STATIC_DIR = os.path.join(APP_BASE_DIR, "static", PACKAGE_NAME)

aa_intel_tool/helper/static_files.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@
1717

1818
# AA Intel Tool
1919
from aa_intel_tool import __title__
20-
from aa_intel_tool.constants import AA_INTEL_TOOL_STATIC_DIR
20+
from aa_intel_tool.constants import APP_STATIC_DIR
2121

2222
logger = LoggerAddTag(my_logger=get_extension_logger(__name__), prefix=__title__)
2323

2424

2525
def calculate_integrity_hash(relative_file_path: str) -> str:
2626
"""
2727
Calculates the integrity hash for a given static file
28+
2829
:param self:
2930
:type self:
30-
:param relative_file_path: The file path relative to the `aa-intel-tool/aa_intel_tool/static/aa_intel_tool` folder
31+
:param relative_file_path: The file path relative to the `{APP_NAME}/{PACKAGE_NAME}/static/{PACKAGE_NAME}` folder
3132
:type relative_file_path: str
3233
:return: The integrity hash
3334
:rtype: str
3435
"""
3536

36-
file_path = os.path.join(AA_INTEL_TOOL_STATIC_DIR, relative_file_path)
37+
file_path = os.path.join(APP_STATIC_DIR, relative_file_path)
3738
integrity_hash = calculate_integrity(
3839
path=Path(file_path), algorithm=Algorithm.SHA512
3940
)

aa_intel_tool/templatetags/aa_intel_tool.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import os
77

88
# Django
9-
from django.conf import settings
109
from django.template.defaulttags import register
1110
from django.templatetags.static import static
1211
from django.utils.safestring import mark_safe
@@ -19,6 +18,8 @@
1918

2019
# AA Intel Tool
2120
from aa_intel_tool import __title__, __version__
21+
from aa_intel_tool.app_settings import debug_enabled
22+
from aa_intel_tool.constants import PACKAGE_NAME
2223
from aa_intel_tool.helper.static_files import calculate_integrity_hash
2324

2425
logger = LoggerAddTag(my_logger=get_extension_logger(__name__), prefix=__title__)
@@ -31,7 +32,7 @@ def aa_intel_tool_static(
3132
"""
3233
Versioned static URL
3334
34-
:param relative_file_path: The file path relative to the `aa-intel-tool/aa_intel_tool/static/aa_intel_tool folder
35+
:param relative_file_path: The file path relative to the `{APP_NAME}/{PACKAGE_NAME}/static/{PACKAGE_NAME}` folder
3536
:type relative_file_path: str
3637
:param script_type: The script type
3738
:type script_type: str
@@ -49,13 +50,13 @@ def aa_intel_tool_static(
4950
if file_type not in ["css", "js"]:
5051
raise ValueError(f"Unsupported file type: {file_type}")
5152

52-
static_file_path = os.path.join("aa_intel_tool", relative_file_path)
53+
static_file_path = os.path.join(PACKAGE_NAME, relative_file_path)
5354
static_url = static(static_file_path)
5455

5556
# Integrity hash calculation only for non-debug mode
5657
sri_string = (
5758
f' integrity="{calculate_integrity_hash(relative_file_path)}" crossorigin="anonymous"'
58-
if not settings.DEBUG
59+
if not debug_enabled()
5960
else ""
6061
)
6162

@@ -68,16 +69,20 @@ def aa_intel_tool_static(
6869
else static_url + "?v=" + __version__
6970
)
7071

72+
return_value = None
73+
7174
# Return the versioned URL with integrity hash for CSS
7275
if file_type == "css":
73-
return mark_safe(f'<link rel="stylesheet" href="{versioned_url}"{sri_string}>')
76+
return_value = mark_safe(
77+
f'<link rel="stylesheet" href="{versioned_url}"{sri_string}>'
78+
)
7479

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

79-
return mark_safe(
84+
return_value = mark_safe(
8085
f'<script{js_type} src="{versioned_url}"{sri_string}></script>'
8186
)
8287

83-
return None # pragma: no cover
88+
return return_value

aa_intel_tool/tests/test_app_settings.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from django.test import TestCase, override_settings
1111

1212
# AA Intel Tool
13-
from aa_intel_tool.app_settings import AppSettings
13+
from aa_intel_tool.app_settings import AppSettings, debug_enabled
1414

1515
SETTINGS_PATH = "aa_intel_tool.app_settings"
1616

@@ -105,3 +105,25 @@ def test_dscan_grid_size_custom(self):
105105
expected_grid_size = 1000
106106

107107
self.assertEqual(first=grid_size, second=expected_grid_size)
108+
109+
@override_settings(DEBUG=True)
110+
def test_debug_enabled_with_debug_true(self) -> None:
111+
"""
112+
Test debug_enabled with DEBUG = True
113+
114+
:return:
115+
:rtype:
116+
"""
117+
118+
self.assertTrue(debug_enabled())
119+
120+
@override_settings(DEBUG=False)
121+
def test_debug_enabled_with_debug_false(self) -> None:
122+
"""
123+
Test debug_enabled with DEBUG = False
124+
125+
:return:
126+
:rtype:
127+
"""
128+
129+
self.assertFalse(debug_enabled())

aa_intel_tool/tests/test_templatetags.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# AA Intel Tool
1010
from aa_intel_tool import __version__
11+
from aa_intel_tool.constants import PACKAGE_NAME
1112
from aa_intel_tool.helper.static_files import calculate_integrity_hash
1213

1314

@@ -36,12 +37,12 @@ def test_versioned_static(self):
3637
rendered_template = template_to_render.render(context=context)
3738

3839
expected_static_css_src = (
39-
f'/static/aa_intel_tool/css/aa-intel-tool.min.css?v={context["version"]}'
40+
f'/static/{PACKAGE_NAME}/css/aa-intel-tool.min.css?v={context["version"]}'
4041
)
4142
expected_static_css_src_integrity = calculate_integrity_hash(
4243
"css/aa-intel-tool.min.css"
4344
)
44-
expected_static_js_src = f'/static/aa_intel_tool/javascript/aa-intel-tool.min.js?v={context["version"]}'
45+
expected_static_js_src = f'/static/{PACKAGE_NAME}/javascript/aa-intel-tool.min.js?v={context["version"]}'
4546
expected_static_js_src_integrity = calculate_integrity_hash(
4647
"javascript/aa-intel-tool.min.js"
4748
)
@@ -75,8 +76,28 @@ def test_versioned_static_with_debug_enabled(self) -> None:
7576
rendered_template = template_to_render.render(context=context)
7677

7778
expected_static_css_src = (
78-
f'/static/aa_intel_tool/css/aa-intel-tool.min.css?v={context["version"]}'
79+
f'/static/{PACKAGE_NAME}/css/aa-intel-tool.min.css?v={context["version"]}'
7980
)
8081

8182
self.assertIn(member=expected_static_css_src, container=rendered_template)
8283
self.assertNotIn(member="integrity=", container=rendered_template)
84+
85+
@override_settings(DEBUG=False)
86+
def test_invalid_file_type(self) -> None:
87+
"""
88+
Test should raise a ValueError for an invalid file type
89+
90+
:return:
91+
:rtype:
92+
"""
93+
94+
context = Context({"version": __version__})
95+
template_to_render = Template(
96+
template_string=(
97+
"{% load aa_intel_tool %}"
98+
"{% aa_intel_tool_static 'invalid/invalid.txt' %}"
99+
)
100+
)
101+
102+
with self.assertRaises(ValueError):
103+
template_to_render.render(context=context)

0 commit comments

Comments
 (0)