109 行
5.1 KiB
Python
109 行
5.1 KiB
Python
"""Import remaining JSON data files into database."""
|
|
import json, os, sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
DATA_DIR = '/opt/gw/hulai-website/data'
|
|
|
|
def load(name):
|
|
path = os.path.join(DATA_DIR, name)
|
|
if os.path.exists(path):
|
|
with open(path, 'r', encoding='utf-8') as f:
|
|
return json.load(f)
|
|
return None
|
|
|
|
from app.database import SessionLocal
|
|
db = SessionLocal()
|
|
|
|
# === PRICING ===
|
|
from app.models.pricing import PricingItem
|
|
d = load('pricing.json')
|
|
if d and not db.query(PricingItem).first():
|
|
for i, p in enumerate(d.get('products', [])):
|
|
db.add(PricingItem(product_slug=p.get('id',''), product_name=p.get('name',''),
|
|
price_label=p.get('priceLabel',''), description=p.get('priceNote',''),
|
|
features=p.get('highlights',[]), is_visible=True, sort_order=i))
|
|
print('pricing imported')
|
|
|
|
# === QUALIFICATIONS ===
|
|
from app.models.qualification import Qualification
|
|
d = load('qualifications.json')
|
|
if d and not db.query(Qualification).first():
|
|
i = 0
|
|
for q in d.get('licenses', []):
|
|
db.add(Qualification(title=q['title'], issuer=q.get('issuer',''), description=q.get('description',''), category='license', sort_order=i)); i+=1
|
|
ins = d.get('insurance', {})
|
|
if ins and ins.get('title'):
|
|
db.add(Qualification(title=ins['title'], issuer=ins.get('issuer',''), description=ins.get('description',''), category='insurance', sort_order=i)); i+=1
|
|
for q in d.get('awards', []):
|
|
db.add(Qualification(title=q['title'], issuer=q.get('issuer',''), year=str(q.get('year','')), description=q.get('description',''), category='award', sort_order=i)); i+=1
|
|
her = d.get('heritage', {})
|
|
if her and her.get('title'):
|
|
db.add(Qualification(title=her['title'], description=her.get('description',''), category='heritage', sort_order=i))
|
|
print('qualifications imported')
|
|
|
|
# === PARTNERS ===
|
|
from app.models.partner import Partner
|
|
d = load('partners.json')
|
|
if d and not db.query(Partner).first():
|
|
cat_map = {'govPartners':'政府合作','associations':'行业协会','academicCoops':'校企合作',
|
|
'scenicPartners':'景区合作','hotelPartners':'酒店合作','platformEndorsements':'平台认证','mediaReports':'媒体报道'}
|
|
i = 0
|
|
for key, cat_name in cat_map.items():
|
|
for p in d.get(key, []):
|
|
db.add(Partner(name=p['name'], description=p.get('description',''), website=p.get('website',''), category=cat_name, sort_order=i)); i+=1
|
|
print('partners imported')
|
|
|
|
# === GALLERY ===
|
|
from app.models.gallery import GalleryItem
|
|
d = load('gallery.json')
|
|
if d and not db.query(GalleryItem).first():
|
|
for i, g in enumerate(d.get('works', [])):
|
|
db.add(GalleryItem(title=g.get('title',''), location=g.get('location',''),
|
|
description=g.get('description',''), image=g.get('image',''),
|
|
tags=[g['category']] if g.get('category') else [], is_visible=True, sort_order=i))
|
|
print('gallery imported')
|
|
|
|
# === SITE IMAGES ===
|
|
from app.models.site_image import SiteImage
|
|
d = load('images.json')
|
|
if d and not db.query(SiteImage).first():
|
|
for group, imgs in d.items():
|
|
for key, path in imgs.items():
|
|
db.add(SiteImage(group_name=group, image_key=key, image_path=path))
|
|
print('images imported')
|
|
|
|
# === DESTINATIONS ===
|
|
from app.models.destination import DestinationConfig, DestinationItem, DestinationDimension, DestinationHonestItem
|
|
d = load('destinations.json')
|
|
if d and not db.query(DestinationConfig).first():
|
|
cfg = DestinationConfig(title=d.get('title',''), subtitle=d.get('subtitle',''), intro=d.get('intro',''),
|
|
honest_title=d.get('honestNote',{}).get('title',''), honest_subtitle=d.get('honestNote',{}).get('subtitle',''),
|
|
closing_title=d.get('closing',{}).get('title',''), closing_text=d.get('closing',{}).get('text',''),
|
|
data_sources=d.get('dataSources',''))
|
|
db.add(cfg)
|
|
db.flush()
|
|
for i, dest in enumerate(d.get('destinations',[])):
|
|
db.add(DestinationItem(dest_id=dest['id'], name=dest['name'], tag=dest.get('tag',''), highlight=dest.get('highlight',False), sort_order=i))
|
|
for i, dim in enumerate(d.get('dimensions',[])):
|
|
db.add(DestinationDimension(label=dim['label'], icon=dim.get('icon',''), values=dim.get('values',[]), sort_order=i))
|
|
for i, item in enumerate(d.get('honestNote',{}).get('items',[])):
|
|
db.add(DestinationHonestItem(text=item, sort_order=i))
|
|
print('destinations imported')
|
|
|
|
# === DESTINATIONS DETAIL ===
|
|
from app.models.destination_detail import DestinationDetail
|
|
d = load('destinations-detail.json')
|
|
if d and not db.query(DestinationDetail).first():
|
|
for i, dd in enumerate(d.get('destinations',[])):
|
|
db.add(DestinationDetail(slug=dd.get('id',''), name=dd.get('name',''), subtitle=dd.get('subtitle',''),
|
|
tags=[dd['tag']] if dd.get('tag') else [], description=dd.get('description',''),
|
|
highlights=dd.get('highlights',[]),
|
|
best_season=dd.get('bestSeason',{}).get('primary',''),
|
|
cover_image=dd.get('heroImage',''), is_visible=True, sort_order=i))
|
|
print('destinations-detail imported')
|
|
|
|
db.commit()
|
|
print('=== All remaining data imported ===')
|
|
db.close()
|