Skip to content

Commit

Permalink
feat: serialize subscription plan is_current field
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Jul 18, 2024
1 parent 9594ca4 commit 30574c3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions license_manager/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class MinimalSubscriptionPlanSerializer(serializers.ModelSerializer):
"""
Minimal serializer for the `SubscriptionPlan` model.
"""
is_current = serializers.BooleanField(
help_text='Indicates whether start_date <= now <= expiration_date.',
read_only=True,
)

class Meta:
model = SubscriptionPlan
Expand All @@ -59,6 +63,7 @@ class Meta:
'enterprise_customer_uuid',
'enterprise_catalog_uuid',
'is_active',
'is_current',
'is_revocation_cap_enabled',
'days_until_expiration',
'days_until_expiration_including_renewals',
Expand Down
1 change: 1 addition & 0 deletions license_manager/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ def test_subscription_plan_create_staff_user_200(api_client, staff_user, boolean
"start_date",
"title",
"uuid",
"is_current",
}
assert response.json().keys() == expected_fields

Expand Down
8 changes: 8 additions & 0 deletions license_manager/apps/subscriptions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from django.forms import ValidationError
from django.utils import timezone
from django.utils.functional import cached_property
from django.utils.translation import gettext as _
from edx_rbac.models import UserRole, UserRoleAssignment
Expand Down Expand Up @@ -491,6 +492,13 @@ def days_until_expiration(self):
"""
return days_until(self.expiration_date)

@property
def is_current(self):
"""
Returns a boolean indicating whether start_date <= now <= expiration_date.
"""
return self.start_date <= timezone.now() <= self.expiration_date

@property
def has_revocations_remaining(self):
"""
Expand Down
23 changes: 23 additions & 0 deletions license_manager/apps/subscriptions/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ def test_prior_renewals(self):
)
self.assertEqual(renewed_subscription_plan_2.prior_renewals, [renewal_1, renewal_2])

@ddt.data(
{'start_date_delta': timedelta(days=-1), 'end_date_delta': timedelta(days=1), 'expected_current': True},
{'start_date_delta': timedelta(days=-1), 'end_date_delta': timedelta(days=0), 'expected_current': True},
{'start_date_delta': timedelta(days=0), 'end_date_delta': timedelta(days=1), 'expected_current': True},
{'start_date_delta': timedelta(days=0), 'end_date_delta': timedelta(days=0), 'expected_current': True},
{'start_date_delta': timedelta(days=1), 'end_date_delta': timedelta(days=1), 'expected_current': False},
{'start_date_delta': timedelta(days=10), 'end_date_delta': timedelta(days=150), 'expected_current': False},
{'start_date_delta': timedelta(days=-1), 'end_date_delta': timedelta(days=-1), 'expected_current': False},
{'start_date_delta': timedelta(days=-10), 'end_date_delta': timedelta(days=-5), 'expected_current': False},
)
@ddt.unpack
def test_is_current(self, start_date_delta, end_date_delta, expected_current):
today = localized_utcnow()
with freezegun.freeze_time(today):
subscription_plan = SubscriptionPlanFactory.create(
start_date=today + start_date_delta,
expiration_date=today + end_date_delta,
)
if expected_current:
self.assertTrue(subscription_plan.is_current)
else:
self.assertFalse(subscription_plan.is_current)

@ddt.data(True, False)
def test_is_locked_for_renewal_processing(self, is_locked_for_renewal_processing):
today = localized_utcnow()
Expand Down

0 comments on commit 30574c3

Please sign in to comment.