Files
wagtailcms/apps/core/api.py
T

117 lines
4.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
apps.core 全局 Snippet 只读接口。挂载在 /api/v1/custom/core/
(详见 apps/core/urls.py 与 documents/设计方案分析与完善版.md §2.7)。
公开只读数据,限流走 DRF ScopedRateThrottle 的 "public" scope(100/min)。
"""
from rest_framework.response import Response
from rest_framework.throttling import ScopedRateThrottle
from rest_framework.views import APIView
from django.http import Http404
from .industry_templates import INDUSTRIES, demo_path, demo_root
from .models import NavigationMenu, Partner, SimpleContentPage, SiteSettings, TeamMember, Testimonial
from .serializers import (
NavigationMenuSerializer,
PartnerSerializer,
SiteSettingsSerializer,
TeamMemberSerializer,
TestimonialSerializer,
)
class TeamMemberListView(APIView):
throttle_classes = [ScopedRateThrottle]
throttle_scope = "public"
def get(self, request, *args, **kwargs):
members = TeamMember.objects.all()
return Response({"items": TeamMemberSerializer(members, many=True).data})
class TestimonialListView(APIView):
throttle_classes = [ScopedRateThrottle]
throttle_scope = "public"
def get(self, request, *args, **kwargs):
testimonials = Testimonial.objects.all()
return Response({"items": TestimonialSerializer(testimonials, many=True).data})
class PartnerListView(APIView):
throttle_classes = [ScopedRateThrottle]
throttle_scope = "public"
def get(self, request, *args, **kwargs):
partners = Partner.objects.all()
return Response({"items": PartnerSerializer(partners, many=True).data})
class NavigationMenuView(APIView):
"""根据 ?name= 查询指定导航菜单(如 main / footer)。"""
throttle_classes = [ScopedRateThrottle]
throttle_scope = "public"
def get(self, request, *args, **kwargs):
name = request.query_params.get("name", "main")
menu = NavigationMenu.objects.prefetch_related("items").filter(name=name).first()
if menu is None:
return Response({"error": {"code": "NOT_FOUND", "message": "导航菜单不存在"}}, status=404)
return Response(NavigationMenuSerializer(menu).data)
class SiteSettingsView(APIView):
throttle_classes = [ScopedRateThrottle]
throttle_scope = "public"
def get(self, request, *args, **kwargs):
settings_obj = SiteSettings.for_request(request)
return Response(SiteSettingsSerializer(settings_obj).data)
class IndustryDemoView(APIView):
throttle_classes = [ScopedRateThrottle]
throttle_scope = "public"
def get(self, request, industry=None, *args, **kwargs):
if industry is None:
return Response({"items": [
{"slug": slug, "label": spec["label"], "company": spec["company"],
"description": spec["description"], "url": demo_path(slug)}
for slug, spec in INDUSTRIES.items()
if (root := demo_root(slug)) is not None and root.live
]})
if industry not in INDUSTRIES:
raise Http404
root = demo_root(industry)
if root is None or not root.live or not type(root).objects.public().filter(pk=root.pk).exists():
raise Http404
path = request.query_params.get("path", "").strip("/")
page = root
if path:
for segment in path.split("/"):
if not segment or segment in (".", ".."):
raise Http404
child = page.get_children().live().public().filter(slug=segment).first()
if child is None:
raise Http404
page = child.specific
if not page.get_view_restrictions().exists() and not root.get_view_restrictions().exists():
body = getattr(page, "body", None)
blocks = body.stream_block.get_api_representation(body, context={"request": request}) if hasattr(body, "stream_block") else []
children = [
{"title": child.title, "url": demo_path(industry, f"{path}/{child.slug}")}
for child in page.get_children().live().public()
]
navigation = [{"title": "首页", "url": demo_path(industry)}] + [
{"title": child.title, "url": demo_path(industry, child.slug)}
for child in root.get_children().live().public()
if child.slug != "privacy-policy"
]
return Response({"industry": industry, "label": INDUSTRIES[industry]["label"],
"company": root.title, "title": page.title, "blocks": blocks,
"html": str(body) if isinstance(page, SimpleContentPage) else "",
"intro": getattr(page, "intro", "") or getattr(page, "summary", ""),
"children": children, "navigation": navigation})
raise Http404