subscription: fix compute_start

This commit is contained in:
Skia 2018-04-16 14:46:13 +02:00
parent d619e0cd9b
commit e9e51d34d3
2 changed files with 38 additions and 5 deletions

View File

@ -151,7 +151,9 @@ class Subscription(models.Model):
if not d: if not d:
d = date.today() d = date.today()
if user is not None and user.subscriptions.exists(): if user is not None and user.subscriptions.exists():
d = user.subscriptions.last().subscription_end last = user.subscriptions.last()
if last.is_valid_now():
d = last.subscription_end
if duration <= 2: # Sliding subscriptions for 1 or 2 semesters if duration <= 2: # Sliding subscriptions for 1 or 2 semesters
return d return d
return get_start_of_semester(d) return get_start_of_semester(d)
@ -174,7 +176,7 @@ class Subscription(models.Model):
try: try:
return start.replace(month=int(round((start.month - 1 + 6 * duration) % 12 + 1, 0)), return start.replace(month=int(round((start.month - 1 + 6 * duration) % 12 + 1, 0)),
year=int(round(start.year + int(duration / 2) + (1 if int(start.month + 6 * duration) > 12 and (duration % 2 == 1 or duration < 1) else 0), 0))) year=int(round(start.year + int(duration / 2) + (1 if int(start.month + 6 * duration) > 12 and (duration % 2 == 1 or duration < 1) else 0), 0)))
except ValueError as e: except ValueError as e: # This catches 29th of February errors
return start.replace(day=1, month=int(round((start.month + 6 * duration) % 12 + 1, 0)), return start.replace(day=1, month=int(round((start.month + 6 * duration) % 12 + 1, 0)),
year=int(round(start.year + int(duration / 2) + (1 if int(start.month + 6 * duration) > 12 and (duration % 2 == 1 or duration < 1) else 0), 0))) year=int(round(start.year + int(duration / 2) + (1 if int(start.month + 6 * duration) > 12 and (duration % 2 == 1 or duration < 1) else 0), 0)))

View File

@ -140,15 +140,46 @@ class SubscriptionIntegrationTest(TestCase):
@mock.patch('subscription.models.date', FakeDate) @mock.patch('subscription.models.date', FakeDate)
def test_dates_sliding_with_subscribed_user(self): def test_dates_sliding_with_subscribed_user(self):
user = User.objects.filter(pk=self.user.pk).first() user = User.objects.filter(pk=self.user.pk).first()
s = Subscription(member=user, subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3], s = Subscription(member=user, subscription_type='deux-semestres',
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0]) payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0])
s.subscription_start = date(2015, 8, 29) s.subscription_start = date(2015, 8, 29)
s.subscription_end = s.compute_end(duration=2, s.subscription_end = s.compute_end(duration=settings.SITH_SUBSCRIPTIONS[s.subscription_type]['duration'],
start=s.subscription_start) start=s.subscription_start)
s.save() s.save()
self.assertTrue(s.subscription_end == date(2016, 8, 29)) self.assertTrue(s.subscription_end == date(2016, 8, 29))
date_mock_today(2016, 8, 25) date_mock_today(2016, 8, 25)
d = Subscription.compute_end(duration=2, d = Subscription.compute_end(duration=settings.SITH_SUBSCRIPTIONS['deux-semestres']['duration'],
user=user) user=user)
self.assertTrue(d == date(2017, 8, 29)) self.assertTrue(d == date(2017, 8, 29))
@mock.patch('subscription.models.date', FakeDate)
def test_dates_renewal_sliding_during_two_free_monthes(self):
user = User.objects.filter(pk=self.user.pk).first()
s = Subscription(member=user, subscription_type='deux-mois-essai',
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0])
s.subscription_start = date(2015, 8, 29)
s.subscription_end = s.compute_end(duration=settings.SITH_SUBSCRIPTIONS[s.subscription_type]['duration'],
start=s.subscription_start)
s.save()
self.assertTrue(s.subscription_end == date(2015, 10, 29))
date_mock_today(2015, 9, 25)
d = Subscription.compute_end(duration=settings.SITH_SUBSCRIPTIONS['deux-semestres']['duration'],
user=user)
self.assertTrue(d == date(2016, 10, 29))
@mock.patch('subscription.models.date', FakeDate)
def test_dates_renewal_sliding_after_two_free_monthes(self):
user = User.objects.filter(pk=self.user.pk).first()
s = Subscription(member=user, subscription_type='deux-mois-essai',
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0])
s.subscription_start = date(2015, 8, 29)
s.subscription_end = s.compute_end(duration=settings.SITH_SUBSCRIPTIONS[s.subscription_type]['duration'],
start=s.subscription_start)
s.save()
self.assertTrue(s.subscription_end == date(2015, 10, 29))
date_mock_today(2015, 11, 5)
d = Subscription.compute_end(duration=settings.SITH_SUBSCRIPTIONS['deux-semestres']['duration'],
user=user)
print(d)
self.assertTrue(d == date(2016, 11, 5))