feat: implement Phase 3 participant self-management (stories 4.5, 6.1)

Implemented features:
- Story 4.5: View Participant List - participants can see other active participants
- Story 6.1: Update Profile - participants can edit name and gift ideas before matching
- Utility functions for state management and business logic
- Comprehensive unit and integration tests

New files:
- src/utils/participant.py - Business logic utilities
- src/templates/participant/profile_edit.html - Profile edit form
- tests/unit/test_participant_utils.py - Unit tests for utilities
- tests/integration/test_participant_list.py - Integration tests for participant list
- tests/integration/test_profile_update.py - Integration tests for profile updates

Modified files:
- src/routes/participant.py - Added dashboard participant list and profile edit route
- src/templates/participant/dashboard.html - Added participant list section and edit link
- src/forms/participant.py - Added ProfileUpdateForm
- src/app.py - Added participant.profile_edit to setup check exemptions
- tests/conftest.py - Added exchange_factory, participant_factory, auth_participant fixtures

All tests passing. Phase 3.3 (reminder preferences) and 3.4 (withdrawal) remain to be implemented.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-22 20:05:28 -07:00
parent 75378ac769
commit a7902aa623
10 changed files with 709 additions and 1 deletions
+1
View File
@@ -144,6 +144,7 @@ def register_setup_check(app: Flask) -> None:
"participant.magic_login",
"participant.dashboard",
"participant.logout",
"participant.profile_edit",
]:
return
+22
View File
@@ -48,3 +48,25 @@ class MagicLinkRequestForm(FlaskForm):
Length(max=255, message="Email must be less than 255 characters"),
],
)
class ProfileUpdateForm(FlaskForm):
"""Form for updating participant profile."""
name = StringField(
"Name",
validators=[
DataRequired(message="Name is required"),
Length(min=1, max=255, message="Name must be 1-255 characters"),
],
description="Your display name (visible to other participants)",
)
gift_ideas = TextAreaField(
"Gift Ideas",
validators=[
Length(max=10000, message="Gift ideas must be less than 10,000 characters")
],
description="Optional wishlist or gift preferences for your Secret Santa",
render_kw={"rows": 6, "maxlength": 10000},
)
+73 -1
View File
@@ -17,7 +17,11 @@ from flask import (
from src.app import db
from src.decorators.auth import participant_required
from src.forms.participant import MagicLinkRequestForm, ParticipantRegistrationForm
from src.forms.participant import (
MagicLinkRequestForm,
ParticipantRegistrationForm,
ProfileUpdateForm,
)
from src.models.exchange import Exchange
from src.models.magic_token import MagicToken
from src.models.participant import Participant
@@ -341,10 +345,78 @@ def dashboard(id: int): # noqa: A002
if not participant:
abort(404)
# Get list of active participants
from src.utils.participant import can_update_profile, get_active_participants
participants = get_active_participants(exchange.id)
can_edit = can_update_profile(participant)
return render_template(
"participant/dashboard.html",
exchange=exchange,
participant=participant,
participants=participants,
participant_count=len(participants),
can_edit_profile=can_edit,
)
@participant_bp.route("/participant/profile/edit", methods=["GET", "POST"])
@participant_required
def profile_edit():
"""Edit participant profile (name and gift ideas).
Only allowed before matching occurs.
Returns:
GET: Profile edit form
POST: Redirect to dashboard on success, or re-render form on error
"""
from flask import current_app
# Get participant from session
participant = db.session.query(Participant).filter_by(id=session["user_id"]).first()
if not participant:
abort(404)
# Check if profile editing is allowed
from src.utils.participant import can_update_profile
if not can_update_profile(participant):
flash(
"Your profile is locked after matching. Contact the admin for changes.",
"error",
)
return redirect(url_for("participant.dashboard", id=participant.exchange_id))
# Create form with current values
form = ProfileUpdateForm(obj=participant)
if form.validate_on_submit():
try:
# Update participant
participant.name = form.name.data.strip()
participant.gift_ideas = (
form.gift_ideas.data.strip() if form.gift_ideas.data else None
)
db.session.commit()
flash("Your profile has been updated successfully.", "success")
return redirect(
url_for("participant.dashboard", id=participant.exchange_id)
)
except Exception as e:
db.session.rollback()
current_app.logger.error(f"Failed to update participant profile: {e}")
flash("Failed to update profile. Please try again.", "error")
return render_template(
"participant/profile_edit.html",
form=form,
participant=participant,
exchange=participant.exchange,
)
+22
View File
@@ -37,6 +37,28 @@
<dd>{{ participant.gift_ideas }}</dd>
{% endif %}
</dl>
{% if can_edit_profile %}
<a href="{{ url_for('participant.profile_edit') }}">Edit Profile</a>
{% endif %}
</section>
<section>
<h2>Participants ({{ participant_count }})</h2>
{% if participants %}
<ul>
{% for p in participants %}
<li>
{{ p.name }}
{% if p.id == participant.id %}
<span class="badge">You</span>
{% endif %}
</li>
{% endfor %}
</ul>
{% else %}
<p>No other participants yet. Share the registration link!</p>
{% endif %}
</section>
<section>
@@ -0,0 +1,64 @@
{% extends "layouts/base.html" %}
{% block title %}Edit Profile - {{ exchange.name }}{% endblock %}
{% block content %}
<article>
<header>
<h1>Edit Your Profile</h1>
<p>Update your display name and gift ideas. Your Secret Santa will see this information after matching.</p>
</header>
<form method="POST" action="{{ url_for('participant.profile_edit') }}">
{{ form.hidden_tag() }}
<div>
{{ form.name.label }}
{{ form.name(class="form-control") }}
{% if form.name.errors %}
<ul class="field-errors">
{% for error in form.name.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<small>{{ form.name.description }}</small>
</div>
<div>
{{ form.gift_ideas.label }}
{{ form.gift_ideas(class="form-control") }}
{% if form.gift_ideas.errors %}
<ul class="field-errors">
{% for error in form.gift_ideas.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<small>{{ form.gift_ideas.description }}</small>
<small id="gift-ideas-count">
{{ (form.gift_ideas.data or '')|length }} / 10,000 characters
</small>
</div>
<div>
<button type="submit">Save Changes</button>
<a href="{{ url_for('participant.dashboard', id=exchange.id) }}">Cancel</a>
</div>
</form>
</article>
<script>
// Character counter (progressive enhancement)
document.addEventListener('DOMContentLoaded', function() {
const textarea = document.querySelector('textarea[name="gift_ideas"]');
const counter = document.getElementById('gift-ideas-count');
if (textarea && counter) {
textarea.addEventListener('input', function() {
counter.textContent = this.value.length + ' / 10,000 characters';
});
}
});
</script>
{% endblock %}
+55
View File
@@ -0,0 +1,55 @@
"""Participant business logic utilities."""
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from src.models import Participant
def get_active_participants(exchange_id: int) -> list["Participant"]:
"""Get all active (non-withdrawn) participants for an exchange.
Args:
exchange_id: ID of the exchange
Returns:
List of active participants, ordered by name
"""
from src.models.participant import Participant
result: list[Participant] = (
Participant.query.filter(
Participant.exchange_id == exchange_id, Participant.withdrawn_at.is_(None)
)
.order_by(Participant.name)
.all()
)
return result
def is_withdrawn(participant: "Participant") -> bool:
"""Check if participant has withdrawn.
Args:
participant: The participant to check
Returns:
True if withdrawn, False otherwise
"""
return participant.withdrawn_at is not None
def can_update_profile(participant: "Participant") -> bool:
"""Check if participant can update their profile.
Profile updates are allowed until matching occurs.
Args:
participant: The participant to check
Returns:
True if profile updates are allowed, False otherwise
"""
exchange = participant.exchange
allowed_states = ["draft", "registration_open", "registration_closed"]
return exchange.state in allowed_states