feat: add 18 independent industry demo templates

This commit is contained in:
2026-11-03 22:33:03 +08:00
parent 1d08bdb6a5
commit 2b60c0d5e0
7 changed files with 632 additions and 1 deletions
+50 -1
View File
@@ -6,8 +6,10 @@ apps.core 全局 Snippet 只读接口。挂载在 /api/v1/custom/core/
from rest_framework.response import Response
from rest_framework.throttling import ScopedRateThrottle
from rest_framework.views import APIView
from django.http import Http404
from .models import NavigationMenu, Partner, SiteSettings, TeamMember, Testimonial
from .industry_templates import INDUSTRIES, demo_path, demo_root
from .models import NavigationMenu, Partner, SimpleContentPage, SiteSettings, TeamMember, Testimonial
from .serializers import (
NavigationMenuSerializer,
PartnerSerializer,
@@ -65,3 +67,50 @@ class SiteSettingsView(APIView):
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