Repository navigation
Improve student activity registration system - #2
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends the Mergington High School activities app to support unregistering participants, improves the activities UI to show/manage participant lists, and adds an automated API test suite to cover the core flows.
Changes:
- Added a
DELETE /activities/{activity_name}/participants?email=...API to unregister students, and prevented duplicate signups. - Updated the frontend to render per-activity participant lists with a remove button and centralized message display logic.
- Added pytest coverage for listing, signup, duplicate signup prevention, unregister, and error cases; updated styling and dependencies.
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
src/app.py |
Adds new activities, blocks duplicate signups, and introduces the unregister endpoint. |
src/static/app.js |
Renders participant lists, wires up “Remove” (DELETE) calls, refreshes the UI, and centralizes message handling. |
src/static/styles.css |
Adds participant list and delete button styling; updates activity card styling. |
tests/test_app.py |
Introduces API-level tests for redirect, listing, signup, duplicates, unregister, and not-found cases. |
src/README.md |
Documents the new DELETE endpoint. |
requirements.txt |
Adds pytest and fixes formatting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @app.post("/activities/{activity_name}/signup") | ||
| def signup_for_activity(activity_name: str, email: str): | ||
| """Sign up a student for an activity""" | ||
| # Validate activity exists | ||
| if activity_name not in activities: | ||
| raise HTTPException(status_code=404, detail="Activity not found") | ||
|
|
||
| # Get the specific activity | ||
| activity = activities[activity_name] | ||
|
|
||
| # Validate student is not already signed up | ||
| if email in activity["participants"]: | ||
| raise HTTPException(status_code=400, detail="Student already signed up for this activity") | ||
|
|
||
| # Add student | ||
| activity["participants"].append(email) | ||
| return {"message": f"Signed up {email} for {activity_name}"} | ||
|
|
||
|
|
||
| @app.delete("/activities/{activity_name}/participants") | ||
| def unregister_from_activity(activity_name: str, email: str): | ||
| """Remove a student from an activity""" | ||
| if activity_name not in activities: | ||
| raise HTTPException(status_code=404, detail="Activity not found") | ||
|
|
||
| activity = activities[activity_name] | ||
|
|
||
| if email not in activity["participants"]: | ||
| raise HTTPException(status_code=404, detail="Student is not signed up for this activity") | ||
|
|
||
| activity["participants"].remove(email) | ||
| return {"message": f"Removed {email} from {activity_name}"} |
There was a problem hiding this comment.
The email query parameter is accepted as a free-form str for both signup and unregister. Since it’s later rendered in the UI, it’s safer to validate/normalize it at the API boundary (e.g., EmailStr, strip(), and consistent casing) to reduce the chance of storing unexpected values and to provide consistent duplicate detection/unregister behavior.
| const participantItems = details.participants.length | ||
| ? details.participants | ||
| .map( | ||
| (participant) => ` | ||
| <li class="participant-item"> | ||
| <span class="participant-email">${participant}</span> | ||
| <button | ||
| type="button" | ||
| class="participant-delete" | ||
| data-activity="${name}" | ||
| data-email="${participant}" | ||
| aria-label="Remove ${participant} from ${name}" | ||
| title="Unregister participant" | ||
| > | ||
| <span aria-hidden="true">×</span> | ||
| </button> | ||
| </li> | ||
| ` | ||
| ) | ||
| .join("") | ||
| : '<li class="participants-empty">No one has signed up yet.</li>'; | ||
|
|
||
| activityCard.innerHTML = ` | ||
| <h4>${name}</h4> | ||
| <p>${details.description}</p> | ||
| <p><strong>Schedule:</strong> ${details.schedule}</p> | ||
| <p><strong>Availability:</strong> ${spotsLeft} spots left</p> | ||
| <div class="participants-section"> | ||
| <p class="participants-title">Current Participants</p> | ||
| <ul class="participants-list">${participantItems}</ul> | ||
| </div> | ||
| `; | ||
|
|
There was a problem hiding this comment.
Participant emails are inserted into the DOM via template literals assigned to innerHTML (both text nodes and data-* attributes). Since email is user-controlled (signup query param), this enables XSS/HTML injection and can also break the data-email attribute if the value contains quotes. Prefer building the participant list with DOM APIs (createElement, textContent, dataset) or escaping/sanitizing values before interpolating into HTML.
| const participantItems = details.participants.length | |
| ? details.participants | |
| .map( | |
| (participant) => ` | |
| <li class="participant-item"> | |
| <span class="participant-email">${participant}</span> | |
| <button | |
| type="button" | |
| class="participant-delete" | |
| data-activity="${name}" | |
| data-email="${participant}" | |
| aria-label="Remove ${participant} from ${name}" | |
| title="Unregister participant" | |
| > | |
| <span aria-hidden="true">×</span> | |
| </button> | |
| </li> | |
| ` | |
| ) | |
| .join("") | |
| : '<li class="participants-empty">No one has signed up yet.</li>'; | |
| activityCard.innerHTML = ` | |
| <h4>${name}</h4> | |
| <p>${details.description}</p> | |
| <p><strong>Schedule:</strong> ${details.schedule}</p> | |
| <p><strong>Availability:</strong> ${spotsLeft} spots left</p> | |
| <div class="participants-section"> | |
| <p class="participants-title">Current Participants</p> | |
| <ul class="participants-list">${participantItems}</ul> | |
| </div> | |
| `; | |
| // Build static structure for the activity card without interpolating untrusted data into HTML | |
| activityCard.innerHTML = ` | |
| <h4 class="activity-name"></h4> | |
| <p class="activity-description"></p> | |
| <p class="activity-schedule"><strong>Schedule:</strong> <span class="activity-schedule-value"></span></p> | |
| <p class="activity-availability"><strong>Availability:</strong> <span class="activity-spots-left"></span> spots left</p> | |
| <div class="participants-section"> | |
| <p class="participants-title">Current Participants</p> | |
| <ul class="participants-list"></ul> | |
| </div> | |
| `; | |
| // Populate static fields using textContent to avoid HTML injection | |
| const nameEl = activityCard.querySelector(".activity-name"); | |
| const descriptionEl = activityCard.querySelector(".activity-description"); | |
| const scheduleValueEl = activityCard.querySelector(".activity-schedule-value"); | |
| const spotsLeftEl = activityCard.querySelector(".activity-spots-left"); | |
| const participantsListEl = activityCard.querySelector(".participants-list"); | |
| if (nameEl) { | |
| nameEl.textContent = name; | |
| } | |
| if (descriptionEl) { | |
| descriptionEl.textContent = details.description; | |
| } | |
| if (scheduleValueEl) { | |
| scheduleValueEl.textContent = details.schedule; | |
| } | |
| if (spotsLeftEl) { | |
| spotsLeftEl.textContent = String(spotsLeft); | |
| } | |
| // Populate participants list safely using DOM APIs | |
| if (details.participants.length && participantsListEl) { | |
| details.participants.forEach((participant) => { | |
| const li = document.createElement("li"); | |
| li.className = "participant-item"; | |
| const emailSpan = document.createElement("span"); | |
| emailSpan.className = "participant-email"; | |
| emailSpan.textContent = participant; | |
| const deleteButton = document.createElement("button"); | |
| deleteButton.type = "button"; | |
| deleteButton.className = "participant-delete"; | |
| deleteButton.dataset.activity = name; | |
| deleteButton.dataset.email = participant; | |
| deleteButton.setAttribute("aria-label", `Remove ${participant} from ${name}`); | |
| deleteButton.title = "Unregister participant"; | |
| const innerSpan = document.createElement("span"); | |
| innerSpan.setAttribute("aria-hidden", "true"); | |
| innerSpan.textContent = "×"; | |
| deleteButton.appendChild(innerSpan); | |
| li.appendChild(emailSpan); | |
| li.appendChild(deleteButton); | |
| participantsListEl.appendChild(li); | |
| }); | |
| } else if (participantsListEl) { | |
| const emptyLi = document.createElement("li"); | |
| emptyLi.className = "participants-empty"; | |
| emptyLi.textContent = "No one has signed up yet."; | |
| participantsListEl.appendChild(emptyLi); | |
| } |
|
|
||
| function showMessage(message, type) { | ||
| messageDiv.textContent = message; | ||
| messageDiv.className = type; |
There was a problem hiding this comment.
showMessage() assigns messageDiv.className = type, which overwrites any existing/base classes on the message element (e.g., a shared "message" class for padding/margins) and can make styling brittle. Consider using classList to toggle status classes while preserving a stable base class.
| messageDiv.className = type; | |
| // Remove any previous status classes while preserving base classes | |
| messageDiv.classList.remove("error", "success"); | |
| if (type) { | |
| messageDiv.classList.add(type); | |
| } |
|
|
||
| function showMessage(message, type) { | ||
| messageDiv.textContent = message; | ||
| messageDiv.className = type; | ||
| messageDiv.classList.remove("hidden"); | ||
|
|
||
| setTimeout(() => { | ||
| messageDiv.classList.add("hidden"); |
There was a problem hiding this comment.
showMessage() creates a new setTimeout each time it’s called without clearing any prior timer. If multiple messages are shown quickly, an earlier timeout can hide the latest message prematurely. Track the timeout id and clearTimeout() before scheduling a new hide.
| function showMessage(message, type) { | |
| messageDiv.textContent = message; | |
| messageDiv.className = type; | |
| messageDiv.classList.remove("hidden"); | |
| setTimeout(() => { | |
| messageDiv.classList.add("hidden"); | |
| let messageTimeoutId; | |
| function showMessage(message, type) { | |
| messageDiv.textContent = message; | |
| messageDiv.className = type; | |
| messageDiv.classList.remove("hidden"); | |
| if (messageTimeoutId) { | |
| clearTimeout(messageTimeoutId); | |
| } | |
| messageTimeoutId = setTimeout(() => { | |
| messageDiv.classList.add("hidden"); | |
| messageTimeoutId = undefined; |
This pull request adds the ability for students to unregister from activities, improves the user interface for managing participants, and introduces automated tests to ensure the correctness of the core features. It also expands the list of available activities and enhances the overall user experience with better feedback and styling.
Backend API enhancements:
DELETEendpoint (/activities/{activity_name}/participants?email=...) to allow students to unregister from an activity, with appropriate error handling for missing activities or participants. [1] [2]Frontend improvements:
Styling and UI enhancements:
Testing:
tests/test_app.py) covering activity listing, signup, duplicate signup prevention, unregistration, and error cases, ensuring API reliability and correctness.Data model update: