Skip to content

List late activity cancellations

In this example, it will be demonstrated how to list everyone who signed off from an activity during the last two hours right before it started.

The following logical steps are performed:

  • Define the team_id and activity_id.
  • Retrieve the activity and calculate the start of the two-hour cancellation window.
  • Retrieve the users associated with the activity.
  • Select users whose status is Not attending and whose response was updated within the cancellation window.
  • Print the users in the order they signed off.

The example uses updated_at as the sign-off time. This field represents the last time the activity-user record was updated, so the example assumes that its latest update was the attendance response.

from datetime import datetime, timedelta

from pyholdsport import (
    Holdsport,
    HoldsportActivitiesUser,
    HoldsportActivity,
    HoldsportActivityUserStatus,
)

with Holdsport(
    holdsport_username="username",
    holdsport_password="password",
) as holdsport:
    team_id = 123
    activity_id = 12345

    activity: HoldsportActivity | None = holdsport.get_activity(
        team_id=team_id,
        activity_id=activity_id,
    )
    if activity is None:
        msg = f"Activity with id {activity_id} was not found."
        raise ValueError(msg)

    activity_start = datetime.strptime(
        activity.starttime, "%Y-%m-%dT%H:%M:%S%z"
    )
    cancellation_window_start = activity_start - timedelta(hours=2)

    late_cancellations: list[tuple[HoldsportActivitiesUser, datetime]] = []
    activities_users: list[HoldsportActivitiesUser] = (
        holdsport.get_activities_users(
            activity_id=activity_id,
        )
    )
    for user in activities_users:
        signed_off_at = datetime.strptime(
            user.updated_at, "%Y-%m-%dT%H:%M:%S%z"
        )
        if (
            user.status_code is HoldsportActivityUserStatus.NOT_ATTENDING
            and cancellation_window_start <= signed_off_at < activity_start
        ):
            late_cancellations.append((user, signed_off_at))

    print(f"Late cancellations for '{activity.name}':")
    for user, signed_off_at in sorted(
        late_cancellations, key=lambda entry: (entry[1], entry[0].name)
    ):
        print(f"{user.name}: {signed_off_at.isoformat()}")