Files
RosterChief/events/models.py
Bernard Siebens 98b8002a04 Add billing-ending banner, events CRUD, RBIHF import, public API, team photos, and sponsors
A large batch of club-management features built up over one session:

- Club dashboard banner warning admins 1 month before billing ends
- Full Events/EventSeries CRUD (recurrence builder, occurrence lifecycle,
  per-team permissions), with match->game rename and game-specific fields
  (score, competition, live status, external game ID)
- Django-admin competition dropdown, gated per-club by feature flag
- Auto-import of RBIHF fixtures (scrape -> diff -> preview -> confirm),
  with location/opponent dropdowns suggested from existing club data
- Feature-flag-gated Shop/Forms nav sections, reusing the same flag
  machinery for the RBIHF import button
- Team roster now scoped to members active this season or next, sorted and
  grouped by position
- Club sport type (ice hockey / other), shown in the control panel's club
  subtitle
- Per-season team photo upload from the team page
- New public read-only API (Django Ninja) at /api/v1/: news, team rosters,
  upcoming/live/per-team games, and sponsors -- auto-documented via Swagger
  UI, CORS-enabled for a club's own external website
- Club sponsors: admin-only CRUD (logo, URL, active date window) plus a
  date-windowed, optionally randomized API endpoint
- Assorted fixes: NullBooleanField dropdown rendering, cross-club event
  validation timing, searchable-select chip placement, btn-neutral ->
  default button style sweep, calendar-month chart windows

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01R1gj3J1QPfP38XWpnpbFpy
2026-08-06 17:36:04 +02:00

204 lines
10 KiB
Python

