21 lines
769 B
Python
21 lines
769 B
Python
from django.utils.timezone import now
|
|
|
|
from manage_subscriptions.models import PrincipalSubscription, SubscriptionStatus
|
|
|
|
|
|
def get_active_subscription_id_for_principal(principal):
|
|
# Filter subscriptions for the principal that are active and not cancelled
|
|
active_subscriptions = PrincipalSubscription.objects.filter(
|
|
principal=principal,
|
|
status=SubscriptionStatus.ACTIVE,
|
|
cancelled=False,
|
|
end_date__gte=now().date(), # Ensure the subscription hasn't expired
|
|
).order_by(
|
|
"-end_date"
|
|
) # Order by end_date to get the most recent active subscription
|
|
|
|
if active_subscriptions.exists():
|
|
# Return the ID of the most recent active subscription
|
|
return active_subscriptions.first().id
|
|
return None
|