2023-01-03 20:36:34 +00:00
|
|
|
import contextlib
|
|
|
|
|
2023-01-02 22:06:00 +00:00
|
|
|
from django.contrib.auth.models import User
|
2023-01-03 20:36:34 +00:00
|
|
|
from django.utils import timezone
|
2023-01-02 22:06:00 +00:00
|
|
|
|
2023-01-03 20:36:34 +00:00
|
|
|
from membership.models import Member
|
2023-01-02 22:06:00 +00:00
|
|
|
from membership.models import Membership
|
|
|
|
from membership.models import SubscriptionPeriod
|
|
|
|
|
|
|
|
|
|
|
|
def get_subscription_periods() -> list[SubscriptionPeriod]:
|
|
|
|
subscription_periods = SubscriptionPeriod.objects.prefetch_related(
|
|
|
|
"membership_set",
|
|
|
|
"membership_set__user",
|
|
|
|
).all()
|
|
|
|
return list(subscription_periods)
|
|
|
|
|
|
|
|
|
2023-01-03 20:36:34 +00:00
|
|
|
def get_current_subscription_period() -> SubscriptionPeriod | None:
|
|
|
|
with contextlib.suppress(SubscriptionPeriod.DoesNotExist):
|
|
|
|
return SubscriptionPeriod.objects.prefetch_related(
|
2023-01-11 20:55:58 +00:00
|
|
|
"membership_set",
|
|
|
|
"membership_set__user",
|
2023-01-03 20:36:34 +00:00
|
|
|
).get(period__contains=timezone.now())
|
|
|
|
|
|
|
|
|
2023-01-02 22:06:00 +00:00
|
|
|
def get_memberships(
|
2023-01-11 20:55:58 +00:00
|
|
|
*,
|
|
|
|
user: User | None = None,
|
|
|
|
period: SubscriptionPeriod | None = None,
|
2023-01-02 22:06:00 +00:00
|
|
|
) -> Membership.QuerySet:
|
|
|
|
memberships = Membership.objects.select_related("membership_type").all()
|
|
|
|
|
|
|
|
if user:
|
|
|
|
memberships = memberships.for_user(user)
|
|
|
|
|
|
|
|
if period:
|
|
|
|
memberships = memberships.filter(period=period)
|
|
|
|
|
|
|
|
return memberships
|
2023-01-03 16:00:07 +00:00
|
|
|
|
|
|
|
|
2023-01-03 20:36:34 +00:00
|
|
|
def get_members():
|
2023-01-12 16:31:34 +00:00
|
|
|
return Member.objects.all().annotate_membership().order_by("username")
|
2023-01-03 20:36:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_member(*, member_id: int) -> Member:
|
|
|
|
return get_members().get(id=member_id)
|