fix: highlight active portal navigation
This commit is contained in:
@@ -82,21 +82,23 @@
|
||||
<div class="container">
|
||||
<a class="navbar-brand fw-semibold" href="{% url 'portal-home' %}">cmhub</a>
|
||||
<div class="ms-auto d-flex gap-2 flex-wrap justify-content-end">
|
||||
{% with current_url=request.resolver_match.url_name %}
|
||||
{% if user.is_authenticated %}
|
||||
<a class="btn btn-sm btn-outline-secondary" href="{% url 'portal-dashboard' %}">控制台</a>
|
||||
<a class="btn btn-sm btn-primary" href="{% url 'portal-recharge' %}">充值</a>
|
||||
<a class="btn btn-sm btn-outline-secondary" href="{% url 'portal-apikeys' %}">API Key</a>
|
||||
<a class="btn btn-sm btn-outline-secondary" href="{% url 'portal-models' %}">可用模型</a>
|
||||
<a class="btn btn-sm btn-outline-secondary" href="{% url 'portal-recharge-records' %}">充值记录</a>
|
||||
<a class="btn btn-sm btn-outline-secondary" href="{% url 'portal-usage-records' %}">消费记录</a>
|
||||
<a class="btn btn-sm {% if current_url == 'portal-dashboard' %}btn-primary{% else %}btn-outline-secondary{% endif %}"{% if current_url == 'portal-dashboard' %} aria-current="page"{% endif %} href="{% url 'portal-dashboard' %}">控制台</a>
|
||||
<a class="btn btn-sm {% if current_url == 'portal-recharge' %}btn-primary{% else %}btn-outline-secondary{% endif %}"{% if current_url == 'portal-recharge' %} aria-current="page"{% endif %} href="{% url 'portal-recharge' %}">充值</a>
|
||||
<a class="btn btn-sm {% if current_url == 'portal-apikeys' %}btn-primary{% else %}btn-outline-secondary{% endif %}"{% if current_url == 'portal-apikeys' %} aria-current="page"{% endif %} href="{% url 'portal-apikeys' %}">API Key</a>
|
||||
<a class="btn btn-sm {% if current_url == 'portal-models' %}btn-primary{% else %}btn-outline-secondary{% endif %}"{% if current_url == 'portal-models' %} aria-current="page"{% endif %} href="{% url 'portal-models' %}">可用模型</a>
|
||||
<a class="btn btn-sm {% if current_url == 'portal-recharge-records' %}btn-primary{% else %}btn-outline-secondary{% endif %}"{% if current_url == 'portal-recharge-records' %} aria-current="page"{% endif %} href="{% url 'portal-recharge-records' %}">充值记录</a>
|
||||
<a class="btn btn-sm {% if current_url == 'portal-usage-records' %}btn-primary{% else %}btn-outline-secondary{% endif %}"{% if current_url == 'portal-usage-records' %} aria-current="page"{% endif %} href="{% url 'portal-usage-records' %}">消费记录</a>
|
||||
<form method="post" action="{% url 'portal-logout' %}">
|
||||
{% csrf_token %}
|
||||
<button class="btn btn-sm btn-outline-secondary" type="submit">退出</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<a class="btn btn-sm btn-outline-secondary" href="{% url 'portal-login' %}">登录</a>
|
||||
<a class="btn btn-sm btn-primary" href="{% url 'portal-signup' %}">注册</a>
|
||||
<a class="btn btn-sm {% if current_url == 'portal-login' %}btn-primary{% else %}btn-outline-secondary{% endif %}"{% if current_url == 'portal-login' %} aria-current="page"{% endif %} href="{% url 'portal-login' %}">登录</a>
|
||||
<a class="btn btn-sm {% if current_url == 'portal-signup' %}btn-primary{% else %}btn-outline-secondary{% endif %}"{% if current_url == 'portal-signup' %} aria-current="page"{% endif %} href="{% url 'portal-signup' %}">注册</a>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
@@ -110,6 +110,20 @@ class PortalAccountFlowTests(TestCase):
|
||||
ai_model=ai_model,
|
||||
)
|
||||
|
||||
def assert_nav_link_active(self, response, *, href: str, label: str):
|
||||
html = response.content.decode("utf-8")
|
||||
self.assertIn(
|
||||
f'<a class="btn btn-sm btn-primary" aria-current="page" href="{href}">{label}</a>',
|
||||
html,
|
||||
)
|
||||
|
||||
def assert_nav_link_inactive(self, response, *, href: str, label: str):
|
||||
html = response.content.decode("utf-8")
|
||||
self.assertIn(
|
||||
f'<a class="btn btn-sm btn-outline-secondary" href="{href}">{label}</a>',
|
||||
html,
|
||||
)
|
||||
|
||||
def test_signup_creates_unverified_user_wallet_with_zero_points_and_no_ledger(self):
|
||||
suffix = uuid.uuid4().hex[:8]
|
||||
email = f"signup-{suffix}@example.com"
|
||||
@@ -250,6 +264,31 @@ class PortalAccountFlowTests(TestCase):
|
||||
self.assertNotContains(response, "api_key_encrypted")
|
||||
self.assertNotContains(response, "extra_body")
|
||||
|
||||
def test_authenticated_nav_highlights_current_page_only(self):
|
||||
user = self.create_verified_user()
|
||||
self.client.force_login(user)
|
||||
cases = (
|
||||
("/dashboard", "/dashboard", "控制台"),
|
||||
("/recharge", "/recharge", "充值"),
|
||||
("/apikeys", "/apikeys", "API Key"),
|
||||
("/models", "/models", "可用模型"),
|
||||
("/records/recharge", "/records/recharge", "充值记录"),
|
||||
("/records/usage", "/records/usage", "消费记录"),
|
||||
)
|
||||
|
||||
for path, href, label in cases:
|
||||
with self.subTest(path=path):
|
||||
response = self.client.get(path)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assert_nav_link_active(response, href=href, label=label)
|
||||
if path != "/recharge":
|
||||
self.assert_nav_link_inactive(
|
||||
response,
|
||||
href="/recharge",
|
||||
label="充值",
|
||||
)
|
||||
|
||||
def test_create_api_key_shows_plaintext_once_and_stores_only_hash(self):
|
||||
user = self.create_verified_user()
|
||||
self.client.force_login(user)
|
||||
|
||||
Reference in New Issue
Block a user