Make sliding subscriptions for 1 or 2 semesters

This commit is contained in:
Skia 2016-07-28 12:41:29 +02:00
parent 80f72df1de
commit e3705f9fe2
2 changed files with 29 additions and 8 deletions

View File

@ -64,7 +64,7 @@ class Subscription(models.Model):
@staticmethod @staticmethod
def compute_start(d=date.today()): def compute_start(d=date.today(), duration=1):
""" """
This function computes the start date of the subscription with respect to the given date (default is today), This function computes the start date of the subscription with respect to the given date (default is today),
and the start date given in settings.SITH_START_DATE. and the start date given in settings.SITH_START_DATE.
@ -74,6 +74,8 @@ class Subscription(models.Model):
2015-03-17 -> 2015-02-15 2015-03-17 -> 2015-02-15
2015-01-11 -> 2014-08-15 2015-01-11 -> 2014-08-15
""" """
if duration <= 2: # Sliding subscriptions for 1 or 2 semesters
return d
today = d today = d
year = today.year year = today.year
start = date(year, settings.SITH_START_DATE[0], settings.SITH_START_DATE[1]) start = date(year, settings.SITH_START_DATE[0], settings.SITH_START_DATE[1])
@ -99,10 +101,15 @@ class Subscription(models.Model):
2015-09-18 - 4 -> 2017-09-18 2015-09-18 - 4 -> 2017-09-18
""" """
if start is None: if start is None:
start = Subscription.compute_start() start = Subscription.compute_start(duration=duration)
# This can certainly be simplified, but it works like this # This can certainly be simplified, but it works like this
return start.replace(month=(start.month+6*duration)%12, try:
return start.replace(month=(start.month-1+6*duration)%12+1,
year=start.year+int(duration/2)+(1 if start.month > 6 and duration%2 == 1 else 0)) year=start.year+int(duration/2)+(1 if start.month > 6 and duration%2 == 1 else 0))
except ValueError as e:
return start.replace(day=1, month=(start.month+6*duration)%12+1,
year=start.year+int(duration/2)+(1 if start.month > 6 and duration%2 == 1 else 0))
def can_be_edited_by(self, user): def can_be_edited_by(self, user):
return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_in_group(settings.SITH_GROUPS['root']['name']) return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_in_group(settings.SITH_GROUPS['root']['name'])
@ -110,10 +117,10 @@ class Subscription(models.Model):
def is_valid_now(self): def is_valid_now(self):
return self.subscription_start <= date.today() and date.today() <= self.subscription_end return self.subscription_start <= date.today() and date.today() <= self.subscription_end
def guy_test(date): def guy_test(date, duration=4):
print(str(date)+" -> "+str(Subscription.compute_start(date))) print(str(date)+" - "+str(duration)+" -> "+str(Subscription.compute_start(date, duration)))
def bibou_test(duration, date=None): def bibou_test(duration, date=date.today()):
print(str(date)+" - "+str(duration)+" -> "+str(Subscription.compute_end(duration, date))) print(str(date)+" - "+str(duration)+" -> "+str(Subscription.compute_end(duration, Subscription.compute_start(date, duration))))
def guy(): def guy():
guy_test(date(2015, 7, 11)) guy_test(date(2015, 7, 11))
guy_test(date(2015, 8, 11)) guy_test(date(2015, 8, 11))
@ -124,6 +131,15 @@ def guy():
guy_test(date(2015, 8, 17)) guy_test(date(2015, 8, 17))
guy_test(date(2015, 9, 17)) guy_test(date(2015, 9, 17))
print('='*80) print('='*80)
guy_test(date(2015, 7, 11), 1)
guy_test(date(2015, 8, 11), 2)
guy_test(date(2015, 2, 17), 3)
guy_test(date(2015, 3, 17), 4)
guy_test(date(2015, 1, 11), 1)
guy_test(date(2015, 2, 11), 2)
guy_test(date(2015, 8, 17), 3)
guy_test(date(2015, 9, 17), 4)
print('='*80)
bibou_test(1, date(2015, 2, 18)) bibou_test(1, date(2015, 2, 18))
bibou_test(2, date(2015, 2, 18)) bibou_test(2, date(2015, 2, 18))
bibou_test(3, date(2015, 2, 18)) bibou_test(3, date(2015, 2, 18))
@ -132,6 +148,10 @@ def guy():
bibou_test(2, date(2015, 9, 18)) bibou_test(2, date(2015, 9, 18))
bibou_test(3, date(2015, 9, 18)) bibou_test(3, date(2015, 9, 18))
bibou_test(4, date(2015, 9, 18)) bibou_test(4, date(2015, 9, 18))
print('='*80)
bibou_test(2, date(2000, 2, 29))
bibou_test(1, date(2000, 5, 31))
bibou_test(1, date(2000, 7, 31))
bibou_test(1) bibou_test(1)
bibou_test(2) bibou_test(2)
bibou_test(3) bibou_test(3)

View File

@ -66,7 +66,8 @@ class NewSubscription(CanEditMixin, CreateView): # TODO: this must be able to cr
return {} return {}
def form_valid(self, form): def form_valid(self, form):
form.instance.subscription_start = Subscription.compute_start() form.instance.subscription_start = Subscription.compute_start(
duration=settings.SITH_SUBSCRIPTIONS[form.instance.subscription_type]['duration'])
form.instance.subscription_end = Subscription.compute_end( form.instance.subscription_end = Subscription.compute_end(
duration=settings.SITH_SUBSCRIPTIONS[form.instance.subscription_type]['duration'], duration=settings.SITH_SUBSCRIPTIONS[form.instance.subscription_type]['duration'],
start=form.instance.subscription_start start=form.instance.subscription_start