Spaces:
Running
Running
File size: 7,074 Bytes
92bfe31 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | """
Register curriculum document metadata in Firestore.
Populates the curriculumDocuments collection so the app can display
lessons mapped to their source PDFs before ingestion.
Run: python backend/scripts/register_firestore_metadata.py
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
def _get_firestore_client():
try:
import firebase_admin
from firebase_admin import firestore
if not firebase_admin._apps:
sa_json = os.getenv("FIREBASE_SERVICE_ACCOUNT_JSON")
sa_file = os.getenv("FIREBASE_SERVICE_ACCOUNT_FILE")
bucket_name = os.getenv("FIREBASE_STORAGE_BUCKET", "mathpulse-ai-2026.firebasestorage.app")
if sa_json:
import json as _json
from firebase_admin import credentials
creds = credentials.Certificate(_json.loads(sa_json))
firebase_admin.initialize_app(creds, {"storageBucket": bucket_name})
elif sa_file and Path(sa_file).exists():
from firebase_admin import credentials
creds = credentials.Certificate(sa_file)
firebase_admin.initialize_app(creds, {"storageBucket": bucket_name})
else:
firebase_admin.initialize_app(options={"storageBucket": bucket_name})
return firestore.client()
except Exception as e:
print(f"Firestore init failed: {e}")
return None
CURRICULUM_DOCUMENTS = [
{
"id": "gm_lesson_1",
"moduleId": "gm-q1-business-finance",
"lessonId": "gm-q1-bf-1",
"title": "Represent business transactions and financial goals using variables and equations.",
"subject": "General Mathematics",
"subjectId": "gen-math",
"quarter": 1,
"competencyCode": "GM11-BF-1",
"learningCompetency": "Represent business transactions and financial goals using variables and equations.",
"storagePath": "curriculum/general_math/GENERAL-MATHEMATICS-1.pdf",
"status": "uploaded",
},
{
"id": "gm_navotas_lesson_1",
"moduleId": "gm-q1-patterns-sequences-series",
"lessonId": "gm-q1-pss-1",
"title": "Identify and describe arithmetic and geometric patterns in data.",
"subject": "General Mathematics",
"subjectId": "gen-math",
"quarter": 1,
"competencyCode": "GM11-PSS-1",
"learningCompetency": "Identify and describe arithmetic and geometric patterns in data.",
"storagePath": "curriculum/gen_math_sdo/SDO_Navotas_Gen.Math_SHS_1stSem.FV.pdf",
"status": "uploaded",
},
{
"id": "bm_lesson_1",
"moduleId": "bm-q1-business-math",
"lessonId": "bm-q1-1",
"title": "Translate verbal phrases to mathematical expressions; model business scenarios using linear equations and inequalities.",
"subject": "Business Mathematics",
"subjectId": "business-math",
"quarter": 1,
"competencyCode": "ABM_BM11BS-Ia-b-1",
"learningCompetency": "Translate verbal phrases to mathematical expressions; model business scenarios using linear equations and inequalities.",
"storagePath": "curriculum/business_math/SDO_Navotas_Bus.Math_SHS_1stSem.FV.pdf",
"status": "uploaded",
},
{
"id": "stat_lesson_1",
"moduleId": "stat-q1-probability",
"lessonId": "stat-q1-1",
"title": "Define and describe random variables and their types.",
"subject": "Statistics and Probability",
"subjectId": "stats-prob",
"quarter": 1,
"competencyCode": "SP_SHS11-Ia-1",
"learningCompetency": "Define and describe random variables and their types.",
"storagePath": "curriculum/stat_prob/SDO_Navotas_STAT_PROB_SHS_1stSem.FV.pdf",
"status": "uploaded",
},
{
"id": "fm1_lesson_1",
"moduleId": "fm1-q1-counting",
"lessonId": "fm1-q1-fpc-1",
"title": "Apply the fundamental counting principle in contextual problems.",
"subject": "Finite Mathematics 1",
"subjectId": "finite-math-1",
"quarter": 1,
"competencyCode": "FM1-SHS11-Ia-1",
"learningCompetency": "Apply the fundamental counting principle in contextual problems.",
"storagePath": "curriculum/finite_math/Finite-Mathematics-1-1.pdf",
"status": "uploaded",
},
{
"id": "fm2_lesson_1",
"moduleId": "fm2-q1-matrices",
"lessonId": "fm2-q1-matrices-1",
"title": "Represent contextual data using matrix notation.",
"subject": "Finite Mathematics 2",
"subjectId": "finite-math-2",
"quarter": 1,
"competencyCode": "FM2-SHS11-Ia-1",
"learningCompetency": "Represent contextual data using matrix notation.",
"storagePath": "curriculum/finite_math/Finite-Mathematics-2-1.pdf",
"status": "uploaded",
},
{
"id": "org_mgmt_lesson_1",
"moduleId": "org-mgmt-q1",
"lessonId": "org-mgmt-q1-1",
"title": "Understand the fundamental concepts of organization and management.",
"subject": "Organization and Management",
"subjectId": "org-mgmt",
"quarter": 1,
"competencyCode": "ABM_OM11-Ia-1",
"learningCompetency": "Understand the fundamental concepts of organization and management.",
"storagePath": "curriculum/org_mgmt/SDO_Navotas_SHS_ABM_OrgAndMngt_FirstSem_FV.pdf",
"status": "uploaded",
},
]
def register_metadata(force: bool = False):
db = _get_firestore_client()
if db is None:
print("ERROR: Cannot connect to Firestore. Check credentials.")
print("Set FIREBASE_SERVICE_ACCOUNT_JSON or place mathpulse-sa.json in backend/ directory.")
return
print("Connected to Firestore.")
print("-" * 50)
registered = 0
skipped = 0
updated = 0
for doc in CURRICULUM_DOCUMENTS:
doc_id = doc["id"]
doc_ref = db.collection("curriculumDocuments").document(doc_id)
existing = doc_ref.get()
if existing.exists and not force:
print(f"[SKIP] {doc_id} already registered")
skipped += 1
continue
if existing.exists and force:
updated += 1
else:
registered += 1
data = {
**doc,
"uploadedAt": None,
}
doc_ref.set(data, merge=True)
print(f"[OK] {'Updated' if force and existing.exists else 'Registered'} {doc_id} -> {doc.get('storagePath')}")
print("-" * 50)
print(f"Done: {registered} registered, {skipped} skipped, {updated} updated.")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Register curriculum document metadata in Firestore")
parser.add_argument("--force", action="store_true", help="Overwrite existing records")
args = parser.parse_args()
register_metadata(force=args.force) |