From 463b29da45e95e67e8a13eff24468a85f90aefee Mon Sep 17 00:00:00 2001 From: Silas Santini <70163606+pancakereport@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:57:18 -0800 Subject: [PATCH 01/12] Export every student's sections, find a student's sections, and keep CSV exports from running formulas Admins can export a CSV of every student in the offering with their section of each type, or "Floating" for a lab or discussion they aren't in, and search students by name or email to see their sections. Exported cells that start with =, +, -, @, a tab or a carriage return get a leading apostrophe so spreadsheets open them as text, in the new export and in Export Rosters. Co-Authored-By: Claude Opus 5.5 --- server/state.py | 143 ++++++++++++++++++----- src/AdminPage.tsx | 19 +++ src/StudentSearch.tsx | 131 +++++++++++++++++++++ src/models.tsx | 4 +- tests/e2e/test_admin.py | 19 +++ tests/unit/test_student_export_search.py | 127 ++++++++++++++++++++ 6 files changed, 412 insertions(+), 31 deletions(-) create mode 100644 src/StudentSearch.tsx create mode 100644 tests/unit/test_student_export_search.py diff --git a/server/state.py b/server/state.py index 73e3d0e..c624499 100644 --- a/server/state.py +++ b/server/state.py @@ -10,7 +10,7 @@ from urllib.parse import urlparse from typing import List, Optional, Union from zoneinfo import ZoneInfo -from server.import_sheet import parse_section_times +from server.import_sheet import DAYS, parse_section_times import flask from flask import Blueprint, abort, g, jsonify, render_template, request, current_app @@ -159,6 +159,47 @@ def add_student_helper(student: User, target_section: Section): # SQLAlchemy 2.0 no longer cascades new objects into the session through backrefs. db.session.add(student) + +SECTION_TYPE_ORDER = {"Lab": 0, "Discussion": 1, "Tutoring": 2} +# Every student is expected to be in one of each of these; tutoring is opt-in. +FLOATING_TYPES = {"Lab", "Discussion"} +FLOATING = "Floating" +STUDENT_SEARCH_LIMIT = 25 + + +def section_day_and_start(section: Section) -> tuple[str, str]: + """The section's weekday ("M", "Th") and start time ("08:00a") in Berkeley.""" + if not isinstance(section.start_time, (int, float)): + return "", str(section.start_time or "") + try: + dt = datetime.fromtimestamp(section.start_time, tz=ZoneInfo("America/Los_Angeles")) + except (OverflowError, OSError, ValueError): + return "", str(section.start_time) + day = DAYS[dt.weekday()] if dt.weekday() < len(DAYS) else "" + return day, dt.strftime("%I:%M%p").lower().replace("m", "") + + +def describe_section(section: Section) -> str: + """e.g. "Alice Smith, Th 08:00a, Soda 271" """ + day, start = section_day_and_start(section) + parts = [section.staff.name if section.staff else "No TA", f"{day} {start}".strip(), section.location or ""] + return ", ".join(part for part in parts if part) + + +def csv_safe(value) -> str: + """The value as a CSV cell that a spreadsheet won't run as a formula: one + that starts with =, +, -, @, a tab or a carriage return gets a leading + apostrophe (OWASP's advice for CSV injection), so it opens as text.""" + text = "" if value is None else str(value) + if text[:1] in ("=", "+", "-", "@", "\t", "\r"): + return "'" + text + return text + + +def write_csv_row(writer, row) -> None: + writer.writerow([csv_safe(cell) for cell in row]) + + def create_state_client(app: flask.Flask): # Every offering page and API lives under /offerings//. bp = Blueprint("offering", __name__, url_prefix=OFFERING_URL_PREFIX) @@ -1002,35 +1043,10 @@ def export_rosters(): for section in Section.query.filter_by(course=get_course()).all(): staff_email = section.staff.email if section.staff else "" - location = section.location or "" - day = "" - start = "" - - DAY_MAP = { - 0: "M", - 1: "T", - 2: "W", - 3: "Th", - 4: "F", - } - - try: - - if isinstance(section.start_time, (int, float)): - # Interpret stored UNIX timestamps in America/LA timezones - tz = ZoneInfo("America/Los_Angeles") - dt = datetime.fromtimestamp(section.start_time, tz=tz) - day = DAY_MAP.get(dt.weekday(), "") # e.g. "M", "Tu" - start = dt.strftime("%I:%M%p").lower().replace('m', '') # e.g. "08:00a" - else: - # fallback: output as-is (just as a string) - start = str(section.start_time or "") - except Exception: - day = "" - start = str(section.start_time or "") - + day, start = section_day_and_start(section) for student in section.students: - writer.writerow([student.email, staff_email, location, day, start, section.name or ""]) + write_csv_row(writer, [student.email, staff_email, section.location or "", day, start, + section.name or ""]) csv_text = out.getvalue() return { @@ -1038,6 +1054,75 @@ def export_rosters(): "custom": {"fileName": "rosters.csv", "rosters": csv_text}, } + @api + @admin_required + def export_students(): + """Every student in the offering, with their section of each type, or + "Floating" for a lab or discussion they aren't in.""" + course = get_course() + sections = Section.query.filter_by(course=course).all() + types = sorted({section.name for section in sections if section.name}, + key=lambda name: (SECTION_TYPE_ORDER.get(name, len(SECTION_TYPE_ORDER)), name)) + students = ( + User.query.filter(set_contains(User.student_offerings, course)) + .options(joinedload(User.sections).joinedload(Section.staff)) + .all() + ) + + out = StringIO() + writer = csv.writer(out) + writer.writerow(["Name", "Email", *types]) + for student in sorted(students, key=lambda student: (student.name.lower(), student.email.lower())): + by_type = {} + for section in student.sections_in(course): + by_type.setdefault(section.name, []).append(describe_section(section)) + write_csv_row(writer, [ + student.name, + student.email, + *("; ".join(by_type[name]) if name in by_type + else FLOATING if name in FLOATING_TYPES else "" + for name in types), + ]) + + return {"custom": {"fileName": "students.csv", "students": out.getvalue()}} + + @api + @staff_required + def search_students(query: str): + """Students in the offering whose name or email contains `query`, with + their sections here.""" + query = query.strip().lower() + if not query: + return [] + course = get_course() + students = ( + User.query.filter(set_contains(User.student_offerings, course)) + .filter(func.lower(User.name).contains(query, autoescape=True) + | func.lower(User.email).contains(query, autoescape=True)) + .options(joinedload(User.sections).joinedload(Section.staff)) + .order_by(User.name, User.email) + .limit(STUDENT_SEARCH_LIMIT) + .all() + ) + return [ + { + **student.identity_json, + "sections": [ + { + "id": str(section.id), + "name": section.name, + "startTime": section.start_time, + "endTime": section.end_time, + "location": section.location, + "staff": section.staff.identity_json if section.staff is not None else None, + } + for section in sorted(student.sections_in(course), + key=lambda section: SECTION_TYPE_ORDER.get(section.name, 99)) + ], + } + for student in students + ] + @api @staff_required def fetch_user(user_id: str): diff --git a/src/AdminPage.tsx b/src/AdminPage.tsx index 0997377..a0e3d53 100644 --- a/src/AdminPage.tsx +++ b/src/AdminPage.tsx @@ -20,8 +20,10 @@ import type { CourseConfig } from "./models"; import StateContext from "./StateContext"; import ToggleSwitch from "./ToggleSwitch"; import useAPI from "./useStateAPI"; +import useBareAPI from "./useAPI"; import AddStudentModal from "./AddStudentModal"; import ResetSectionsModal from "./ResetSectionsModal"; +import StudentSearch from "./StudentSearch"; import SyncBcoursesModal from "./SyncBcoursesModal"; import MessageContext from "./MessageContext"; @@ -161,6 +163,13 @@ export default function AdminPage(): React.ReactNode { } downloadText(rosters, fileName); }); + // Doesn't change the state, so no need to refresh it. + const exportStudents = useBareAPI( + "export_students", + useCallback(({ custom: { students, fileName } }: { custom: { students: string; fileName: string } }) => { + downloadText(students, fileName); + }, []) + ); const fetchToDrop = useAPI("fetch_to_drop", ({ custom: { students } }) => { if (students == null) { return; @@ -310,8 +319,18 @@ export default function AdminPage(): React.ReactNode { {" "} {" "} +

+

+ Export Students lists every student with their section of each type, or + “Floating” if they have no lab or discussion section. Sync bCourses first so + it includes students who’ve just joined and leaves out those who’ve dropped. +

+ + {/* Line 3: Description Text */}

diff --git a/src/StudentSearch.tsx b/src/StudentSearch.tsx new file mode 100644 index 0000000..a18e766 --- /dev/null +++ b/src/StudentSearch.tsx @@ -0,0 +1,131 @@ +import { useCallback, useContext, useRef, useState } from "react"; +import * as React from "react"; +import Button from "react-bootstrap/Button"; +import Form from "react-bootstrap/Form"; +import InputGroup from "react-bootstrap/InputGroup"; +import Table from "react-bootstrap/Table"; +import { Link } from "react-router-dom"; +import type { Person, Section } from "./models"; +import { sectionInterval } from "./models"; +import Pronouns from "./Pronouns"; +import StateContext from "./StateContext"; +import useAPI from "./useAPI"; + +type FoundSection = Pick< + Section, + "id" | "name" | "startTime" | "endTime" | "location" | "staff" +>; +type FoundStudent = Person & { sections: Array }; + +// Every student should be in one of each; tutoring is opt-in. Matches +// FLOATING_TYPES on the server. +const EXPECTED_TYPES = ["Lab", "Discussion"]; + +function SectionLink({ section }: { section: FoundSection }) { + return ( + + {section.name}: {section.staff == null ? "No TA" : section.staff.name},{" "} + {sectionInterval(section)} + {section.location ? `, ${section.location}` : null} + + ); +} + +// Find students in this offering by name or email, and see their sections. +export default function StudentSearch(): React.ReactNode { + const [query, setQuery] = useState(""); + const [results, setResults] = useState<{ + query: string; + students: Array; + } | null>(null); + + const { sections } = useContext(StateContext); + // Floating only means something for the types this course has sections of. + const expectedTypes = EXPECTED_TYPES.filter((type) => + sections.some((section) => section.name === type), + ); + + const searchedFor = useRef(""); + const showResults = useCallback( + (students: Array) => + setResults({ query: searchedFor.current, students }), + [], + ); + const searchStudents = useAPI("search_students", showResults); + + const search = (e: React.FormEvent) => { + e.preventDefault(); + const trimmed = query.trim(); + if (trimmed === "") { + setResults(null); + return; + } + searchedFor.current = trimmed; + searchStudents({ query: trimmed }); + }; + + return ( +

+
+ + Find a student by name or email: + + + setQuery(e.target.value)} + placeholder="e.g. Oski or oski@berkeley.edu" + /> + + +
+ {results != null && + (results.students.length === 0 ? ( +

+ No students match “{results.query}”. +

+ ) : ( + + + + + + + + + + {results.students.map((student) => { + const floating = expectedTypes.filter( + (type) => + !student.sections.some((section) => section.name === type), + ); + return ( + + + + + + ); + })} + +
StudentEmailSections
+ {student.name} + + {student.email} + {student.sections.map((section) => ( +
+ +
+ ))} + {floating.map((type) => ( +
{type}: Floating
+ ))} +
+ ))} +
+ ); +} diff --git a/src/models.tsx b/src/models.tsx index 8cf30e2..9d193db 100644 --- a/src/models.tsx +++ b/src/models.tsx @@ -127,7 +127,7 @@ export function getEnrollmentCount(section: Section): number { return section.enrollmentCount ?? section.students.length; } -export function nextSessionStartTime(section: Section, dayOffset: number = 0) { +export function nextSessionStartTime(section: Pick, dayOffset: number = 0) { const time = moment.unix(section.startTime).tz(TZ); while (time.isBefore(moment().add(dayOffset, "days"))) { time.add(7, "days"); @@ -145,7 +145,7 @@ export function sessionStartTimes(section: Section) { return out; } -export function sectionInterval(section: Section): React.ReactElement { +export function sectionInterval(section: Pick): React.ReactElement { const isPT = moment.tz.guess() === TZ; const useLocalString = document.cookie .split("; ") diff --git a/tests/e2e/test_admin.py b/tests/e2e/test_admin.py index 09c3b4d..01dad33 100644 --- a/tests/e2e/test_admin.py +++ b/tests/e2e/test_admin.py @@ -120,3 +120,22 @@ def test_admin_makes_uses_rotates_and_revokes_an_api_key(site): site.wait(lambda: site.has_text("No one has an API key for this course.")) status, body = call_api(site, new_key, "refresh_state") assert status == 401 + + +def test_admin_finds_a_students_sections(site): + site.visit(f"{OFFERING}/admin", TEACHER) + field = site.find('//input[@id="student-search"]') + field.send_keys("jimmy") + site.click("Search", field.find_element(By.XPATH, "..")) + site.wait(lambda: site.has_text("jimmyxu@example.com")) + row = site.find('//tr[contains(., "jimmyxu@example.com")]') + # In Yu Long's lab, and in no discussion yet. + assert "Lab: Yu Long" in row.text + assert "Soda 271" in row.text + assert "Discussion: Floating" in row.text + assert "Tutoring" not in row.text + + field.clear() + field.send_keys("nobody") + site.click("Search", field.find_element(By.XPATH, "..")) + site.wait(lambda: site.has_text("No students match “nobody”.")) diff --git a/tests/unit/test_student_export_search.py b/tests/unit/test_student_export_search.py new file mode 100644 index 0000000..128ec89 --- /dev/null +++ b/tests/unit/test_student_export_search.py @@ -0,0 +1,127 @@ +"""Exporting every student with their sections, and finding a student's sections.""" + +import csv +from io import StringIO + +from server.models import Section, User, db +from server.state import csv_safe +from tests.unit.support import CourseTestCase, lab + +# 2026-08-20 15:00 UTC is Thursday 08:00 in Berkeley. +THURSDAY_8AM = 1787238000 + + +def discussion(course, location="Disc Room"): + return Section(course=course, name="Discussion", location=location, description="", + capacity=30, can_self_enroll=True, start_time=THURSDAY_8AM, end_time=THURSDAY_8AM + 3600) + + +class StudentExportTests(CourseTestCase): + def setUp(self): + super().setUp() + self.make_staff_admin() + + def export(self): + body = self.api("export_students").get_json() + self.assertTrue(body["success"], body) + custom = body["data"]["custom"] + self.assertEqual(custom["fileName"], "students.csv") + return list(csv.reader(StringIO(custom["students"]))) + + def test_lists_each_students_sections_or_floating(self): + known = db.session.get(User, self.known_id) + staff = db.session.get(User, self.staff_id) + disc = discussion("cs61a") + disc.staff = staff + floater = User(email="floater@test", name="Floater") + floater.set_role("cs61a", is_staff=False) + known.sections.extend([db.session.get(Section, self.section_id), disc]) + db.session.add_all([disc, floater]) + db.session.commit() + + rows = self.export() + self.assertEqual(rows[0], ["Name", "Email", "Lab", "Discussion"]) + self.assertEqual(rows[1:], [ + ["Floater", "floater@test", "Floating", "Floating"], + ["Known", "known@test", "Staff, W 04:00p, Room", "Staff, Th 08:00a, Disc Room"], + ]) + + def test_leaves_staff_and_other_offerings_out(self): + self.enroll_known_in_data8() + rows = self.export() + # Only known is a cs61a student; data8's lab isn't a column of its own. + self.assertEqual(rows, [["Name", "Email", "Lab"], ["Known", "known@test", "Floating"]]) + + def test_tutoring_isnt_floating(self): + tutoring = Section(course="cs61a", name="Tutoring", location="T", description="", + capacity=5, can_self_enroll=True, start_time=0, end_time=3600) + db.session.add(tutoring) + db.session.commit() + self.assertEqual(self.export()[1], ["Known", "known@test", "Floating", ""]) + + def test_neutralizes_formulas(self): + known = db.session.get(User, self.known_id) + known.name = "=HYPERLINK(\"http://evil\",\"x\")" + db.session.commit() + self.assertEqual(self.export()[1][0], "'=HYPERLINK(\"http://evil\",\"x\")") + + + +class StudentExportPermissionTests(CourseTestCase): + def test_admins_only(self): + body = self.api("export_students").get_json() # signed in as staff, not admin + self.assertFalse(body["success"]) + self.assertIn("Only course admins", body["message"]) + + +class RosterExportFormulaTests(CourseTestCase): + def test_roster_export_neutralizes_formulas(self): + self.make_staff_admin() + section = db.session.get(Section, self.section_id) + section.location = "@SUM(A1)" + section.students.append(db.session.get(User, self.known_id)) + db.session.commit() + rosters = self.api("export_rosters").get_json()["data"]["custom"]["rosters"] + self.assertEqual(list(csv.reader(StringIO(rosters)))[1][2], "'@SUM(A1)") + + +class CsvSafeTests(CourseTestCase): + def test_csv_safe(self): + for value, expected in [("=1+1", "'=1+1"), ("+1", "'+1"), ("-1", "'-1"), ("@x", "'@x"), + ("\tx", "'\tx"), ("\rx", "'\rx"), ("a=b", "a=b"), ("", ""), (None, "")]: + self.assertEqual(csv_safe(value), expected) + + +class StudentSearchTests(CourseTestCase): + def search(self, query): + body = self.api("search_students", query=query).get_json() + self.assertTrue(body["success"], body) + return body["data"] + + def test_finds_by_name_or_email_with_sections(self): + known = db.session.get(User, self.known_id) + known.sections.append(db.session.get(Section, self.section_id)) + db.session.commit() + + for query in ("KNO", "known@", " now "): + results = self.search(query) + self.assertEqual([r["email"] for r in results], ["known@test"], query) + [section] = results[0]["sections"] + self.assertEqual((section["id"], section["name"], section["location"], section["staff"]["name"]), + (str(self.section_id), "Lab", "Room", "Staff")) + + def test_only_this_offerings_students_and_sections(self): + self.enroll_known_in_data8() + self.assertEqual(self.search("staff"), []) # staff aren't students + self.assertEqual(self.search("known")[0]["sections"], []) # their data8 lab isn't here + + def test_wildcards_are_literal(self): + self.assertEqual(self.search("%"), []) + self.assertEqual(self.search("_"), []) + self.assertEqual(self.search(""), []) + + def test_students_cant_search(self): + self.sign_in(db.session.get(User, self.known_id)) + body = self.api("search_students", query="known").get_json() + self.assertFalse(body["success"]) + self.assertIn("Only staff", body["message"]) From 31608bcd35def3dd9b7deddde09b4b1a90bc1b0e Mon Sep 17 00:00:00 2001 From: Silas Santini <70163606+pancakereport@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:54:08 -0800 Subject: [PATCH 02/12] Move roster actions to a Roster Management tab, and rename Export Rosters The imports, exports, student search, and remove and reset tools move from General to a new Roster Management tab, between General and Lab. Export Rosters becomes Export Section Assignments, with a note that its format is what Import Section Assignments reads, so it can be edited and imported back. Co-Authored-By: Claude Opus 5.5 --- docs/developers.md | 2 +- src/AdminPage.tsx | 16 +++++++++++----- tests/e2e/test_admin.py | 2 ++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/developers.md b/docs/developers.md index 3f5841a..5847777 100644 --- a/docs/developers.md +++ b/docs/developers.md @@ -21,7 +21,7 @@ The commands below are for macOS and Linux. On Windows, use [WSL](https://learn. ## Authentication -One deployment serves every bCourses course, with the same routes as seating. Users log in through Canvas OAuth (`/login/`, which returns to `/authorized/`), and `/offerings` lists their courses. Each course offering lives under `/offerings//`. Roles come from each Canvas enrollment at sign-in: Teachers and TAs are staff, and Teachers and Lead TAs are admins. Members of the `ADMIN_OVERRIDE_CANVAS_COURSE_ID` course are staff and admin everywhere. Someone added to a course after signing in needs to sign out and in again. Students are also added when an admin syncs the course with bCourses ("Sync bCourses" on the Admin page), which adds everyone on the course's bCourses roster, including students who haven't accepted their invitation, and removes students no longer on it from the course and its sections, keeping their attendance records. +One deployment serves every bCourses course, with the same routes as seating. Users log in through Canvas OAuth (`/login/`, which returns to `/authorized/`), and `/offerings` lists their courses. Each course offering lives under `/offerings//`. Roles come from each Canvas enrollment at sign-in: Teachers and TAs are staff, and Teachers and Lead TAs are admins. Members of the `ADMIN_OVERRIDE_CANVAS_COURSE_ID` course are staff and admin everywhere. Someone added to a course after signing in needs to sign out and in again. Students are also added when an admin syncs the course with bCourses ("Sync bCourses" on the Admin page's Roster Management tab), which adds everyone on the course's bCourses roster, including students who haven't accepted their invitation, and removes students no longer on it from the course and its sections, keeping their attendance records. Course staff set up Sections for a course with "Import courses from Canvas" (`/offerings/new`). Offerings moved from the monorepo store their data under an old key (`cs61a`, `data8`, ...) and have been already linked to their Canvas courses. If one ever needs relinking, run `flask --app server.main link-offering ""`, run from the repository root against that database. diff --git a/src/AdminPage.tsx b/src/AdminPage.tsx index a0e3d53..3042d06 100644 --- a/src/AdminPage.tsx +++ b/src/AdminPage.tsx @@ -298,7 +298,8 @@ export default function AdminPage(): React.ReactNode { ) : null} - + + {/* Line 1: Import Buttons */}

{" "}

- Export Students lists every student with their section of each type, or - “Floating” if they have no lab or discussion section. Sync bCourses first so - it includes students who’ve just joined and leaves out those who’ve dropped. + Export Section Assignments has one row per student per section, in the format Import Section + Assignments reads, so you can export, edit the spreadsheet, and import it back. Students in no + section aren’t in it. +

+

+ Export Students has one row per student in the offering, with their section of each type, or + “Floating” if they have no lab or discussion section. Sync bCourses first so it + includes students who’ve just joined and leaves out those who’ve dropped.

diff --git a/tests/e2e/test_admin.py b/tests/e2e/test_admin.py index 01dad33..ca1115a 100644 --- a/tests/e2e/test_admin.py +++ b/tests/e2e/test_admin.py @@ -57,6 +57,7 @@ def test_admin_syncs_students_with_bcourses(site, database): session.commit() site.visit(f"{OFFERING}/admin", TEACHER) + site.click("Roster Management", site.find('//*[@role="tablist"]')) site.click("Sync bCourses") dialog = site.find('//*[@role="dialog"]') # The fake Canvas lists three students in the course; Jimmy Xu is already here. @@ -124,6 +125,7 @@ def test_admin_makes_uses_rotates_and_revokes_an_api_key(site): def test_admin_finds_a_students_sections(site): site.visit(f"{OFFERING}/admin", TEACHER) + site.click("Roster Management", site.find('//*[@role="tablist"]')) field = site.find('//input[@id="student-search"]') field.send_keys("jimmy") site.click("Search", field.find_element(By.XPATH, "..")) From a9261baaa21caa6efc354b382573be9076af87e9 Mon Sep 17 00:00:00 2001 From: Silas Santini <70163606+pancakereport@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:56:20 -0800 Subject: [PATCH 03/12] Name export files for the course, e.g. cs61a-rosters.csv So exports from different courses don't overwrite each other in a downloads folder. Co-Authored-By: Claude Opus 5.5 --- server/state.py | 14 +++++++++++--- tests/unit/test_student_export_search.py | 22 ++++++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/server/state.py b/server/state.py index c624499..7603afa 100644 --- a/server/state.py +++ b/server/state.py @@ -196,6 +196,14 @@ def csv_safe(value) -> str: return text +def export_file_name(name: str) -> str: + """e.g. "cs61a-rosters.csv" for CS 61A: the course code, lowercased with + anything but letters and digits dropped, so exports from different courses + don't overwrite each other in a downloads folder.""" + course = re.sub(r"[^a-z0-9]", "", get_course_name().lower()) + return f"{course}-{name}" if course else name + + def write_csv_row(writer, row) -> None: writer.writerow([csv_safe(cell) for cell in row]) @@ -970,7 +978,7 @@ def export_helper(): return { **refresh_state(), "custom": { - "fileName": "attendances.json", + "fileName": export_file_name("attendances.json"), "attendances": stringify( [{"type": k, "attendances": v} for k, v in attendances.items()] ), @@ -1051,7 +1059,7 @@ def export_rosters(): csv_text = out.getvalue() return { **refresh_state(), - "custom": {"fileName": "rosters.csv", "rosters": csv_text}, + "custom": {"fileName": export_file_name("rosters.csv"), "rosters": csv_text}, } @api @@ -1084,7 +1092,7 @@ def export_students(): for name in types), ]) - return {"custom": {"fileName": "students.csv", "students": out.getvalue()}} + return {"custom": {"fileName": export_file_name("students.csv"), "students": out.getvalue()}} @api @staff_required diff --git a/tests/unit/test_student_export_search.py b/tests/unit/test_student_export_search.py index 128ec89..9136908 100644 --- a/tests/unit/test_student_export_search.py +++ b/tests/unit/test_student_export_search.py @@ -5,7 +5,7 @@ from server.models import Section, User, db from server.state import csv_safe -from tests.unit.support import CourseTestCase, lab +from tests.unit.support import DATA8, CourseTestCase, canvas_course # 2026-08-20 15:00 UTC is Thursday 08:00 in Berkeley. THURSDAY_8AM = 1787238000 @@ -25,7 +25,7 @@ def export(self): body = self.api("export_students").get_json() self.assertTrue(body["success"], body) custom = body["data"]["custom"] - self.assertEqual(custom["fileName"], "students.csv") + self.assertEqual(custom["fileName"], "cs61a-students.csv") return list(csv.reader(StringIO(custom["students"]))) def test_lists_each_students_sections_or_floating(self): @@ -66,7 +66,6 @@ def test_neutralizes_formulas(self): self.assertEqual(self.export()[1][0], "'=HYPERLINK(\"http://evil\",\"x\")") - class StudentExportPermissionTests(CourseTestCase): def test_admins_only(self): body = self.api("export_students").get_json() # signed in as staff, not admin @@ -81,10 +80,25 @@ def test_roster_export_neutralizes_formulas(self): section.location = "@SUM(A1)" section.students.append(db.session.get(User, self.known_id)) db.session.commit() - rosters = self.api("export_rosters").get_json()["data"]["custom"]["rosters"] + custom = self.api("export_rosters").get_json()["data"]["custom"] + self.assertEqual(custom["fileName"], "cs61a-rosters.csv") + rosters = custom["rosters"] self.assertEqual(list(csv.reader(StringIO(rosters)))[1][2], "'@SUM(A1)") +class ExportFileNameTests(CourseTestCase): + def test_named_for_the_course_code(self): + # DATA 8's offering has a Canvas code; CS 61A's has only a name. + self.make_staff_admin() + known = self.enroll_known_in_data8() + known.canvas_courses = {**known.canvas_courses, str(DATA8): canvas_course("DATA 8", admin=True, staff=True)} + known.set_role(str(DATA8), is_staff=True, is_admin=True) + db.session.commit() + self.sign_in(known) + body = self.api("export_attendance", canvas_course_id=DATA8).get_json() + self.assertEqual(body["data"]["custom"]["fileName"], "data8-attendances.json") + + class CsvSafeTests(CourseTestCase): def test_csv_safe(self): for value, expected in [("=1+1", "'=1+1"), ("+1", "'+1"), ("-1", "'-1"), ("@x", "'@x"), From c2e89d9f3f1959dd2edc386cb9f019926e22fd38 Mon Sep 17 00:00:00 2001 From: Silas Santini <70163606+pancakereport@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:58:17 -0800 Subject: [PATCH 04/12] Add the term to export file names, e.g. cs61a-fa26-rosters.csv So exports from different semesters of a course don't overwrite each other either. The term comes from the offering's start date, and is left out when the offering has none. Co-Authored-By: Claude Opus 5.5 --- server/state.py | 21 +++++++++++++++++---- tests/unit/test_student_export_search.py | 18 ++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/server/state.py b/server/state.py index 7603afa..ef5268c 100644 --- a/server/state.py +++ b/server/state.py @@ -196,12 +196,25 @@ def csv_safe(value) -> str: return text +def term_slug(start_at: str | None) -> str: + """e.g. "fa26" for a term starting 2026-08-19: Berkeley's spring starts in + January, summer in May or June, and fall in August. "" if unknown.""" + match = re.match(r"(\d{2})(\d{2})-(\d{2})", start_at or "") + if match is None: + return "" + month = int(match.group(3)) + season = "sp" if month <= 4 else "su" if month <= 7 else "fa" + return season + match.group(2) + + def export_file_name(name: str) -> str: - """e.g. "cs61a-rosters.csv" for CS 61A: the course code, lowercased with - anything but letters and digits dropped, so exports from different courses - don't overwrite each other in a downloads folder.""" + """e.g. "cs61a-fa26-rosters.csv" for CS 61A in Fall 2026: the course code, + lowercased with anything but letters and digits dropped, then the term, so + exports from different courses and semesters don't overwrite each other in + a downloads folder.""" course = re.sub(r"[^a-z0-9]", "", get_course_name().lower()) - return f"{course}-{name}" if course else name + prefix = "-".join(part for part in (course, term_slug(g.offering.start_at)) if part) + return f"{prefix}-{name}" if prefix else name def write_csv_row(writer, row) -> None: diff --git a/tests/unit/test_student_export_search.py b/tests/unit/test_student_export_search.py index 9136908..099fe2e 100644 --- a/tests/unit/test_student_export_search.py +++ b/tests/unit/test_student_export_search.py @@ -1,10 +1,11 @@ """Exporting every student with their sections, and finding a student's sections.""" import csv +import unittest from io import StringIO from server.models import Section, User, db -from server.state import csv_safe +from server.state import csv_safe, term_slug from tests.unit.support import DATA8, CourseTestCase, canvas_course # 2026-08-20 15:00 UTC is Thursday 08:00 in Berkeley. @@ -87,8 +88,9 @@ def test_roster_export_neutralizes_formulas(self): class ExportFileNameTests(CourseTestCase): - def test_named_for_the_course_code(self): - # DATA 8's offering has a Canvas code; CS 61A's has only a name. + def test_named_for_the_course_code_and_term(self): + # DATA 8's offering has a Canvas code and a term starting 2026-08-19; + # CS 61A's has only a name, so its files have no term. self.make_staff_admin() known = self.enroll_known_in_data8() known.canvas_courses = {**known.canvas_courses, str(DATA8): canvas_course("DATA 8", admin=True, staff=True)} @@ -96,7 +98,15 @@ def test_named_for_the_course_code(self): db.session.commit() self.sign_in(known) body = self.api("export_attendance", canvas_course_id=DATA8).get_json() - self.assertEqual(body["data"]["custom"]["fileName"], "data8-attendances.json") + self.assertEqual(body["data"]["custom"]["fileName"], "data8-fa26-attendances.json") + + +class TermSlugTests(unittest.TestCase): + def test_term_slug(self): + for start_at, expected in [("2026-01-20T08:00:00Z", "sp26"), ("2026-05-26T07:00:00Z", "su26"), + ("2026-06-22", "su26"), ("2026-08-19T07:00:00Z", "fa26"), + (None, ""), ("", ""), ("soon", "")]: + self.assertEqual(term_slug(start_at), expected, start_at) class CsvSafeTests(CourseTestCase): From aaaa017d44be3178643d74934211d4d0c16d3961 Mon Sep 17 00:00:00 2001 From: Silas Santini <70163606+pancakereport@users.noreply.github.com> Date: Thu, 12 Nov 2026 15:11:41 -0800 Subject: [PATCH 05/12] Space admin tab content from the tab strip, and rename Sync bCourses Students Only API Keys had a margin above its content; the others sat against the line under the tabs. The margin now goes on the tab strip, for every tab. "Sync bCourses" becomes "Sync bCourses Students", since it only syncs students. Co-Authored-By: Claude Opus 5.5 --- docs/developers.md | 2 +- src/AdminPage.tsx | 6 +++--- src/ApiKeysTab.tsx | 2 +- src/SyncBcoursesModal.tsx | 2 +- tests/e2e/test_admin.py | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/developers.md b/docs/developers.md index 5847777..a304028 100644 --- a/docs/developers.md +++ b/docs/developers.md @@ -21,7 +21,7 @@ The commands below are for macOS and Linux. On Windows, use [WSL](https://learn. ## Authentication -One deployment serves every bCourses course, with the same routes as seating. Users log in through Canvas OAuth (`/login/`, which returns to `/authorized/`), and `/offerings` lists their courses. Each course offering lives under `/offerings//`. Roles come from each Canvas enrollment at sign-in: Teachers and TAs are staff, and Teachers and Lead TAs are admins. Members of the `ADMIN_OVERRIDE_CANVAS_COURSE_ID` course are staff and admin everywhere. Someone added to a course after signing in needs to sign out and in again. Students are also added when an admin syncs the course with bCourses ("Sync bCourses" on the Admin page's Roster Management tab), which adds everyone on the course's bCourses roster, including students who haven't accepted their invitation, and removes students no longer on it from the course and its sections, keeping their attendance records. +One deployment serves every bCourses course, with the same routes as seating. Users log in through Canvas OAuth (`/login/`, which returns to `/authorized/`), and `/offerings` lists their courses. Each course offering lives under `/offerings//`. Roles come from each Canvas enrollment at sign-in: Teachers and TAs are staff, and Teachers and Lead TAs are admins. Members of the `ADMIN_OVERRIDE_CANVAS_COURSE_ID` course are staff and admin everywhere. Someone added to a course after signing in needs to sign out and in again. Students are also added when an admin syncs the course with bCourses ("Sync bCourses Students" on the Admin page's Roster Management tab), which adds everyone on the course's bCourses roster, including students who haven't accepted their invitation, and removes students no longer on it from the course and its sections, keeping their attendance records. Course staff set up Sections for a course with "Import courses from Canvas" (`/offerings/new`). Offerings moved from the monorepo store their data under an old key (`cs61a`, `data8`, ...) and have been already linked to their Canvas courses. If one ever needs relinking, run `flask --app server.main link-offering ""`, run from the repository root against that database. diff --git a/src/AdminPage.tsx b/src/AdminPage.tsx index 3042d06..1ad7c22 100644 --- a/src/AdminPage.tsx +++ b/src/AdminPage.tsx @@ -246,7 +246,7 @@ export default function AdminPage(): React.ReactNode {

Admin Settings

- +
Welcome message: @@ -309,7 +309,7 @@ export default function AdminPage(): React.ReactNode { Import Section Assignments {" "}

@@ -332,7 +332,7 @@ export default function AdminPage(): React.ReactNode {

Export Students has one row per student in the offering, with their section of each type, or - “Floating” if they have no lab or discussion section. Sync bCourses first so it + “Floating” if they have no lab or discussion section. Sync bCourses Students first so it includes students who’ve just joined and leaves out those who’ve dropped.

diff --git a/src/ApiKeysTab.tsx b/src/ApiKeysTab.tsx index 7903751..a31967b 100644 --- a/src/ApiKeysTab.tsx +++ b/src/ApiKeysTab.tsx @@ -52,7 +52,7 @@ export default function ApiKeysTab() { }; return ( -
+

An API key lets a script call this course’s Sections API as you, or as anyone in this course. It works only in this course, only while you’re an admin here in bCourses, and diff --git a/src/SyncBcoursesModal.tsx b/src/SyncBcoursesModal.tsx index 9b5fc17..24e1700 100644 --- a/src/SyncBcoursesModal.tsx +++ b/src/SyncBcoursesModal.tsx @@ -79,7 +79,7 @@ export default function SyncBcoursesModal({ show, onClose }: Props) { return ( - Sync bCourses + Sync bCourses Students

diff --git a/tests/e2e/test_admin.py b/tests/e2e/test_admin.py index ca1115a..8ffa97f 100644 --- a/tests/e2e/test_admin.py +++ b/tests/e2e/test_admin.py @@ -58,7 +58,7 @@ def test_admin_syncs_students_with_bcourses(site, database): site.visit(f"{OFFERING}/admin", TEACHER) site.click("Roster Management", site.find('//*[@role="tablist"]')) - site.click("Sync bCourses") + site.click("Sync bCourses Students") dialog = site.find('//*[@role="dialog"]') # The fake Canvas lists three students in the course; Jimmy Xu is already here. site.wait(lambda: "This will add 2 and remove 1 students." in dialog.text) From a0df71085f0696186b51b473b21b89d686b1f8cb Mon Sep 17 00:00:00 2001 From: Silas Santini <70163606+pancakereport@users.noreply.github.com> Date: Thu, 12 Nov 2026 15:25:22 -0800 Subject: [PATCH 06/12] Lay out Roster Management in the order staff set up a semester The tab opens with the setup steps (sync bCourses students, import sections, import section assignments) and says they're safe to re-run, with Sync bCourses Students now the first button. Then the exports and their notes, student search, the tutoring tools with their note below them, and the remove and reset tools last. Co-Authored-By: Claude Opus 5.5 --- src/AdminPage.tsx | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/AdminPage.tsx b/src/AdminPage.tsx index 1ad7c22..6db8337 100644 --- a/src/AdminPage.tsx +++ b/src/AdminPage.tsx @@ -300,20 +300,29 @@ export default function AdminPage(): React.ReactNode {

- {/* Line 1: Import Buttons */} +

At the start of the semester, start by:

+
    +
  1. syncing the bCourses student roster,
  2. +
  3. importing sections,
  4. +
  5. importing section assignments.
  6. +

+ It is safe to run any of these steps again. For example, if you want to drop students from sections + who have dropped your course, re-run the bCourses sync. You can also add more sections after your + initial setup, and assign students who added the course late. +

+

+ {" "} {" "} {" "} -

- {/* Line 2: Export Buttons */}

+ +

- Export Section Assignments has one row per student per section, in the format Import Section - Assignments reads, so you can export, edit the spreadsheet, and import it back. Students in no - section aren’t in it. -

-

- Export Students has one row per student in the offering, with their section of each type, or - “Floating” if they have no lab or discussion section. Sync bCourses Students first so it - includes students who’ve just joined and leaves out those who’ve dropped. + “Export Section Assignments” has one row per student per section, in the format + “Import Section Assignments” reads, so you can export, edit the spreadsheet, and import it + back. Students in no section aren’t in it. “Export Students” has one row per + student in the offering, with their section of each type, or “Floating” if they have no + lab or discussion section. Run “Sync bCourses Students” first so it includes students + who’ve just joined and leaves out those who’ve dropped.

- -

-

- “Export Section Assignments” has one row per student per section, in the format - “Import Section Assignments” reads, so you can export, edit the spreadsheet, and import it - back. Students in no section aren’t in it. “Export Students” has one row per - student in the offering, with their section of each type, or “Floating” if they have no - lab or discussion section. Run “Sync bCourses Students” first so it includes students - who’ve just joined and leaves out those who’ve dropped. -

+
    +
  • + “Export Section Assignments” (CSV) has one row per student per section, in the format + “Import Section Assignments” reads, so you can export, edit the spreadsheet, and import + it back. Students in no section aren’t in it. +
  • +
  • + “Export Full Attendances” (JSON) has every attendance record, by section type and + student. All students who have had attendance recorded are included in this export, even if the + student has been removed from a section or this course offering. +
  • +
  • + “Export Students” (CSV) has one row per student in the offering, with their section of + each type, or “Floating” if they have no lab or discussion section. Run “Sync + bCourses Students” first so it includes students who’ve just joined and leaves out + those who’ve dropped. +
  • +

- + + setMessage(e.target.value)} + /> +

Preview of welcome message: From 94a0b9f70f0c1268d736f2bd398f5a5e22b5079e Mon Sep 17 00:00:00 2001 From: Silas Santini <70163606+pancakereport@users.noreply.github.com> Date: Thu, 12 Nov 2026 15:53:35 -0800 Subject: [PATCH 10/12] Keep the welcome message's Save button beside its box The previous commit also moved it below the box and added a label, which wasn't wanted. Only the taller box stays. Co-Authored-By: Claude Opus 5.5 --- src/AdminPage.tsx | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/AdminPage.tsx b/src/AdminPage.tsx index c7b1382..d58a947 100644 --- a/src/AdminPage.tsx +++ b/src/AdminPage.tsx @@ -249,18 +249,19 @@ export default function AdminPage(): React.ReactNode {
- - setMessage(e.target.value)} - /> - + Welcome message: + + setMessage(e.target.value)} + /> + +
Preview of welcome message: From b88b88478734b08296feb11d2ab983cea1edc513 Mon Sep 17 00:00:00 2001 From: Silas Santini <70163606+pancakereport@users.noreply.github.com> Date: Thu, 12 Nov 2026 15:59:12 -0800 Subject: [PATCH 11/12] Make the welcome message box 4 lines tall instead of 8 Co-Authored-By: Claude Opus 5.5 --- src/AdminPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AdminPage.tsx b/src/AdminPage.tsx index d58a947..55175e0 100644 --- a/src/AdminPage.tsx +++ b/src/AdminPage.tsx @@ -253,7 +253,7 @@ export default function AdminPage(): React.ReactNode { setMessage(e.target.value)} From f0f0e600edba02a49dedc928f821ad31083ef5e3 Mon Sep 17 00:00:00 2001 From: Silas Santini <70163606+pancakereport@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:03:17 -0800 Subject: [PATCH 12/12] wording --- src/AdminPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AdminPage.tsx b/src/AdminPage.tsx index 55175e0..a234295 100644 --- a/src/AdminPage.tsx +++ b/src/AdminPage.tsx @@ -341,7 +341,7 @@ export default function AdminPage(): React.ReactNode {
  • “Export Section Assignments” (CSV) has one row per student per section, in the format “Import Section Assignments” reads, so you can export, edit the spreadsheet, and import - it back. Students in no section aren’t in it. + it back. Students with no section assignments aren’t in it.
  • “Export Full Attendances” (JSON) has every attendance record, by section type and