feat: finish phase 4 portal review fixes
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2011-2024 The Bootstrap Authors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
File diff suppressed because one or more lines are too long
+10
@@ -0,0 +1,10 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2012 Ryan Day
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
+2835
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,11 @@
|
||||
<!doctype html>
|
||||
{% load static %}
|
||||
<html lang="zh-Hans">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{% block title %}cmhub{% endblock %}</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="{% static 'portal/vendor/bootstrap/bootstrap.min.css' %}" rel="stylesheet">
|
||||
<style>
|
||||
:root {
|
||||
--cmhub-bg: #f6f7f9;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
{% if page_obj and records_total_count %}
|
||||
<div class="d-flex justify-content-between align-items-center gap-3 flex-wrap mt-3">
|
||||
<div class="text-secondary small">
|
||||
共 {{ records_total_count }} 条,每页 {{ records_page_size }} 条,当前第 {{ page_obj.number }} / {{ paginator.num_pages }} 页
|
||||
</div>
|
||||
{% if is_paginated %}
|
||||
<nav aria-label="记录分页">
|
||||
<ul class="pagination pagination-sm mb-0">
|
||||
<li class="page-item{% if not page_obj.has_previous %} disabled{% endif %}">
|
||||
{% if page_obj.has_previous %}
|
||||
<a class="page-link" href="?page={{ page_obj.previous_page_number }}">上一页</a>
|
||||
{% else %}
|
||||
<span class="page-link">上一页</span>
|
||||
{% endif %}
|
||||
</li>
|
||||
<li class="page-item{% if not page_obj.has_next %} disabled{% endif %}">
|
||||
{% if page_obj.has_next %}
|
||||
<a class="page-link" href="?page={{ page_obj.next_page_number }}">下一页</a>
|
||||
{% else %}
|
||||
<span class="page-link">下一页</span>
|
||||
{% endif %}
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
@@ -1,4 +1,5 @@
|
||||
{% extends "portal/base.html" %}
|
||||
{% load static %}
|
||||
|
||||
{% block title %}充值 - cmhub{% endblock %}
|
||||
|
||||
@@ -159,7 +160,7 @@
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/qrcode@1.5.4/build/qrcode.min.js"></script>
|
||||
<script src="{% static 'portal/vendor/qrcode/qrcode.js' %}"></script>
|
||||
<script>
|
||||
(function () {
|
||||
const panel = document.querySelector("[data-recharge-order]");
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% include "portal/includes/pagination.html" %}
|
||||
{% else %}
|
||||
<div class="text-secondary">暂无充值记录</div>
|
||||
{% endif %}
|
||||
|
||||
@@ -87,6 +87,7 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% include "portal/includes/pagination.html" %}
|
||||
{% else %}
|
||||
<div class="text-secondary">暂无消费记录</div>
|
||||
{% endif %}
|
||||
|
||||
+98
-1
@@ -3,6 +3,7 @@ from decimal import Decimal
|
||||
|
||||
from allauth.account.models import EmailAddress
|
||||
from django.contrib.auth import get_user, get_user_model
|
||||
from django.contrib.staticfiles import finders
|
||||
from django.core import mail
|
||||
from django.core.cache import cache
|
||||
from django.test import Client, TestCase, override_settings
|
||||
@@ -377,6 +378,43 @@ class PortalAccountFlowTests(TestCase):
|
||||
self.assertContains(response, "R-OWN-PENDING-503")
|
||||
self.assertNotContains(response, "R-OTHER-RECHARGE-503")
|
||||
|
||||
def test_recharge_records_paginate_more_than_page_size(self):
|
||||
user = self.create_verified_user()
|
||||
other_user = self.create_verified_user()
|
||||
self.create_recharge_order(
|
||||
other_user,
|
||||
order_no="R-PAGE-OTHER-505",
|
||||
amount="99.00",
|
||||
points=990,
|
||||
)
|
||||
for index in range(55):
|
||||
self.create_recharge_order(
|
||||
user,
|
||||
order_no=f"R-PAGE-{index:03d}",
|
||||
amount="1.00",
|
||||
points=10,
|
||||
)
|
||||
self.client.force_login(user)
|
||||
|
||||
first_page = self.client.get("/records/recharge")
|
||||
second_page = self.client.get("/records/recharge?page=2")
|
||||
|
||||
self.assertEqual(first_page.status_code, 200)
|
||||
self.assertEqual(first_page.context["records_total_count"], 55)
|
||||
self.assertEqual(first_page.context["page_obj"].number, 1)
|
||||
self.assertEqual(len(first_page.context["recharge_orders"]), 50)
|
||||
self.assertContains(first_page, "共 55 条,每页 50 条")
|
||||
self.assertContains(first_page, "R-PAGE-054")
|
||||
self.assertNotContains(first_page, "R-PAGE-000")
|
||||
self.assertNotContains(first_page, "R-PAGE-OTHER-505")
|
||||
|
||||
self.assertEqual(second_page.status_code, 200)
|
||||
self.assertEqual(second_page.context["page_obj"].number, 2)
|
||||
self.assertEqual(len(second_page.context["recharge_orders"]), 5)
|
||||
self.assertContains(second_page, "R-PAGE-000")
|
||||
self.assertNotContains(second_page, "R-PAGE-054")
|
||||
self.assertNotContains(second_page, "R-PAGE-OTHER-505")
|
||||
|
||||
def test_usage_records_require_login_and_only_show_current_user_usage(self):
|
||||
user = self.create_verified_user()
|
||||
other_user = self.create_verified_user()
|
||||
@@ -435,6 +473,59 @@ class PortalAccountFlowTests(TestCase):
|
||||
self.assertNotContains(response, "other-alias")
|
||||
self.assertNotContains(response, other_key.key_prefix)
|
||||
|
||||
def test_usage_records_paginate_more_than_page_size(self):
|
||||
user = self.create_verified_user()
|
||||
other_user = self.create_verified_user()
|
||||
api_key, _raw_key = ApiKey.create_for_user(user, name="desktop")
|
||||
other_key, _other_raw_key = ApiKey.create_for_user(other_user, name="other")
|
||||
other_call = self.create_call_record(
|
||||
other_user,
|
||||
other_key,
|
||||
alias="other-page-alias",
|
||||
points_cost=99,
|
||||
)
|
||||
PointsLedger.objects.create(
|
||||
user=other_user,
|
||||
change_type=PointsLedger.ChangeType.CONSUME,
|
||||
points_delta=-99,
|
||||
balance_after=0,
|
||||
ref_call=other_call,
|
||||
)
|
||||
for index in range(55):
|
||||
call = self.create_call_record(
|
||||
user,
|
||||
api_key,
|
||||
alias=f"title-page-{index:03d}",
|
||||
points_cost=1,
|
||||
)
|
||||
PointsLedger.objects.create(
|
||||
user=user,
|
||||
change_type=PointsLedger.ChangeType.CONSUME,
|
||||
points_delta=-1,
|
||||
balance_after=100 - index,
|
||||
ref_call=call,
|
||||
)
|
||||
self.client.force_login(user)
|
||||
|
||||
first_page = self.client.get("/records/usage")
|
||||
second_page = self.client.get("/records/usage?page=2")
|
||||
|
||||
self.assertEqual(first_page.status_code, 200)
|
||||
self.assertEqual(first_page.context["records_total_count"], 55)
|
||||
self.assertEqual(first_page.context["page_obj"].number, 1)
|
||||
self.assertEqual(len(first_page.context["usage_entries"]), 50)
|
||||
self.assertContains(first_page, "共 55 条,每页 50 条")
|
||||
self.assertContains(first_page, "title-page-054")
|
||||
self.assertNotContains(first_page, "title-page-000")
|
||||
self.assertNotContains(first_page, "other-page-alias")
|
||||
|
||||
self.assertEqual(second_page.status_code, 200)
|
||||
self.assertEqual(second_page.context["page_obj"].number, 2)
|
||||
self.assertEqual(len(second_page.context["usage_entries"]), 5)
|
||||
self.assertContains(second_page, "title-page-000")
|
||||
self.assertNotContains(second_page, "title-page-054")
|
||||
self.assertNotContains(second_page, "other-page-alias")
|
||||
|
||||
def test_recharge_page_requires_login_and_shows_form(self):
|
||||
user = self.create_verified_user()
|
||||
UserWallet.objects.create(user=user, points_balance=25)
|
||||
@@ -450,8 +541,14 @@ class PortalAccountFlowTests(TestCase):
|
||||
self.assertEqual(response.context["balance"].points_balance, 25)
|
||||
self.assertContains(response, "充值金额")
|
||||
self.assertContains(response, "创建订单")
|
||||
self.assertContains(response, "qrcode@1.5.4")
|
||||
self.assertContains(response, "portal/vendor/bootstrap/bootstrap.min.css")
|
||||
self.assertContains(response, "portal/vendor/qrcode/qrcode.js")
|
||||
self.assertContains(response, "QRCode.toCanvas")
|
||||
self.assertNotContains(response, "cdn.jsdelivr.net")
|
||||
self.assertIsNotNone(
|
||||
finders.find("portal/vendor/bootstrap/bootstrap.min.css")
|
||||
)
|
||||
self.assertIsNotNone(finders.find("portal/vendor/qrcode/qrcode.js"))
|
||||
|
||||
def test_recharge_page_post_creates_pending_order_without_crediting_wallet_or_ledger(self):
|
||||
user = self.create_verified_user()
|
||||
|
||||
+19
-2
@@ -1,5 +1,6 @@
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.core.paginator import Paginator
|
||||
from django.db.models import Sum
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.urls import reverse, reverse_lazy
|
||||
@@ -74,6 +75,18 @@ def get_usage_ledger_entries_for_user(user):
|
||||
)
|
||||
|
||||
|
||||
def paginate_records(request, queryset):
|
||||
paginator = Paginator(queryset, RECORDS_PAGE_SIZE)
|
||||
page_obj = paginator.get_page(request.GET.get("page"))
|
||||
return {
|
||||
"paginator": paginator,
|
||||
"page_obj": page_obj,
|
||||
"is_paginated": page_obj.has_other_pages(),
|
||||
"records_total_count": paginator.count,
|
||||
"records_page_size": RECORDS_PAGE_SIZE,
|
||||
}
|
||||
|
||||
|
||||
class DashboardView(LoginRequiredMixin, TemplateView):
|
||||
template_name = "portal/dashboard.html"
|
||||
|
||||
@@ -175,8 +188,10 @@ class RechargeRecordListView(LoginRequiredMixin, TemplateView):
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
orders = get_recharge_orders_for_user(self.request.user)
|
||||
pagination = paginate_records(self.request, orders)
|
||||
context.update(get_portal_account_summary(self.request.user))
|
||||
context["recharge_orders"] = orders[:RECORDS_PAGE_SIZE]
|
||||
context.update(pagination)
|
||||
context["recharge_orders"] = pagination["page_obj"].object_list
|
||||
return context
|
||||
|
||||
|
||||
@@ -186,6 +201,8 @@ class UsageRecordListView(LoginRequiredMixin, TemplateView):
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
entries = get_usage_ledger_entries_for_user(self.request.user)
|
||||
pagination = paginate_records(self.request, entries)
|
||||
context.update(get_portal_account_summary(self.request.user))
|
||||
context["usage_entries"] = entries[:RECORDS_PAGE_SIZE]
|
||||
context.update(pagination)
|
||||
context["usage_entries"] = pagination["page_obj"].object_list
|
||||
return context
|
||||
|
||||
Reference in New Issue
Block a user