File size: 20,659 Bytes
bee9bbb | 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | """Owner-scoped enrolled-course and assignment persistence for site 33."""
from __future__ import annotations
import json
import sqlite3
import unicodedata
from collections.abc import Mapping, Sequence
from datetime import datetime, timedelta, timezone
from uuid import uuid4
import enrolled_course
COURSE_ENROLLMENT_ID = "deep-learning-specialization"
COURSE_ID = enrolled_course.COURSE_ID
ASSIGNMENT_ID = enrolled_course.ASSIGNMENT_ID
LESSON_ID = enrolled_course.LESSON["id"]
FROZEN_TIME = "2026-08-16T00:00:00+00:00"
UTC = timezone.utc
MAX_NOTE_LENGTH = 5000
MAX_ATTEMPTS_PER_WINDOW = 3
_SCHEMA = (
"""CREATE TABLE IF NOT EXISTS coursera_enrolled_course_state (
owner_subject_id TEXT NOT NULL, course_id TEXT NOT NULL,
current_week INTEGER NOT NULL DEFAULT 1,
lesson_opened INTEGER NOT NULL DEFAULT 0 CHECK(lesson_opened IN (0,1)),
assignment_completed INTEGER NOT NULL DEFAULT 0 CHECK(assignment_completed IN (0,1)),
updated_at TEXT NOT NULL,
PRIMARY KEY(owner_subject_id,course_id))""",
"""CREATE TABLE IF NOT EXISTS coursera_course_notes (
note_id INTEGER PRIMARY KEY AUTOINCREMENT,
owner_subject_id TEXT NOT NULL, course_id TEXT NOT NULL,
lesson_id TEXT NOT NULL, note_text TEXT NOT NULL,
created_at TEXT NOT NULL, updated_at TEXT NOT NULL)""",
"""CREATE TABLE IF NOT EXISTS coursera_assignment_attempts (
attempt_id TEXT PRIMARY KEY, owner_subject_id TEXT NOT NULL,
course_id TEXT NOT NULL, assignment_id TEXT NOT NULL,
attempt_number INTEGER NOT NULL,
status TEXT NOT NULL CHECK(status IN ('in_progress','submitted')),
started_at TEXT NOT NULL, expires_at TEXT NOT NULL,
submitted_at TEXT, submission_reason TEXT,
UNIQUE(owner_subject_id,assignment_id,attempt_number))""",
"""CREATE TABLE IF NOT EXISTS coursera_assignment_drafts (
attempt_id TEXT PRIMARY KEY REFERENCES coursera_assignment_attempts(attempt_id),
owner_subject_id TEXT NOT NULL, answers_json TEXT NOT NULL,
updated_at TEXT NOT NULL)""",
"""CREATE TABLE IF NOT EXISTS coursera_assignment_results (
attempt_id TEXT PRIMARY KEY REFERENCES coursera_assignment_attempts(attempt_id),
owner_subject_id TEXT NOT NULL, result_json TEXT NOT NULL,
created_at TEXT NOT NULL)""",
"CREATE INDEX IF NOT EXISTS coursera_notes_owner_idx ON coursera_course_notes(owner_subject_id,course_id,note_id)",
"CREATE INDEX IF NOT EXISTS coursera_attempts_owner_idx ON coursera_assignment_attempts(owner_subject_id,assignment_id,status)",
)
def migrate(connection: sqlite3.Connection) -> None:
for statement in _SCHEMA:
connection.execute(statement)
connection.execute(
"INSERT OR IGNORE INTO coursera_schema_migrations(migration_id,applied_at) VALUES (?,?)",
("0002-enrolled-assignment", FROZEN_TIME),
)
def seed(connection: sqlite3.Connection) -> None:
connection.execute(
"""INSERT OR IGNORE INTO coursera_enrolled_course_state(
owner_subject_id,course_id,current_week,lesson_opened,
assignment_completed,updated_at) VALUES (?,?,1,0,0,?)""",
("learner-in-progress", COURSE_ID, FROZEN_TIME),
)
def _connection(*, transaction: bool = False):
from backend import learning_db
return learning_db.connection(transaction=transaction)
def _now(value: datetime | None) -> datetime:
current = value or datetime.now(UTC)
if current.tzinfo is None:
raise ValueError("server time must be timezone-aware")
return current.astimezone(UTC)
def _iso(value: datetime) -> str:
return value.astimezone(UTC).isoformat()
def _parse(value: str) -> datetime:
return datetime.fromisoformat(value).astimezone(UTC)
def _require_access(connection: sqlite3.Connection, subject_id: str) -> None:
row = connection.execute(
"""SELECT 1 FROM coursera_enrollments
WHERE owner_subject_id=? AND course_id=? AND status='active'""",
(subject_id, COURSE_ENROLLMENT_ID),
).fetchone()
if row is None:
raise LookupError("Active enrollment not found")
def course_access(subject_id: str) -> dict[str, object]:
with _connection() as connection:
_require_access(connection, subject_id)
row = connection.execute(
"""SELECT current_week,lesson_opened,assignment_completed,updated_at
FROM coursera_enrolled_course_state
WHERE owner_subject_id=? AND course_id=?""",
(subject_id, COURSE_ID),
).fetchone()
if row is None:
return {
"enrolled": True,
"current_week": 1,
"lesson_opened": False,
"assignment_completed": False,
"updated_at": FROZEN_TIME,
}
return {
"enrolled": True,
"current_week": int(row["current_week"]),
"lesson_opened": bool(row["lesson_opened"]),
"assignment_completed": bool(row["assignment_completed"]),
"updated_at": str(row["updated_at"]),
}
def mark_lesson_opened(subject_id: str) -> None:
current = _iso(_now(None))
with _connection(transaction=True) as connection:
_require_access(connection, subject_id)
connection.execute(
"""INSERT INTO coursera_enrolled_course_state(
owner_subject_id,course_id,current_week,lesson_opened,
assignment_completed,updated_at) VALUES (?,?,1,1,0,?)
ON CONFLICT(owner_subject_id,course_id) DO UPDATE SET
lesson_opened=1,updated_at=excluded.updated_at""",
(subject_id, COURSE_ID, current),
)
def _note(row: sqlite3.Row) -> dict[str, object]:
return {
"note_id": int(row["note_id"]),
"text": str(row["note_text"]),
"lesson_id": str(row["lesson_id"]),
"created_at": str(row["created_at"]),
"updated_at": str(row["updated_at"]),
}
def save_note(subject_id: str, text: str) -> dict[str, object]:
normalized = text.strip()
if not normalized:
raise ValueError("Note text is required")
if len(normalized) > MAX_NOTE_LENGTH:
raise ValueError("Note text is too long")
current = _iso(_now(None))
with _connection(transaction=True) as connection:
_require_access(connection, subject_id)
cursor = connection.execute(
"""INSERT INTO coursera_course_notes(
owner_subject_id,course_id,lesson_id,note_text,created_at,updated_at)
VALUES (?,?,?,?,?,?)""",
(subject_id, COURSE_ID, LESSON_ID, normalized, current, current),
)
row = connection.execute(
"SELECT * FROM coursera_course_notes WHERE note_id=? AND owner_subject_id=?",
(cursor.lastrowid, subject_id),
).fetchone()
if row is None: # pragma: no cover - insert invariant
raise RuntimeError("note insert returned no row")
return _note(row)
def list_notes(subject_id: str, query: str = "") -> list[dict[str, object]]:
normalized = query.strip().casefold()
with _connection() as connection:
rows = connection.execute(
"""SELECT * FROM coursera_course_notes
WHERE owner_subject_id=? AND course_id=? ORDER BY note_id DESC""",
(subject_id, COURSE_ID),
).fetchall()
notes = [_note(row) for row in rows]
if normalized:
notes = [note for note in notes if normalized in str(note["text"]).casefold()]
return notes
def delete_note(subject_id: str, note_id: int) -> None:
with _connection(transaction=True) as connection:
deleted = connection.execute(
"DELETE FROM coursera_course_notes WHERE note_id=? AND owner_subject_id=? AND course_id=?",
(note_id, subject_id, COURSE_ID),
)
if deleted.rowcount != 1:
raise LookupError("Note not found")
def _encode_answers(answers: Mapping[int, Sequence[int]]) -> str:
return json.dumps(
{str(number): list(selected) for number, selected in sorted(answers.items())},
separators=(",", ":"),
sort_keys=True,
)
def _decode_answers(payload: str) -> dict[int, tuple[int, ...]]:
parsed = json.loads(payload)
return {int(number): tuple(int(value) for value in values) for number, values in parsed.items()}
def _draft_answers(connection: sqlite3.Connection, attempt_id: str) -> dict[int, tuple[int, ...]]:
row = connection.execute(
"SELECT answers_json FROM coursera_assignment_drafts WHERE attempt_id=?",
(attempt_id,),
).fetchone()
return {} if row is None else _decode_answers(str(row["answers_json"]))
def _result(connection: sqlite3.Connection, subject_id: str, attempt_id: str) -> dict[str, object]:
row = connection.execute(
"""SELECT result_json FROM coursera_assignment_results
WHERE attempt_id=? AND owner_subject_id=?""",
(attempt_id, subject_id),
).fetchone()
if row is None:
raise LookupError("Attempt not found")
result = json.loads(str(row["result_json"]))
for item in result["question_results"]:
item["selected"] = tuple(item["selected"])
return result
def _attempt_view(
connection: sqlite3.Connection,
row: sqlite3.Row,
current: datetime,
) -> dict[str, object]:
if row["status"] == "submitted":
return _result(connection, str(row["owner_subject_id"]), str(row["attempt_id"]))
remaining = max(0, int((_parse(str(row["expires_at"])) - current).total_seconds()))
return {
"attempt_id": str(row["attempt_id"]),
"attempt_number": int(row["attempt_number"]),
"status": "in_progress",
"started_at": str(row["started_at"]),
"expires_at": str(row["expires_at"]),
"remaining_seconds": remaining,
"answers": _draft_answers(connection, str(row["attempt_id"])),
}
def _store_result(
connection: sqlite3.Connection,
row: sqlite3.Row,
scored: list[dict[str, object]],
*,
submitted_at: datetime,
reason: str,
) -> dict[str, object]:
score = sum(int(item["points_awarded"]) for item in scored)
max_score = sum(int(question["points"]) for question in enrolled_course.QUESTIONS)
result: dict[str, object] = {
"attempt_id": str(row["attempt_id"]),
"attempt_number": int(row["attempt_number"]),
"status": "submitted",
"submitted_at": _iso(submitted_at),
"submission_reason": reason,
"score": score,
"max_score": max_score,
"percentage": round(score * 100 / max_score),
"passed": score >= 8,
"provenance": enrolled_course.ANSWER_KEY_PROVENANCE,
"question_results": scored,
}
payload = json.dumps(result, separators=(",", ":"), sort_keys=True)
connection.execute(
"""INSERT INTO coursera_assignment_results(
attempt_id,owner_subject_id,result_json,created_at) VALUES (?,?,?,?)""",
(row["attempt_id"], row["owner_subject_id"], payload, _iso(submitted_at)),
)
connection.execute(
"""UPDATE coursera_assignment_attempts
SET status='submitted',submitted_at=?,submission_reason=?
WHERE attempt_id=? AND owner_subject_id=?""",
(_iso(submitted_at), reason, row["attempt_id"], row["owner_subject_id"]),
)
connection.execute(
"""INSERT INTO coursera_enrolled_course_state(
owner_subject_id,course_id,current_week,lesson_opened,
assignment_completed,updated_at) VALUES (?,?,1,0,1,?)
ON CONFLICT(owner_subject_id,course_id) DO UPDATE SET
assignment_completed=1,updated_at=excluded.updated_at""",
(row["owner_subject_id"], COURSE_ID, _iso(submitted_at)),
)
return _result(connection, str(row["owner_subject_id"]), str(row["attempt_id"]))
def _expire_if_needed(
connection: sqlite3.Connection, row: sqlite3.Row, current: datetime
) -> sqlite3.Row:
if row["status"] != "in_progress" or current < _parse(str(row["expires_at"])):
return row
answers = _draft_answers(connection, str(row["attempt_id"]))
scored = enrolled_course.score_expired_answers(answers)
_store_result(
connection,
row,
scored,
submitted_at=_parse(str(row["expires_at"])),
reason="expired",
)
refreshed = connection.execute(
"SELECT * FROM coursera_assignment_attempts WHERE attempt_id=?",
(row["attempt_id"],),
).fetchone()
if refreshed is None: # pragma: no cover - update invariant
raise RuntimeError("expired attempt disappeared")
return refreshed
def start_or_resume_attempt(
subject_id: str, now: datetime | None = None
) -> dict[str, object]:
current = _now(now)
with _connection(transaction=True) as connection:
_require_access(connection, subject_id)
row = connection.execute(
"""SELECT * FROM coursera_assignment_attempts
WHERE owner_subject_id=? AND assignment_id=? AND status='in_progress'
ORDER BY attempt_number DESC LIMIT 1""",
(subject_id, ASSIGNMENT_ID),
).fetchone()
if row is not None:
row = _expire_if_needed(connection, row, current)
if row["status"] == "in_progress":
return _attempt_view(connection, row, current)
window_start = _iso(current - timedelta(hours=24))
recent_count = int(
connection.execute(
"""SELECT COUNT(*) FROM coursera_assignment_attempts
WHERE owner_subject_id=? AND assignment_id=?
AND status='submitted' AND submitted_at>?""",
(subject_id, ASSIGNMENT_ID, window_start),
).fetchone()[0]
)
if recent_count >= MAX_ATTEMPTS_PER_WINDOW:
raise ValueError("Try again after the 24 hours wait period")
attempt_number = int(
connection.execute(
"""SELECT COUNT(*) FROM coursera_assignment_attempts
WHERE owner_subject_id=? AND assignment_id=?""",
(subject_id, ASSIGNMENT_ID),
).fetchone()[0]
) + 1
attempt_id = uuid4().hex
expires = current + timedelta(minutes=int(enrolled_course.ATTEMPT_RULES["duration_minutes"]))
connection.execute(
"""INSERT INTO coursera_assignment_attempts(
attempt_id,owner_subject_id,course_id,assignment_id,attempt_number,
status,started_at,expires_at,submitted_at,submission_reason)
VALUES (?,?,?,?,?,'in_progress',?,?,NULL,NULL)""",
(attempt_id, subject_id, COURSE_ID, ASSIGNMENT_ID, attempt_number, _iso(current), _iso(expires)),
)
connection.execute(
"""INSERT INTO coursera_assignment_drafts(
attempt_id,owner_subject_id,answers_json,updated_at) VALUES (?,?,?,?)""",
(attempt_id, subject_id, "{}", _iso(current)),
)
row = connection.execute(
"SELECT * FROM coursera_assignment_attempts WHERE attempt_id=?",
(attempt_id,),
).fetchone()
if row is None: # pragma: no cover - insert invariant
raise RuntimeError("attempt insert returned no row")
return _attempt_view(connection, row, current)
def get_attempt(
subject_id: str, attempt_id: str, now: datetime | None = None
) -> dict[str, object]:
current = _now(now)
with _connection(transaction=True) as connection:
row = connection.execute(
"""SELECT * FROM coursera_assignment_attempts
WHERE attempt_id=? AND owner_subject_id=? AND assignment_id=?""",
(attempt_id, subject_id, ASSIGNMENT_ID),
).fetchone()
if row is None:
raise LookupError("Attempt not found")
row = _expire_if_needed(connection, row, current)
return _attempt_view(connection, row, current)
def current_attempt(
subject_id: str, now: datetime | None = None
) -> dict[str, object]:
"""Return the current attempt without creating one from a GET request."""
current = _now(now)
with _connection(transaction=True) as connection:
_require_access(connection, subject_id)
row = connection.execute(
"""SELECT * FROM coursera_assignment_attempts
WHERE owner_subject_id=? AND assignment_id=? AND status='in_progress'
ORDER BY attempt_number DESC LIMIT 1""",
(subject_id, ASSIGNMENT_ID),
).fetchone()
if row is None:
raise LookupError("Attempt not found")
row = _expire_if_needed(connection, row, current)
return _attempt_view(connection, row, current)
def save_draft(
subject_id: str,
attempt_id: str,
answers: Mapping[int, Sequence[int]],
now: datetime | None = None,
) -> dict[str, object]:
current = _now(now)
with _connection(transaction=True) as connection:
row = connection.execute(
"""SELECT * FROM coursera_assignment_attempts
WHERE attempt_id=? AND owner_subject_id=? AND assignment_id=?""",
(attempt_id, subject_id, ASSIGNMENT_ID),
).fetchone()
if row is None:
raise LookupError("Attempt not found")
row = _expire_if_needed(connection, row, current)
if row["status"] != "in_progress":
return _attempt_view(connection, row, current)
normalized = enrolled_course.validate_answers(
answers, require_complete=False
)
connection.execute(
"""UPDATE coursera_assignment_drafts SET answers_json=?,updated_at=?
WHERE attempt_id=? AND owner_subject_id=?""",
(_encode_answers(normalized), _iso(current), attempt_id, subject_id),
)
return _attempt_view(connection, row, current)
def _normalize_name(value: str) -> str:
return " ".join(unicodedata.normalize("NFKC", value).split())
def submit_attempt(
subject_id: str,
attempt_id: str,
answers: Mapping[int, Sequence[int]],
legal_name: str,
now: datetime | None = None,
) -> dict[str, object]:
current = _now(now)
with _connection(transaction=True) as connection:
row = connection.execute(
"""SELECT * FROM coursera_assignment_attempts
WHERE attempt_id=? AND owner_subject_id=? AND assignment_id=?""",
(attempt_id, subject_id, ASSIGNMENT_ID),
).fetchone()
if row is None:
raise LookupError("Attempt not found")
if row["status"] == "submitted":
return _result(connection, subject_id, attempt_id)
row = _expire_if_needed(connection, row, current)
if row["status"] == "submitted":
return _result(connection, subject_id, attempt_id)
profile = connection.execute(
"SELECT display_name FROM coursera_profiles WHERE subject_id=?",
(subject_id,),
).fetchone()
if profile is None:
raise LookupError("Learner profile not found")
if _normalize_name(legal_name) != _normalize_name(str(profile["display_name"])):
raise ValueError("Enter your legal name exactly as shown in your profile")
normalized = enrolled_course.validate_answers(answers, require_complete=True)
connection.execute(
"""UPDATE coursera_assignment_drafts SET answers_json=?,updated_at=?
WHERE attempt_id=? AND owner_subject_id=?""",
(_encode_answers(normalized), _iso(current), attempt_id, subject_id),
)
scored = enrolled_course.score_answers(normalized)
return _store_result(
connection, row, scored, submitted_at=current, reason="submitted"
)
def gradebook(subject_id: str) -> list[dict[str, object]]:
with _connection() as connection:
rows = connection.execute(
"""SELECT results.result_json FROM coursera_assignment_results AS results
JOIN coursera_assignment_attempts AS attempts
ON attempts.attempt_id=results.attempt_id
WHERE results.owner_subject_id=? AND attempts.assignment_id=?
ORDER BY attempts.attempt_number DESC""",
(subject_id, ASSIGNMENT_ID),
).fetchall()
result: list[dict[str, object]] = []
for row in rows:
parsed = json.loads(str(row["result_json"]))
for item in parsed["question_results"]:
item["selected"] = tuple(item["selected"])
result.append(parsed)
return result
|