feat(seo): ایندکسپذیری کامل، محتوای سمت سرور برای خزندههای AI و رفع باگ ایندکس
سئوی سمت سرور (backend/app/seo_pages.py) - متا/canonical/hreflang/JSON-LD برای همهٔ صفحات عمومی (شش صفحهٔ استاتیک فارسی که قبلاً پوستهٔ خالی بودند) - canonical سمت سرور (قبلاً فقط با JS ست میشد)، ۴۰۴ واقعی برای مسیرهای ناموجود - اسکیما: Organization با لوگوی ImageObject، FAQPage، LocalBusiness، BreadcrumbList، CreativeWork پروژه با همهٔ تصاویر بهصورت ImageObject (نام/توضیح + موقعیت پروژه) - hreflang فقط وقتی ترجمهٔ انگلیسی واقعاً وجود دارد؛ sitemap با URLهای درصدگذاریشده - llms.txt و robots.txt تازهساز از دیتابیس؛ سیاست صریح خزندههای AI + Allow: /api/site - _site_content_html: محتوای واقعی سایت بهصورت HTML خوانا در <noscript> — خزندههای بدون جاوااسکریپت (GPTBot/OAI-SearchBot/ClaudeBot/PerplexityBot) از ۲۴۰ نویسه به ۸٬۵۰۰+ نویسه محتوای معنایی میرسند - Cache-Control: no-store روی HTML و llms.txt (جلوگیری از کش کهنهٔ CDN)، max-age روی sitemap فرانتاند - api.tsx: خواندن دادهٔ تزریقشدهٔ سرور (window.__SITE__) بهعنوان حالت اولیه — رندر بدون XHR - App.tsx: اسکلت بارگذاری بهجای DOM خالی (رفع Soft 404 گوگل) - index.html: فاوآیکون واقعی برند (قبلاً به vite.svg ناموجود اشاره میکرد) + preload فونت زیرساخت - Dockerfile: آینهٔ PyPI (Tsinghua/Aliyun) — PyPI برای IP ایران بلاک است - Caddyfile: هر مسیر پسونددار بدون فایل واقعی ۴۰۴ میگیرد (قبلاً HTML اسپا برمیگرداند) - docker-compose.prod.yml: مسیرهای seo برای صفحات استاتیک + llms.txt + کلیدهای IndexNow داده - seed_data*.py: حذف دفاتر و معرفیهای دمو (که در هر استقرار برمیگشتند)؛ تنها دفتر واقعی: اصفهان، ملاصدرا شمالی داراییها - فاوآیکون/آیکونهای برند از لوگوی رسمی + کلیدهای IndexNow
This commit is contained in:
+328
-36
@@ -24,12 +24,15 @@ import re
|
||||
from datetime import date, datetime
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
from fastapi import APIRouter, Depends, Response
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from fastapi.responses import HTMLResponse
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from . import models
|
||||
from .api.public import build_site_payload
|
||||
from .config import settings
|
||||
from .database import get_db
|
||||
|
||||
@@ -43,6 +46,9 @@ FALLBACK_EN_BRAND = "Aramesh Construction Group"
|
||||
# تصویر پیشفرض شبکههای اجتماعی — در frontend/public بیلد میشود
|
||||
DEFAULT_OG_IMAGE = "/og-default.jpg"
|
||||
DEFAULT_OG_SIZE = (1200, 630)
|
||||
# کلید IndexNow — اثبات مالکیت برای اعلام فوری نشانیهای سایت به Bing/Copilot و
|
||||
# موتورهای مشارکتکننده در پروتکل (فایل در /.well-known/indexnow-key.txt سرو میشود)
|
||||
INDEXNOW_KEY = "daa4488a8e2905312b5efd02ad5fc614"
|
||||
# خزندههای هوش مصنوعی جستوجو/ارجاع — سیاست صریح «مجاز» تا قالبهای پیشفرض
|
||||
# «بستن همهچیز» اتفاقی آنها را نبندند (GPTBot آموزش است، OAI-SearchBot ارجاع چتجیپیتی)
|
||||
AI_SEARCH_BOTS = [
|
||||
@@ -79,6 +85,11 @@ def _fa_path(path: str) -> str:
|
||||
return path[3:] if path.startswith("/en/") else path
|
||||
|
||||
|
||||
def _url_escape(url: str) -> str:
|
||||
"""درصدگذاری نویسههای غیر-ASCII (اسلاگهای فارسی) — استاندارد sitemap و IndexNow."""
|
||||
return quote(url, safe=":/?#[]@!$&'()*+,;=")
|
||||
|
||||
|
||||
def _clip(text: str, limit: int) -> str:
|
||||
"""برش متن به طول مشخص برای توضیحهای تکخطی (llms.txt)."""
|
||||
text = " ".join((text or "").split())
|
||||
@@ -211,6 +222,16 @@ def _doc_text(doc: object, limit: int = 300) -> str:
|
||||
return text[:limit] + ("…" if len(text) > limit else "")
|
||||
|
||||
|
||||
def _site_json(db: Session) -> str:
|
||||
"""محتوای کامل سایت بهصورت JSON برای تزریق داخل HTML (window.__SITE__).
|
||||
فرانتاند با همین داده بدون انتظار برای XHR رندر میشود — خزندههای رندرکننده
|
||||
(گوگل) هیچوقت در پنجرهٔ «در حال بارگذاری» صفحهٔ نیمهخالی نمیبینند."""
|
||||
payload = jsonable_encoder(build_site_payload(db))
|
||||
data = json.dumps(payload, ensure_ascii=False, separators=(",", ":"))
|
||||
# امنسازی درج در HTML: توالی بستن اسکریپت و کامنت HTML نباید در داده ظاهر شود
|
||||
return data.replace("</", "<\\/").replace("<!--", "<\\u0021--")
|
||||
|
||||
|
||||
def _shell(
|
||||
*,
|
||||
title: str,
|
||||
@@ -225,6 +246,7 @@ def _shell(
|
||||
org_jsonld: dict | None = None,
|
||||
noscript: str = "",
|
||||
status_code: int = 200,
|
||||
db: Session | None = None,
|
||||
) -> HTMLResponse:
|
||||
"""index.html فرانتاند با متای تزریقی؛ اگر بیلد در دسترس نبود، صفحه حداقلی."""
|
||||
title = title.strip() or (FALLBACK_EN_BRAND if lang == "en" else FALLBACK_FA_BRAND)
|
||||
@@ -265,6 +287,9 @@ def _shell(
|
||||
if org_jsonld:
|
||||
payload = json.dumps(org_jsonld, ensure_ascii=False).replace("</", "<\\/")
|
||||
tags.append(f'<script type="application/ld+json" id="seo-org-jsonld">{payload}</script>')
|
||||
if db is not None:
|
||||
# دادهٔ کامل سایت داخل HTML — اولین رندر بدون انتظار برای درخواست /api/site
|
||||
tags.append(f"<script>window.__SITE__={_site_json(db)};</script>")
|
||||
block = "\n ".join(tags)
|
||||
|
||||
raw = _read_index()
|
||||
@@ -276,6 +301,7 @@ def _shell(
|
||||
f'<!doctype html><html {lang_attr}><head><meta charset="utf-8">'
|
||||
f"<title>{esc(title)}</title>{alternates}{block}</head><body>{body}</body></html>",
|
||||
status_code=status_code,
|
||||
headers={"Cache-Control": "no-store"},
|
||||
)
|
||||
|
||||
# اسکیمای Organization مینیمالِ پوستهٔ استاتیک حذف میشود تا با نسخهٔ غنی تزریقی تکراری نشود
|
||||
@@ -302,7 +328,7 @@ def _shell(
|
||||
raw = raw.replace("</head>", f" {alternates}\n {block}\n </head>", 1)
|
||||
if noscript:
|
||||
raw = raw.replace("</body>", f"<noscript>{noscript}</noscript>\n </body>", 1)
|
||||
return HTMLResponse(raw, status_code=status_code)
|
||||
return HTMLResponse(raw, status_code=status_code, headers={"Cache-Control": "no-store"})
|
||||
|
||||
|
||||
# ---------- موجودیتهای ساختیافته (JSON-LD) ----------
|
||||
@@ -355,7 +381,19 @@ def _org_jsonld(db: Session, lang: str) -> dict:
|
||||
org["url"] = _absolute("/")
|
||||
logo = (brand.logo or "").strip() if brand else ""
|
||||
if logo:
|
||||
org["logo"] = _absolute(logo)
|
||||
# لوگوی رسمی بهصورت ImageObject — تا موتورها و دستیارهای AI آن را
|
||||
# بهعنوان نشان برند بشناسند، نه یک تصویر عمومی بیهویت
|
||||
org["logo"] = {
|
||||
"@type": "ImageObject",
|
||||
"url": _absolute(logo),
|
||||
"contentUrl": _absolute(logo),
|
||||
"name": f"{_brand_name(db, lang)} — {'لوگوی رسمی' if lang == 'fa' else 'official logo'}",
|
||||
"caption": _brand_name(db, lang),
|
||||
}
|
||||
# تصویر شاخص برند بهعنوان تصویر معرفی سازمان
|
||||
hero_img = (brand.hero or "").strip() if brand else ""
|
||||
if hero_img:
|
||||
org["image"] = {"@type": "ImageObject", "url": _absolute(hero_img), "caption": _brand_name(db, lang)}
|
||||
footer = db.query(models.FooterContent).first()
|
||||
socials = []
|
||||
if footer is not None:
|
||||
@@ -511,7 +549,9 @@ def robots(db: Session = Depends(get_db)):
|
||||
content="User-agent: *\nDisallow: /\n",
|
||||
media_type="text/plain; charset=utf-8",
|
||||
)
|
||||
lines = ["User-agent: *", "Disallow: /admin", "Disallow: /api", ""]
|
||||
# Allow: /api/site — رندرر گوگل موقع رندر صفحه این XHR را هم واکشی میکند؛
|
||||
# Disallow کلی /api باعث میشد «مسدود توسط robots.txt» در منابع صفحه ثبت شود
|
||||
lines = ["User-agent: *", "Disallow: /admin", "Disallow: /api", "Allow: /api/site", ""]
|
||||
# سیاست صریح خزندههای هوش مصنوعی — رباتهای جستوجو/ارجاع آزادند تا محتوا
|
||||
# در پاسخهای ChatGPT/Claude/Perplexity/Gemini ارجاع شود
|
||||
for bot in AI_SEARCH_BOTS:
|
||||
@@ -540,7 +580,11 @@ def sitemap(db: Session = Depends(get_db)):
|
||||
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
||||
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>\n'
|
||||
)
|
||||
return Response(content=xml, media_type="application/xml; charset=utf-8")
|
||||
return Response(
|
||||
content=xml,
|
||||
media_type="application/xml; charset=utf-8",
|
||||
headers={"Cache-Control": "public, max-age=300"},
|
||||
)
|
||||
esc = html.escape
|
||||
|
||||
articles = {
|
||||
@@ -557,10 +601,10 @@ def sitemap(db: Session = Depends(get_db)):
|
||||
def entry(loc: str, lang: str, has_en: bool) -> str:
|
||||
pairs = _alternate_pairs(loc, lang, has_en)
|
||||
links = "\n".join(
|
||||
f' <xhtml:link rel="alternate" hreflang="{code}" href="{esc(href_)}" />'
|
||||
f' <xhtml:link rel="alternate" hreflang="{code}" href="{esc(_url_escape(href_))}" />'
|
||||
for code, href_ in pairs
|
||||
)
|
||||
return f" <url><loc>{esc(_absolute(loc))}</loc>\n{links}\n </url>"
|
||||
return f" <url><loc>{esc(_url_escape(_absolute(loc)))}</loc>\n{links}\n </url>"
|
||||
|
||||
urls: list[str] = []
|
||||
# صفحات استاتیک فارسی + معادلهای انگلیسیِ ترجمهشده
|
||||
@@ -589,7 +633,23 @@ def sitemap(db: Session = Depends(get_db)):
|
||||
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
||||
f"{chr(10).join(urls)}\n</urlset>\n"
|
||||
)
|
||||
return Response(content=xml, media_type="application/xml; charset=utf-8")
|
||||
return Response(
|
||||
content=xml,
|
||||
media_type="application/xml; charset=utf-8",
|
||||
headers={"Cache-Control": "public, max-age=300"},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/.well-known/indexnow-key.txt", response_class=Response)
|
||||
def indexnow_key_file():
|
||||
"""فایل تأیید مالکیت IndexNow — موتورها با خواندن آن، ارسالهای IndexNow را میپذیرند."""
|
||||
return Response(content=INDEXNOW_KEY, media_type="text/plain; charset=utf-8")
|
||||
|
||||
|
||||
@router.get(f"/{INDEXNOW_KEY}.txt", response_class=Response)
|
||||
def indexnow_key_root():
|
||||
"""محل پیشفرض پروتکل IndexNow: /<key>.txt در ریشهٔ دامنه — برخی اعتبارسنجها فقط همین را میخوانند."""
|
||||
return Response(content=INDEXNOW_KEY, media_type="text/plain; charset=utf-8")
|
||||
|
||||
|
||||
@router.get("/llms.txt", response_class=Response)
|
||||
@@ -627,12 +687,10 @@ def llms_txt(db: Session = Depends(get_db)):
|
||||
("projects", "Projects"), ("blog", "Blog"), ("faq", "FAQ"), ("contact", "Contact"),
|
||||
]:
|
||||
title, description = _meta_for(db, page_key, "en")
|
||||
if page_key == "home":
|
||||
url = "/"
|
||||
else:
|
||||
url = f"/en/{page_key}" if page_key != "projects" else "/en/projects"
|
||||
summary = _clip(description, 100)
|
||||
lines.append(f"- [{label}]({_absolute(url)}): {summary or label}")
|
||||
url = "/" if page_key == "home" else f"/en/{page_key}"
|
||||
# توضیح متا کوتاه است (۱۵۰–۱۶۰ نویسه) — کامل میآید تا جمله نیمهبریده نشود
|
||||
summary = " ".join((description or "").split())
|
||||
lines.append(f"- [{label}]({_absolute(url)})" + (f": {summary}" if summary else ""))
|
||||
featured_projects = (
|
||||
db.query(models.Project).order_by(models.Project.position, models.Project.id).limit(6).all()
|
||||
)
|
||||
@@ -640,8 +698,15 @@ def llms_txt(db: Session = Depends(get_db)):
|
||||
lines += ["", "## Featured Projects"]
|
||||
for p in featured_projects:
|
||||
title = (p.title_en or p.title).strip()
|
||||
summary = _clip(p.summary_en or p.summary, 100)
|
||||
lines.append(f"- [{title}]({_absolute('/en/projects/' + p.slug)}): {summary}")
|
||||
# خلاصهٔ ثبتشده؛ در نبودش، موقعیت و وضعیت پروژه بهعنوان توضیح مفید
|
||||
summary = (p.summary_en or p.summary or "").strip()
|
||||
if not summary:
|
||||
place = (p.location_en or p.location or "").strip()
|
||||
summary = f"Project in {place}" if place else ""
|
||||
summary = _clip(summary, 140)
|
||||
# بدون ترجمهٔ انگلیسی، همان نشانی فارسی اعلام میشود؛ توضیح خالی هم حذف میشود
|
||||
path = f"/en/projects/{p.slug}" if _project_has_en(p) else f"/projects/{p.slug}"
|
||||
lines.append(f"- [{title}]({_absolute(path)})" + (f": {summary}" if summary else ""))
|
||||
latest = (
|
||||
db.query(models.Article)
|
||||
.filter(models.Article.status == "published")
|
||||
@@ -653,8 +718,9 @@ def llms_txt(db: Session = Depends(get_db)):
|
||||
lines += ["", "## Recent Articles"]
|
||||
for a in latest:
|
||||
title = (a.title_en or a.title).strip()
|
||||
summary = _clip(a.excerpt_en or a.excerpt, 100)
|
||||
lines.append(f"- [{title}]({_absolute('/en/blog/' + a.slug)}): {summary}")
|
||||
summary = _clip(a.excerpt_en or a.excerpt, 140)
|
||||
path = f"/en/blog/{a.slug}" if _article_has_en(a) else f"/blog/{a.slug}"
|
||||
lines.append(f"- [{title}]({_absolute(path)})" + (f": {summary}" if summary else ""))
|
||||
brand_row = _brand_row(db)
|
||||
contact_lines = []
|
||||
if brand_row and (brand_row.phone or "").strip():
|
||||
@@ -664,16 +730,149 @@ def llms_txt(db: Session = Depends(get_db)):
|
||||
for o in offices:
|
||||
city = (o.city_en or o.city).strip()
|
||||
street = ((o.address_en or o.address) or o.address).strip()
|
||||
if city or street:
|
||||
contact_lines.append(f"- Office: {_clip(street + ('، ' + city if city and street else city), 120)}")
|
||||
# اگر نشانی خودش نام شهر را دارد، تکرار نشود («اصفهان، ملاصدرا…» + «، اصفهان»)
|
||||
detail = street if (city and city in street) else (street + ("، " + city if city and street else city))
|
||||
if detail:
|
||||
contact_lines.append(f"- Office: {_clip(detail, 120)}")
|
||||
if contact_lines:
|
||||
lines += ["", "## Contact", *contact_lines]
|
||||
return Response(content="\n".join(lines) + "\n", media_type="text/plain; charset=utf-8")
|
||||
# llms.txt زنده از دیتابیس ساخته میشود — بدون کش تا همیشه تازه باشد
|
||||
return Response(
|
||||
content="\n".join(lines) + "\n",
|
||||
media_type="text/plain; charset=utf-8",
|
||||
headers={"Cache-Control": "no-store"},
|
||||
)
|
||||
|
||||
|
||||
# این مسیر باید بعد از همهٔ مسیرهای .txt مشخص (robots/llms/indexnow) تعریف شود
|
||||
# تا هیچکدام را سایه نیندازد
|
||||
@router.get("/{key}.txt", response_class=Response)
|
||||
def indexnow_key_any(key: str):
|
||||
"""هر کلید IndexNow معتبر (۳۲ نویسهٔ هگز) — فایل /{key}.txt همان کلید را برمیگرداند.
|
||||
پنل Bing/دیگر موتورها هر بار کلید تازه میسازند؛ این مسیر یعنی هیچ کلیدی
|
||||
نیاز به افزودن دستی فایل ندارد (فایلهای موجود در بیلد هم معتبر میمانند)."""
|
||||
if re.fullmatch(r"[a-f0-9]{32}", key):
|
||||
return Response(content=key, media_type="text/plain; charset=utf-8")
|
||||
return Response(status_code=404)
|
||||
|
||||
|
||||
# ---------- صفحات استاتیک فارسی ----------
|
||||
|
||||
|
||||
def _site_content_html(db: Session, page_key: str, lang: str) -> str:
|
||||
"""محتوای واقعی سایت بهصورت متن HTML خوانا — داخل <noscript>.
|
||||
|
||||
خزندههایی که جاوااسکریپت اجرا نمیکنند (اکثر رباتهای هوش مصنوعی: GPTBot،
|
||||
OAI-SearchBot، ClaudeBot، PerplexityBot) فقط HTML اولیه را میخوانند؛ بدون این،
|
||||
از سایت فقط عنوان و یک خط توضیح میدیدند. اینجا معرفی، خدمات، پروژهها، آمار،
|
||||
دفاتر و پرسشوپاسخها به متن واقعی تبدیل میشوند. برای مرورگرهای معمولی نامرئی
|
||||
است (noscript وقتی JS فعال است نمایش داده نمیشود)."""
|
||||
def en(fa_value: str, en_value: str | None) -> str:
|
||||
return (en_value or "").strip() if (lang == "en" and (en_value or "").strip()) else (fa_value or "").strip()
|
||||
|
||||
esc = html.escape
|
||||
parts: list[str] = []
|
||||
|
||||
brand = _brand_row(db)
|
||||
brand_name = _brand_name(db, lang)
|
||||
hero = db.query(models.Hero).first()
|
||||
if hero:
|
||||
parts.append(f"<h2>{esc(en(hero.title, hero.title_en))}</h2>")
|
||||
sub = en(hero.subtitle, hero.subtitle_en)
|
||||
if sub:
|
||||
parts.append(f"<p>{esc(sub)}</p>")
|
||||
|
||||
if page_key == "home":
|
||||
story = db.query(models.StorySection).first()
|
||||
if story:
|
||||
parts.append(f"<h2>{esc(en(story.title, story.title_en))}</h2>")
|
||||
# نسخهٔ انگلیسی متن داستان اگر ثبت شده باشد، جایگزین میشود
|
||||
paras = story.paragraphs if isinstance(story.paragraphs, list) else []
|
||||
if lang == "en" and isinstance(getattr(story, "paragraphs_en", None), list) and story.paragraphs_en:
|
||||
paras = story.paragraphs_en
|
||||
for para in paras:
|
||||
if isinstance(para, str) and para.strip():
|
||||
parts.append(f"<p>{esc(para.strip())}</p>")
|
||||
|
||||
stats = db.query(models.Stat).order_by(models.Stat.position, models.Stat.id).all()
|
||||
if stats:
|
||||
items = "".join(
|
||||
f"<li>{esc(str(s.value))}{esc(en(s.suffix, s.suffix_en))} — {esc(en(s.label, s.label_en))}</li>"
|
||||
for s in stats
|
||||
)
|
||||
parts.append(f"<h2>{'Key Numbers' if lang == 'en' else 'آمار کلیدی'}</h2><ul>{items}</ul>")
|
||||
|
||||
if page_key in ("home", "projects"):
|
||||
projects = db.query(models.Project).order_by(models.Project.position, models.Project.id).all()
|
||||
if projects:
|
||||
items = ""
|
||||
for p in projects:
|
||||
title = en(p.title, p.title_en)
|
||||
loc = en(p.location, p.location_en)
|
||||
year = en(p.year, p.year_en)
|
||||
meta_bits = "، ".join(x for x in (loc, year) if x)
|
||||
items += f"<li>{esc(title)}" + (f" — {esc(meta_bits)}" if meta_bits else "") + "</li>"
|
||||
parts.append(f"<h2>{'Our Projects' if lang == 'en' else 'پروژههای ما'}</h2><ul>{items}</ul>")
|
||||
|
||||
if page_key in ("home", "services"):
|
||||
services = db.query(models.Service).order_by(models.Service.position, models.Service.id).all()
|
||||
if services:
|
||||
items = "".join(
|
||||
f"<li><strong>{esc(en(s.title, s.title_en))}</strong>"
|
||||
+ (f": {esc(en(s.desc, s.desc_en))}" if en(s.desc, s.desc_en) else "")
|
||||
+ "</li>"
|
||||
for s in services
|
||||
)
|
||||
parts.append(f"<h2>{'Our Services' if lang == 'en' else 'خدمات ما'}</h2><ul>{items}</ul>")
|
||||
|
||||
if page_key == "investment":
|
||||
models_list = db.query(models.InvestmentModel).order_by(models.InvestmentModel.position).all()
|
||||
for m in models_list:
|
||||
parts.append(f"<h3>{esc(en(m.title, m.title_en))}</h3>")
|
||||
if en(m.desc, m.desc_en):
|
||||
parts.append(f"<p>{esc(en(m.desc, m.desc_en))}</p>")
|
||||
|
||||
if page_key in ("home", "faq"):
|
||||
faqs = db.query(models.Faq).order_by(models.Faq.position, models.Faq.id).all()
|
||||
if faqs:
|
||||
items = "".join(
|
||||
f"<li><strong>{esc(en(f.question, f.question_en))}</strong> {esc(en(f.answer, f.answer_en))}</li>"
|
||||
for f in faqs
|
||||
)
|
||||
parts.append(f"<h2>{'Frequently Asked Questions' if lang == 'en' else 'پرسشهای پرتکرار'}</h2><ul>{items}</ul>")
|
||||
|
||||
if page_key == "about":
|
||||
team = db.query(models.TeamMember).order_by(models.TeamMember.position).all()
|
||||
if team:
|
||||
items = "".join(
|
||||
f"<li><strong>{esc(en(m.name, m.name_en))}</strong> — {esc(en(m.role, m.role_en))}</li>" for m in team
|
||||
)
|
||||
parts.append(f"<h2>{'Our Team' if lang == 'en' else 'تیم ما'}</h2><ul>{items}</ul>")
|
||||
|
||||
if page_key in ("home", "contact"):
|
||||
offices = db.query(models.Office).order_by(models.Office.position, models.Office.id).all()
|
||||
contact_bits = []
|
||||
if brand and brand.phone.strip():
|
||||
contact_bits.append(f"<li>{'Phone' if lang == 'en' else 'تلفن'}: {esc(brand.phone.strip())}</li>")
|
||||
if brand and brand.email.strip():
|
||||
contact_bits.append(f"<li>{'Email' if lang == 'en' else 'ایمیل'}: {esc(brand.email.strip())}</li>")
|
||||
if brand and brand.address.strip():
|
||||
label = "Head office" if lang == "en" else "دفتر مرکزی"
|
||||
contact_bits.append(f"<li>{label}: {esc(brand.address.strip())}</li>")
|
||||
for o in offices:
|
||||
city = en(o.city, o.city_en)
|
||||
addr = en(o.address, o.address_en)
|
||||
hours = en(o.hours, o.hours_en)
|
||||
detail = addr if (city and city in addr) else f"{addr}، {city}" if addr and city else (addr or city)
|
||||
contact_bits.append(f"<li>{esc(detail)}" + (f" — {esc(hours)}" if hours else "") + "</li>")
|
||||
if contact_bits:
|
||||
parts.append(f"<h2>{'Contact' if lang == 'en' else 'تماس'}</h2><ul>{''.join(contact_bits)}</ul>")
|
||||
|
||||
if not parts:
|
||||
return ""
|
||||
return "<h1>" + esc(brand_name if page_key == "home" else brand_name) + "</h1>" + "".join(parts)
|
||||
|
||||
|
||||
def _static_shell(db: Session, *, path: str, page_key: str, lang: str = "fa") -> HTMLResponse:
|
||||
"""پوستهٔ صفحات استاتیک — متا از seo_meta، اسکیمای اختصاصی هر صفحه."""
|
||||
title, description = _meta_for(db, page_key, lang)
|
||||
@@ -691,7 +890,14 @@ def _static_shell(db: Session, *, path: str, page_key: str, lang: str = "fa") ->
|
||||
business = _localbusiness_jsonld(db, lang, path)
|
||||
if business:
|
||||
jsonld.append(business)
|
||||
# noscript با محتوای واقعی — خزندههای بدون جاوااسکریپت (رباتهای هوش مصنوعی)
|
||||
# هم عنوان، توضیح و کل محتوای معنایی صفحه را میبینند
|
||||
noscript = f"<h1>{html.escape(title)}</h1>"
|
||||
if description.strip():
|
||||
noscript += f"<p>{html.escape(description)}</p>"
|
||||
noscript += _site_content_html(db, page_key, lang)
|
||||
return _shell(
|
||||
db=db,
|
||||
title=title,
|
||||
description=description,
|
||||
path=path,
|
||||
@@ -699,6 +905,7 @@ def _static_shell(db: Session, *, path: str, page_key: str, lang: str = "fa") ->
|
||||
has_en=_static_has_en(db, page_key),
|
||||
jsonld=jsonld or None,
|
||||
org_jsonld=_org_jsonld(db, lang),
|
||||
noscript=noscript,
|
||||
)
|
||||
|
||||
|
||||
@@ -751,6 +958,7 @@ def blog_detail(slug: str, db: Session = Depends(get_db)):
|
||||
# مقاله ناموجود یا پیشنویس — کد وضعیت ۴۰۴ تا خزنده صفحه را ایندکس نکند؛
|
||||
# SPA همان پیام «یافت نشد» را نشان میدهد
|
||||
return _shell(
|
||||
db=db,
|
||||
title=_brand_name(db), description="", path=f"/blog/{slug}",
|
||||
org_jsonld=_org_jsonld(db, "fa"), status_code=404,
|
||||
)
|
||||
@@ -769,6 +977,7 @@ def blog_detail(slug: str, db: Session = Depends(get_db)):
|
||||
jsonld.append(faq_schema)
|
||||
noscript = f"<h1>{html.escape(title)}</h1><p>{html.escape(first_text)}</p>"
|
||||
return _shell(
|
||||
db=db,
|
||||
title=f"{title} | {brand}",
|
||||
description=_meta_desc_for(article, "fa"),
|
||||
path=f"/blog/{article.slug}",
|
||||
@@ -782,6 +991,94 @@ def blog_detail(slug: str, db: Session = Depends(get_db)):
|
||||
)
|
||||
|
||||
|
||||
def _project_schema(project: models.Project, lang: str, db: Session) -> dict:
|
||||
"""اسکیمای پروژه — همهٔ تصاویر با نام و توضیح به خود پروژه گره میخورند.
|
||||
بدون این، سیستمهای هوش مصنوعی فقط یک فایل تصویر بدون هویت میبینند و
|
||||
نمیتوانند تصویر را به پروژه نسبت دهند."""
|
||||
brand = _brand_name(db, lang)
|
||||
title = (project.title_en or project.title) if lang == "en" else project.title
|
||||
schema: dict = {
|
||||
"@context": "https://schema.org",
|
||||
"@type": "CreativeWork",
|
||||
"name": title,
|
||||
"inLanguage": lang,
|
||||
"creator": {"@type": "Organization", "name": brand},
|
||||
}
|
||||
location = (project.location_en or project.location) if lang == "en" else project.location
|
||||
summary = (project.summary_en or project.summary) if lang == "en" else project.summary
|
||||
body_text = _doc_text(project.body, 2000) if isinstance(project.body, dict) else ""
|
||||
description = (summary or "").strip() or body_text
|
||||
if description:
|
||||
schema["description"] = description
|
||||
if location:
|
||||
schema["locationCreated"] = {"@type": "Place", "name": location.strip()}
|
||||
# همهٔ تصاویر (شاخص + گالری) بهصورت ImageObject با نام — تا هر تصویر هویت داشته باشد
|
||||
paths = [project.image] if (project.image or "").strip() else []
|
||||
for g in (project.gallery or []):
|
||||
if isinstance(g, str) and g.strip() and g not in paths:
|
||||
paths.append(g)
|
||||
if paths:
|
||||
images = [
|
||||
{
|
||||
"@type": "ImageObject",
|
||||
"url": _absolute(p),
|
||||
"name": f"{title} — {'تصویر پروژه' if lang == 'fa' else 'project photo'}",
|
||||
"caption": (f"{title}" + (f"، {location.strip()}" if location else "")),
|
||||
"contentUrl": _absolute(p),
|
||||
}
|
||||
for p in paths[:12]
|
||||
]
|
||||
schema["image"] = images if len(images) > 1 else images[0]
|
||||
return schema
|
||||
|
||||
|
||||
def _project_content_html(project: models.Project, lang: str, brand: str) -> str:
|
||||
"""محتوای خوانا صفحهٔ پروژه برای خزندههای بدون جاوااسکریپت + توضیح تصاویر."""
|
||||
def en(fa_value: str, en_value: str | None) -> str:
|
||||
return (en_value or "").strip() if (lang == "en" and (en_value or "").strip()) else (fa_value or "").strip()
|
||||
|
||||
esc = html.escape
|
||||
title = en(project.title, project.title_en)
|
||||
parts = [f"<h1>{esc(title)}</h1>"]
|
||||
if en(project.summary, project.summary_en):
|
||||
parts.append(f"<p>{esc(en(project.summary, project.summary_en))}</p>")
|
||||
elif isinstance(project.body, dict):
|
||||
text = _doc_text(project.body, 2000)
|
||||
if text:
|
||||
parts.append(f"<p>{esc(text)}</p>")
|
||||
|
||||
facts = []
|
||||
for fa_v, en_v, label_fa, label_en in [
|
||||
(project.location, project.location_en, "موقعیت", "Location"),
|
||||
(project.year, project.year_en, "سال", "Year"),
|
||||
(project.area, getattr(project, "area_en", ""), "زیربنا", "Area"),
|
||||
(project.client, getattr(project, "client_en", ""), "کارفرما", "Client"),
|
||||
]:
|
||||
val = en(fa_v or "", en_v)
|
||||
if val:
|
||||
facts.append(f"<li>{label_en if lang == 'en' else label_fa}: {esc(val)}</li>")
|
||||
if facts:
|
||||
parts.append(f"<ul>{''.join(facts)}</ul>")
|
||||
|
||||
# نام و موقعیت هر تصویر — همان چیزی که سیستمهای هوش مصنوعی برای اعتبارسنجی لازم دارند
|
||||
images = []
|
||||
if (project.image or "").strip():
|
||||
images.append(project.image)
|
||||
for g in (project.gallery or []):
|
||||
if isinstance(g, str) and g.strip() and g not in images:
|
||||
images.append(g)
|
||||
if images:
|
||||
label = "Project photos" if lang == "en" else "تصاویر پروژه"
|
||||
items = "".join(
|
||||
f'<li><img src="{esc(p)}" alt="{esc(title)}'
|
||||
+ (f' — {esc(en(project.location, project.location_en))}' if en(project.location, project.location_en) else "")
|
||||
+ f'" /> {esc(title)}</li>'
|
||||
for p in images[:12]
|
||||
)
|
||||
parts.append(f"<h2>{label}</h2><ul>{items}</ul>")
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
def _faq_jsonld(article: models.Article, lang: str) -> dict | None:
|
||||
"""اسکیمای FAQPage از پرسشوپاسخهای پایان مقاله."""
|
||||
entities = _faq_entities(article.faqs, lang, article.faqs_en)
|
||||
@@ -803,20 +1100,14 @@ def project_detail(slug: str, db: Session = Depends(get_db)):
|
||||
project = db.query(models.Project).filter(models.Project.slug == slug).first()
|
||||
if project is None:
|
||||
return _shell(
|
||||
db=db,
|
||||
title=_brand_name(db), description="", path=f"/projects/{slug}",
|
||||
org_jsonld=_org_jsonld(db, "fa"), status_code=404,
|
||||
)
|
||||
|
||||
brand = _brand_name(db)
|
||||
jsonld = [
|
||||
{
|
||||
"@context": "https://schema.org",
|
||||
"@type": "WebPage",
|
||||
"name": project.title,
|
||||
"description": project.summary,
|
||||
"image": _absolute(project.image),
|
||||
"inLanguage": "fa",
|
||||
},
|
||||
_project_schema(project, "fa", db),
|
||||
_breadcrumb_jsonld([
|
||||
(_nav_label(db, "/", "fa") or "خانه", "/"),
|
||||
(_nav_label(db, "/projects", "fa") or "پروژهها", "/projects"),
|
||||
@@ -824,6 +1115,7 @@ def project_detail(slug: str, db: Session = Depends(get_db)):
|
||||
]),
|
||||
]
|
||||
return _shell(
|
||||
db=db,
|
||||
title=f"{project.title} | {brand}",
|
||||
description=project.summary,
|
||||
path=f"/projects/{project.slug}",
|
||||
@@ -832,6 +1124,7 @@ def project_detail(slug: str, db: Session = Depends(get_db)):
|
||||
og_image_alt=project.title,
|
||||
jsonld=jsonld,
|
||||
org_jsonld=_org_jsonld(db, "fa"),
|
||||
noscript=_project_content_html(project, "fa", brand),
|
||||
)
|
||||
|
||||
|
||||
@@ -854,6 +1147,7 @@ def _en_blog_detail(article: models.Article, db: Session) -> HTMLResponse:
|
||||
jsonld.append(faq_schema)
|
||||
noscript = f"<h1>{html.escape(title)}</h1><p>{html.escape(first_text)}</p>"
|
||||
return _shell(
|
||||
db=db,
|
||||
title=f"{title} | {brand}",
|
||||
description=_meta_desc_for(article, "en"),
|
||||
path=f"/en/blog/{(article.slug_en or '').strip() or article.slug}",
|
||||
@@ -892,6 +1186,7 @@ def en_blog_detail_route(slug: str, db: Session = Depends(get_db)):
|
||||
)
|
||||
if article is None:
|
||||
return _shell(
|
||||
db=db,
|
||||
title=_brand_name(db, "en"), description="", path=f"/en/blog/{slug}", lang="en",
|
||||
org_jsonld=_org_jsonld(db, "en"), status_code=404,
|
||||
)
|
||||
@@ -908,6 +1203,7 @@ def en_project_detail(slug: str, db: Session = Depends(get_db)):
|
||||
project = db.query(models.Project).filter(models.Project.slug == slug).first()
|
||||
if project is None:
|
||||
return _shell(
|
||||
db=db,
|
||||
title=_brand_name(db, "en"), description="", path=f"/en/projects/{slug}", lang="en",
|
||||
org_jsonld=_org_jsonld(db, "en"), status_code=404,
|
||||
)
|
||||
@@ -916,14 +1212,7 @@ def en_project_detail(slug: str, db: Session = Depends(get_db)):
|
||||
title = project.title_en or project.title
|
||||
summary = project.summary_en or project.summary
|
||||
jsonld = [
|
||||
{
|
||||
"@context": "https://schema.org",
|
||||
"@type": "WebPage",
|
||||
"name": title,
|
||||
"description": summary,
|
||||
"image": _absolute(project.image),
|
||||
"inLanguage": "en",
|
||||
},
|
||||
_project_schema(project, "en", db),
|
||||
_breadcrumb_jsonld([
|
||||
(_nav_label(db, "/", "en") or "Home", "/"),
|
||||
(_nav_label(db, "/projects", "en") or "Projects", "/en/projects"),
|
||||
@@ -931,6 +1220,7 @@ def en_project_detail(slug: str, db: Session = Depends(get_db)):
|
||||
]),
|
||||
]
|
||||
return _shell(
|
||||
db=db,
|
||||
title=f"{title} | {brand}",
|
||||
description=summary,
|
||||
path=f"/en/projects/{project.slug}",
|
||||
@@ -940,6 +1230,7 @@ def en_project_detail(slug: str, db: Session = Depends(get_db)):
|
||||
og_image_alt=title,
|
||||
jsonld=jsonld,
|
||||
org_jsonld=_org_jsonld(db, "en"),
|
||||
noscript=_project_content_html(project, "en", brand),
|
||||
)
|
||||
|
||||
|
||||
@@ -950,6 +1241,7 @@ def en_page(path: str, db: Session = Depends(get_db)):
|
||||
page_key = path.split("/")[0] if path else ""
|
||||
if page_key not in META_KEYS:
|
||||
return _shell(
|
||||
db=db,
|
||||
title=_brand_name(db, "en"), description="", path=f"/en/{path}".rstrip("/"), lang="en",
|
||||
org_jsonld=_org_jsonld(db, "en"), status_code=404,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user