feat: add CMS template functions plugin (get_site_config, get_published_content, get_latest_news)
This commit is contained in:
parent
8130d18bf8
commit
ab3e3b3f64
91
plugins/cms_functions.py
Normal file
91
plugins/cms_functions.py
Normal file
@ -0,0 +1,91 @@
|
||||
"""
|
||||
CMS Jinja2 模板函数插件
|
||||
注册到 ServerEnv,供 index.ui 等模板使用
|
||||
|
||||
函数:
|
||||
get_site_config() → dict
|
||||
get_published_content(content_type, limit) → list[dict]
|
||||
get_latest_news(limit) → list[dict]
|
||||
"""
|
||||
from ahserver.serverenv import ServerEnv
|
||||
from sqlor.dbpools import DBPools
|
||||
from appPublic.jsonConfig import getConfig
|
||||
import os
|
||||
|
||||
_workdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
_config = None
|
||||
_db = None
|
||||
|
||||
|
||||
def _get_db():
|
||||
global _config, _db
|
||||
if _db is None:
|
||||
_config = getConfig(_workdir)
|
||||
_db = DBPools(_config.databases)
|
||||
return _db
|
||||
|
||||
|
||||
async def get_site_config():
|
||||
"""获取站点配置"""
|
||||
try:
|
||||
db = _get_db()
|
||||
async with db.sqlorContext('ocai_cms') as sor:
|
||||
rows = await sor.sqlExe(
|
||||
"SELECT config_key, config_value FROM cms_site_config", {}
|
||||
)
|
||||
cfg = {}
|
||||
for r in rows:
|
||||
cfg[r['config_key']] = r['config_value']
|
||||
return cfg
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
async def get_published_content(content_type, limit=10):
|
||||
"""获取已发布内容"""
|
||||
try:
|
||||
db = _get_db()
|
||||
async with db.sqlorContext('ocai_cms') as sor:
|
||||
rows = await sor.sqlExe(
|
||||
"SELECT c.id, c.title, c.subtitle, c.summary_text, c.image_url, "
|
||||
"c.tags, c.body, cat.name as category_name "
|
||||
"FROM cms_content c "
|
||||
"LEFT JOIN cms_categories cat ON c.category_id = cat.id "
|
||||
"WHERE c.content_type = %(ct)s AND c.status = 'published' "
|
||||
"ORDER BY c.sort_order, c.created_at DESC "
|
||||
"LIMIT %(lim)s",
|
||||
{"ct": content_type, "lim": int(limit)}
|
||||
)
|
||||
return list(rows)
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
|
||||
async def get_latest_news(limit=3):
|
||||
"""获取最新新闻"""
|
||||
try:
|
||||
db = _get_db()
|
||||
async with db.sqlorContext('ocai_cms') as sor:
|
||||
rows = await sor.sqlExe(
|
||||
"SELECT c.id, c.title, c.subtitle, c.summary_text, c.image_url, "
|
||||
"c.published_at, cat.name as category_name "
|
||||
"FROM cms_content c "
|
||||
"LEFT JOIN cms_categories cat ON c.category_id = cat.id "
|
||||
"WHERE c.content_type = 'news' AND c.status = 'published' "
|
||||
"ORDER BY c.published_at DESC, c.created_at DESC "
|
||||
"LIMIT %(lim)s",
|
||||
{"lim": int(limit)}
|
||||
)
|
||||
return list(rows)
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
|
||||
def load_cms():
|
||||
g = ServerEnv()
|
||||
g.get_site_config = get_site_config
|
||||
g.get_published_content = get_published_content
|
||||
g.get_latest_news = get_latest_news
|
||||
|
||||
|
||||
load_cms()
|
||||
@ -1,7 +1,7 @@
|
||||
{% set config = {} %}
|
||||
{% set products = [] %}
|
||||
{% set cases = [] %}
|
||||
{% set news = [] %}
|
||||
{% set config = get_site_config() %}
|
||||
{% set products = get_published_content('product', 10) %}
|
||||
{% set cases = get_published_content('case', 6) %}
|
||||
{% set news = get_latest_news(2) %}
|
||||
{
|
||||
"widgettype": "VBox",
|
||||
"id": "app",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user