| Server IP : 3.147.158.171 / Your IP : 216.73.216.88 Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.21 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /tsai/repo/api/ |
Upload File : |
"""
Character Edit Script
Fetches a character by slug, modifies a field, and PATCHes it back.
Usage:
python edit_character.py <slug> <site> [--featured-image] [--secondary-image]
[--fact-check] [--current-events]
Arguments:
slug Character slug
site Site slug (e.g., jpt, tsai, sandbox)
Options:
--featured-image / --no-featured-image Toggle createFeaturedImage
--secondary-image / --no-secondary-image Toggle createSecondaryImage
--fact-check / --no-fact-check Toggle enableFactChecking
--corrections / --no-corrections Toggle enableCorrections
--current-events / --no-current-events Toggle researchCurrentEvents
--env <env> Environment (default: TSAI_ENV or development)
"""
import argparse
import os
import sys
from pathlib import Path
parser = argparse.ArgumentParser(description="Edit a character field via the local FastAPI server.")
parser.add_argument("slug", help="Character slug")
parser.add_argument("site", help="Site slug (e.g., jpt, tsai, sandbox)")
parser.add_argument("--featured-image", action=argparse.BooleanOptionalAction,
default=None, help="Set createFeaturedImage")
parser.add_argument("--secondary-image", action=argparse.BooleanOptionalAction,
default=None, help="Set createSecondaryImage")
parser.add_argument("--fact-check", action=argparse.BooleanOptionalAction,
default=None, help="Set enableFactChecking")
parser.add_argument("--corrections", action=argparse.BooleanOptionalAction,
default=None, help="Set enableCorrections")
parser.add_argument("--current-events", action=argparse.BooleanOptionalAction,
default=None, help="Set researchCurrentEvents")
parser.add_argument(
"--env",
choices=["development", "production", "scike_ai"],
default=os.getenv("TSAI_ENV", "development"),
help="Environment (default: TSAI_ENV or development)",
)
args = parser.parse_args()
os.environ["TSAI_ENV"] = args.env
from dotenv import load_dotenv
API_DIR = Path(__file__).resolve().parent
load_dotenv(dotenv_path=API_DIR / ".env")
os.environ["TSAI_ENV"] = args.env
sys.path.insert(0, str(API_DIR))
import requests
from app.services.site_config_service import SiteConfigService
# (arg_value, character_field) pairs — applied only when arg_value is not None
FIELD_UPDATES = [
(args.featured_image, "createFeaturedImage"),
(args.secondary_image, "createSecondaryImage"),
(args.fact_check, "enableFactChecking"),
(args.corrections, "enableCorrections"),
(args.current_events, "researchCurrentEvents"),
]
def main():
if all(value is None for value, _ in FIELD_UPDATES):
print("Error: supply at least one field to edit "
"(e.g. --featured-image, --fact-check)")
sys.exit(1)
site_config_service = SiteConfigService()
site = site_config_service.get_site_by_slug(args.site)
if not site:
print(f'Error: site "{args.site}" not found in {args.env} config.')
sys.exit(1)
origin = site.cb_host if site.cb_host.startswith("http") else f"https://{site.cb_host}"
headers = {"Origin": origin}
get_url = f"http://127.0.0.1:8000/api/v1/cms/character/by-slug/{args.site}/{args.slug}"
try:
resp = requests.get(get_url, headers=headers)
except requests.ConnectionError:
print("Error: Could not connect to FastAPI server at http://127.0.0.1:8000")
print("Start it with: cd api && uvicorn app.main:app --reload")
sys.exit(1)
if resp.status_code != 200:
print(f"Error fetching character '{args.slug}': {resp.status_code} {resp.text}")
sys.exit(1)
character = resp.json()["data"]
print(f"Character: {character['name']} (slug: {character['slug']})")
changes = {}
for value, field in FIELD_UPDATES:
if value is None:
continue
old = character.get(field)
if old is None:
print(f"{field} not set, will be initialized to {value}")
else:
print(f"Current {field}: {old}")
character[field] = value
changes[field] = old
patch_url = "http://127.0.0.1:8000/api/v1/cms/character/"
resp = requests.patch(
patch_url,
json={"character": character},
headers=headers,
)
if resp.status_code != 200:
print(f"Error patching character: {resp.status_code} {resp.text}")
sys.exit(1)
updated = resp.json()["data"]
for field, old_val in changes.items():
print(f"Updated {field}: {old_val} → {updated[field]}")
if __name__ == "__main__":
main()