compute_end count day by day now (not month by month like before) to allow 6 weeks cotiz

This commit is contained in:
Soldat
2018-09-01 17:45:13 +02:00
parent f78d8e1d95
commit 7a1b9bd412
6 changed files with 140 additions and 100 deletions

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-08-31 18:16
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('subscription', '0007_auto_20180706_1135'),
]
operations = [
migrations.AlterField(
model_name='subscription',
name='subscription_type',
field=models.CharField(choices=[('amicale/doceo', 'Amicale/DOCEO member'), ('assidu', 'Assidu member'), ('benevoles-euroks', "Eurok's volunteer"), ('crous', 'CROUS member'), ('cursus-alternant', 'Alternating cursus'), ('cursus-branche', 'Branch cursus'), ('cursus-tronc-commun', 'Common core cursus'), ('deux-mois-essai', 'Two month for free'), ('deux-semestres', 'Two semesters'), ('membre-honoraire', 'Honorary member'), ('reseau-ut', 'UT network member'), ('sbarro/esta', 'Sbarro/ESTA member'), ('six-semaines-essai', 'Six weeks for free'), ('un-semestre', 'One semester'), ('un-semestre-welcome', 'One semester Welcome Week')], max_length=255, verbose_name='subscription type'),
),
]

View File

@ -31,6 +31,10 @@ from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.contrib.auth.forms import PasswordResetForm
from dateutil.relativedelta import relativedelta
import math
from core.models import User
from core.utils import get_start_of_semester
@ -172,14 +176,8 @@ class Subscription(models.Model):
if start is None:
start = Subscription.compute_start(duration=duration, user=user)
# This can certainly be simplified, but it works like this
try:
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)))
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)),
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)))
return start + relativedelta(months=round(6*duration),days=math.ceil((6*duration - round(6*duration)) * 30))
def can_be_edited_by(self, user):
return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_root

View File

@ -92,7 +92,7 @@ class SubscriptionUnitTest(TestCase):
d = Subscription.compute_end(duration=0.33)
self.assertTrue(d == date(2015, 11, 18))
d = Subscription.compute_end(duration=0.67)
self.assertTrue(d == date(2016, 1, 18))
self.assertTrue(d == date(2016, 1, 19))
d = Subscription.compute_end(duration=0.5)
self.assertTrue(d == date(2015, 12, 18))
@ -135,7 +135,17 @@ class SubscriptionIntegrationTest(TestCase):
s.subscription_end = s.compute_end(duration=0.67,
start=s.subscription_start)
s.save()
self.assertTrue(s.subscription_end == date(2017, 12, 29))
self.assertTrue(s.subscription_end == date(2017, 12, 30))
def test_duration_six_weeks(self):
s = Subscription(member=User.objects.filter(pk=self.user.pk).first(), subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0])
s.subscription_start = date(2018, 9, 1)
s.subscription_end = s.compute_end(duration=0.23,
start=s.subscription_start)
s.save()
self.assertTrue(s.subscription_end == date(2018, 10, 13))
@mock.patch('subscription.models.date', FakeDate)
def test_dates_sliding_with_subscribed_user(self):
@ -150,6 +160,8 @@ class SubscriptionIntegrationTest(TestCase):
date_mock_today(2016, 8, 25)
d = Subscription.compute_end(duration=settings.SITH_SUBSCRIPTIONS['deux-semestres']['duration'],
user=user)
self.assertTrue(d == date(2017, 8, 29))
@mock.patch('subscription.models.date', FakeDate)