-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhovn_sync.py
More file actions
83 lines (67 loc) · 2.33 KB
/
hovn_sync.py
File metadata and controls
83 lines (67 loc) · 2.33 KB
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
# hovn_sync.py
# -------------
# Usage:
# python hovn_sync.py brn_WFI79F brn_123ABC brn_456DEF
#
# Pulls students from DB using booking refs,
# scrapes ARC certs, stores them, emails internal summary.
import sys
from db import SessionLocal
from models import Booking, Student
from redcross import scrape_certs_for_email
from main import _upsert_certs_for_student # reuse existing logic
from emailer import send_migration_notice
def run_sync(booking_refs: list[str]):
db = SessionLocal()
print(f"🔍 Starting sync for {len(booking_refs)} booking refs...")
# Normalize refs
refs = [r.strip() for r in booking_refs if r.strip()]
if not refs:
print("No valid booking refs. Exiting.")
return
# Pull bookings
bookings = (
db.query(Booking)
.filter(Booking.hovn_booking_ref.in_(refs))
.all()
)
if not bookings:
print("No matching bookings found in DB.")
return
# Collect unique students
students = {}
for b in bookings:
if b.student:
students[b.student.id] = b.student
print(f"Found {len(students)} unique students to sync.")
# Process each student
for student in students.values():
if not student.email:
print(f"Skipping student {student.id} (no email)")
continue
print(f"→ Scraping ARC certs for {student.email}...")
scraped = scrape_certs_for_email(student.email) or []
if scraped:
saved = _upsert_certs_for_student(db, student, scraped)
print(f" ✓ Saved {len(saved)} certs")
# Internal-only migration email
send_migration_notice(
ref=";".join(refs),
student=student,
certs=[{
"course_name": c.course_name,
"issuer_org": c.issuer_org,
"format": c.format,
"issue_date": c.issue_date,
"expiry_date": c.expiry_date,
} for c in saved],
)
else:
print(" ⚠ No certs found on ARC.")
print("🎉 Migration complete!")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python hovn_sync.py <booking_ref1> <booking_ref2> ...")
sys.exit(1)
booking_refs = sys.argv[1:]
run_sync(booking_refs)