from django.conf import settings
from django.db import models
from django.db.models import Q
from django.utils.translation import gettext_lazy as _
from django_countries.fields import CountryField
from club.models import Club, Season
from members.models import Member
from rosterchief.base import ClubScopedModel, UUIDModel, validate_club_scope
from teams.models import Team
class Opponent(ClubScopedModel):
name = models.CharField(_("name"), max_length=255)
logo = models.ImageField(_("logo"), upload_to="opponents", blank=True)
class Meta:
verbose_name = _("opponent")
verbose_name_plural = _("opponents")
ordering = ["name"]
def __str__(self):
return self.name
class Location(ClubScopedModel):
name = models.CharField(_("name"), max_length=255)
address = models.CharField(_("address"), max_length=255)
city = models.CharField(_("city"), max_length=255)
zip_code = models.CharField(_("zip code"), max_length=255)
country = CountryField(_("country"))
is_home = models.BooleanField(
_("home location"),
default=False,
help_text=_("The club's own ground, set from the control panel -- lets an event's location tell a home game from an away one."),
)
class Meta:
verbose_name = _("location")
verbose_name_plural = _("locations")
ordering = ["name"]
constraints = [
models.UniqueConstraint(fields=["club"], condition=Q(is_home=True), name="unique_home_location_per_club"),
]
def __str__(self):
return self.name
class Event(ClubScopedModel):
class EventKind(models.TextChoices):
TRAINING = "training", _("Training")
GAME = "game", _("Game")
TOURNAMENT = "tournament", _("Tournament")
MEETING = "meeting", _("Meeting")
SOCIAL = "social", _("Social")
OTHER = "other", _("Other")
series = models.ForeignKey("EventSeries", on_delete=models.CASCADE, related_name="occurrences", null=True, blank=True, verbose_name=_("series"), help_text=_("The recurring series this occurrence belongs to; blank for one-off events."))
detached = models.BooleanField(_("detached"), default=False, help_text=_("Edited independently; excluded from series-wide updates and regeneration."))
cancelled = models.BooleanField(_("cancelled"), default=False)
teams = models.ManyToManyField(Team, related_name="scheduled_events", blank=True, verbose_name=_("teams"))
invited_members = models.ManyToManyField(Member, related_name="invited_to_events", blank=True, verbose_name=_("invited members"))
excluded_members = models.ManyToManyField(Member, related_name="excluded_from_events", blank=True, verbose_name=_("excluded members"))
season = models.ForeignKey(Season, on_delete=models.SET_NULL, related_name="events", null=True, blank=True, verbose_name=_("season"), help_text=_("Season whose team rosters define the audience; derived from the start date when left blank."))
kind = models.CharField(_("kind"), max_length=10, choices=EventKind.choices, default=EventKind.OTHER)
title = models.CharField(_("title"), max_length=255)
start = models.DateTimeField(_("start"))
end = models.DateTimeField(_("end"), blank=True, null=True)
gathering = models.DateTimeField(_("gathering"), blank=True, null=True)
deadline = models.DateTimeField(_("registration deadline"), blank=True, null=True)
location = models.ForeignKey(Location, on_delete=models.SET_NULL, related_name="events", null=True, blank=True, verbose_name=_("location"))
opponent = models.ForeignKey(Opponent, on_delete=models.SET_NULL, related_name="events", null=True, blank=True, verbose_name=_("opponent"))
created_by = models.ForeignKey(Member, on_delete=models.SET_NULL, related_name="created_events", null=True, blank=True, verbose_name=_("created by"))
# Game-specific -- meaningless for other kinds, so all optional. external_game_id
# is this game's id in an external competition/fixture data source, for a later
# automatic score-fetcher to key off; nothing populates it yet.
competition = models.CharField(_("competition"), max_length=255, blank=True, help_text=_("The league, cup or competition this game is part of."))
external_game_id = models.CharField(_("external game ID"), max_length=255, blank=True, help_text=_("This game's id in an external competition data source, for automatic score fetching later."))
score_for = models.PositiveSmallIntegerField(_("score (us)"), null=True, blank=True)
score_against = models.PositiveSmallIntegerField(_("score (opponent)"), null=True, blank=True)
is_live = models.BooleanField(_("live"), default=False, help_text=_("The game is currently in progress."))
class Meta:
verbose_name = _("event")
verbose_name_plural = _("events")
ordering = ["-start"]
def __str__(self):
return self.title
def clean(self):
validate_club_scope(self, self.club_id, same_club_fields=("season", "location", "opponent"))
@property
def is_home_game(self) -> bool:
"""Whether this game is being played at the club's own ground
(Location.is_home) -- False for anything that isn't a game, or a game
with no location set, or one at an away/neutral location."""
return self.kind == self.EventKind.GAME and self.location_id is not None and self.location.is_home
class EventSeries(ClubScopedModel):
"""A recurring event definition that materialises concrete Event rows."""
rrule = models.CharField(_("recurrence rule"), max_length=255, help_text=_("RFC 5545 RRULE, e.g. FREQ=WEEKLY;BYDAY=MO,WE."))
dtstart = models.DateTimeField(_("first occurrence"))
until = models.DateTimeField(_("until"), null=True, blank=True, help_text=_("Series end: no occurrences are generated after this. Leave blank for open-ended (bounded by the rule's own COUNT/UNTIL, if any)."))
duration = models.DurationField(_("duration"), null=True, blank=True, help_text=_("Length of each occurrence; sets each event's end."))
gathering_offset = models.DurationField(_("gathering offset"), null=True, blank=True, help_text=_("How long before the start each occurrence's gathering time is."))
deadline_offset = models.DurationField(_("registration deadline offset"), null=True, blank=True, help_text=_("How long before the start each occurrence's registration deadline is."))
excluded_dates = models.JSONField(_("excluded dates"), default=list, blank=True, help_text=_("ISO start datetimes of occurrences removed from the series (EXDATEs)."))
generated_until = models.DateTimeField(_("generated until"), null=True, blank=True, help_text=_("Occurrences have been materialised up to this point."))
# Template copied onto each generated occurrence.
kind = models.CharField(_("kind"), max_length=10, choices=Event.EventKind.choices, default=Event.EventKind.OTHER)
title = models.CharField(_("title"), max_length=255)
location = models.ForeignKey(Location, on_delete=models.SET_NULL, related_name="event_series", null=True, blank=True, verbose_name=_("location"))
opponent = models.ForeignKey(Opponent, on_delete=models.SET_NULL, related_name="event_series", null=True, blank=True, verbose_name=_("opponent"))
teams = models.ManyToManyField(Team, related_name="event_series", blank=True, verbose_name=_("teams"))
invited_members = models.ManyToManyField(Member, related_name="invited_to_event_series", blank=True, verbose_name=_("invited members"))
excluded_members = models.ManyToManyField(Member, related_name="excluded_from_event_series", blank=True, verbose_name=_("excluded members"))
class Meta:
verbose_name = _("event series")
verbose_name_plural = _("event series")
ordering = ["title"]
def __str__(self):
return self.title
def clean(self):
validate_club_scope(self, self.club_id, same_club_fields=("location", "opponent"))
class Attendance(UUIDModel):
class AttendanceStatus(models.TextChoices):
PRESENT = "present", _("Present")
ABSENT = "absent", _("Absent")
EXCUSED = "excused", _("Excused")
SELECTED = "selected", _("Selected")
NOT_SELECTED = "not_selected", _("Not selected")
MAYBE = "maybe", _("Maybe")
NO_RESPONSE = "no_response", _("No response")
event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name="attendances", verbose_name=_("event"))
member = models.ForeignKey(Member, on_delete=models.CASCADE, related_name="attendances", verbose_name=_("member"))
status = models.CharField(_("status"), max_length=20, choices=AttendanceStatus.choices, default=AttendanceStatus.NO_RESPONSE)
showed_up = models.BooleanField(
_("showed up"),
null=True,
blank=True,
default=None,
help_text=_("Recorded by a check-in, separate from the RSVP status above. Blank means no check-in has been recorded yet."),
)
note = models.TextField(_("note"), blank=True)
class Meta:
verbose_name = _("attendance")
verbose_name_plural = _("attendances")
ordering = ["event", "member__last_name", "member__first_name"]
constraints = [
models.UniqueConstraint(fields=["event", "member"], name="unique_attendance_per_event_per_member"),
]
def __str__(self):
return f"{self.event} - {self.member}"
class Competition(models.Model):
"""A competition has a name with a specific URL to fetch data from. These are managed centrally."""
name = models.CharField(max_length=250)
module = models.CharField(max_length=250)
sport_type = models.CharField(
_("sport"),
max_length=20,
choices=Club.SportType.choices,
default=Club.SportType.OTHER,
help_text=_("Which sport this competition is for."),
)
flag = models.ForeignKey(
settings.WAFFLE_FLAG_MODEL,
on_delete=models.PROTECT,
null=True,
blank=True,
related_name="competitions",
verbose_name=_("feature flag"),
help_text=_("Which clubs this competition is offered to -- set (or leave blank to hide it everywhere) from the control panel's Features page. A competition with no flag never shows up on the Event admin's competition dropdown."),
)
class Meta:
verbose_name = _("competition")
verbose_name_plural = _("competitions")
ordering = ["name"]
def __str__(self):
return self.name