Files

73 lines
4.8 KiB
Python

"""Read-only, request-scoped portal guidance; never infers a live VPN connection."""
from django.conf import settings
from django.urls import reverse
from control.models import PlanApplication, Subscription
from .models import AccountBandwidthExemption, AccountBandwidthExecutor
from .policy import DEFAULT_KIB_PER_SECOND, MAX_KIB_PER_SECOND
def portal_context(request):
context = {"PLATFORM_NAME": "纵横家"}
user = getattr(request, "user", None)
if not user or not user.is_authenticated:
return context
can_manage = bool(user.is_active and user.is_staff and (
user.has_perm("shared_budget.view_accountbandwidthpolicy")
or user.has_perm("shared_budget.change_accountbandwidthpolicy")))
if can_manage:
admin_budget = {
"default_upload": DEFAULT_KIB_PER_SECOND, "default_download": DEFAULT_KIB_PER_SECOND,
"maximum_limit": MAX_KIB_PER_SECOND,
"allocator_enabled": getattr(settings, "ACCOUNT_BANDWIDTH_BUDGET_V2_ENABLED", False) is True,
"policy_url": reverse("admin:shared_budget_accountbandwidthpolicy_changelist"),
"execution_label": "待现场验收",
}
if user.has_perm("shared_budget.view_accountbandwidthexemption"):
admin_budget["exemption_count"] = AccountBandwidthExemption.objects.count()
admin_budget["exemption_url"] = reverse("admin:shared_budget_accountbandwidthexemption_changelist")
admin_budget["registered_executors"] = AccountBandwidthExecutor.objects.count()
context["admin_budget"] = admin_budget
match = getattr(request, "resolver_match", None)
if not match or match.url_name != "portal-dashboard":
return context
# Every query is scoped to the signed-in user; no account ID is accepted
# from query strings, form fields, or a selected client node.
subscriptions = user.subscriptions.select_related("plan", "routing_policy").all()
subscription = subscriptions.exclude(status__in=["cancelled", "expired"]).first() or subscriptions.first()
pending = user.plan_applications.filter(status=PlanApplication.Status.PENDING).exists()
journey = {"pending": pending, "ready": False, "step": 1,
"primary_url": reverse("apply-plan"), "primary_label": "选择套餐并申请"}
if not user.is_active or user.account_status in ("suspended", "departed"):
journey.update(state="restricted", step=2, title="服务暂不可用",
body="账号已暂停或停止服务,请联系管理员核实。当前页面不会提供订阅凭据。",
primary_url=reverse("portal-dashboard") + "#application-history", primary_label="查看申请记录")
elif subscription and subscription.is_usable:
journey.update(state="ready", ready=True, step=3, title="订阅已准备好",
body="下载 Windows 客户端后,用同一账号登录;也可展开个人订阅地址供兼容客户端使用。",
primary_url=reverse("client-download-latest"), primary_label="下载 Windows 客户端")
elif pending:
journey.update(state="review", step=2, title="申请已提交,等待审核",
body="不用重复提交。管理员核实身份并审核后,开通进度与订阅会显示在这里;暂未开放付款的套餐不会扣款。",
primary_url=reverse("portal-dashboard") + "#application-history", primary_label="查看审核进度")
elif subscription and subscription.online_payment_pending:
journey.update(state="payment_pending", step=2, title="套餐暂未开放付款",
body="申请只作登记,尚未扣款或开通付费服务。请等待管理员通知,不要向个人账号转账。",
primary_url=reverse("portal-dashboard") + "#application-history", primary_label="查看申请记录")
elif subscription and subscription.status == "active" and user.service_enabled:
journey.update(state="provisioning", step=2, title="审核已通过,等待订阅就绪",
body="节点配置仍在处理。请稍后刷新本页;实际连接状态只在客户端查看。",
primary_url=reverse("portal-dashboard"), primary_label="刷新开通进度")
elif subscription:
journey.update(state="renew", step=2, title="订阅当前不可用",
body="请核对下方有效期、流量和处理状态。需要续期或更换套餐时再提交申请。",
primary_url=reverse("apply-plan"), primary_label="查看可申请套餐")
else:
journey.update(state="apply", title="先选择套餐,提交开通申请",
body="注册只需一次。提交套餐申请后由管理员审核,通过后在这里获取订阅与客户端下载入口。")
context["portal_journey"] = journey
return context