2017-04-24 15:51:12 +00:00
|
|
|
# -*- coding:utf-8 -*
|
|
|
|
#
|
|
|
|
# Copyright 2016,2017
|
|
|
|
# - Skia <skia@libskia.so>
|
|
|
|
#
|
|
|
|
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
|
|
# http://ae.utbm.fr.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU General Public License a published by the Free Software
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
|
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
from datetime import date, timedelta
|
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.conf import settings
|
2016-01-29 14:20:00 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.core.urlresolvers import reverse
|
2016-08-31 16:40:17 +00:00
|
|
|
from django.contrib.auth.forms import PasswordResetForm
|
2015-12-15 16:50:50 +00:00
|
|
|
|
|
|
|
from core.models import User
|
2017-06-12 20:53:25 +00:00
|
|
|
from core.utils import get_start_of_semester
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2016-08-27 05:10:48 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
def validate_type(value):
|
2016-03-31 08:36:00 +00:00
|
|
|
if value not in settings.SITH_SUBSCRIPTIONS.keys():
|
2015-12-15 16:50:50 +00:00
|
|
|
raise ValidationError(_('Bad subscription type'))
|
|
|
|
|
2017-06-12 08:10:42 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
def validate_payment(value):
|
2016-04-20 00:07:01 +00:00
|
|
|
if value not in settings.SITH_SUBSCRIPTION_PAYMENT_METHOD:
|
2015-12-15 16:50:50 +00:00
|
|
|
raise ValidationError(_('Bad payment method'))
|
|
|
|
|
2017-06-12 08:10:42 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
class Subscription(models.Model):
|
2016-12-10 00:58:30 +00:00
|
|
|
member = models.ForeignKey(User, related_name='subscriptions')
|
2015-12-15 16:50:50 +00:00
|
|
|
subscription_type = models.CharField(_('subscription type'),
|
|
|
|
max_length=255,
|
2017-06-12 08:10:42 +00:00
|
|
|
choices=((k, v['name']) for k, v in sorted(settings.SITH_SUBSCRIPTIONS.items())))
|
2015-12-15 16:50:50 +00:00
|
|
|
subscription_start = models.DateField(_('subscription start'))
|
|
|
|
subscription_end = models.DateField(_('subscription end'))
|
2017-04-25 22:51:56 +00:00
|
|
|
payment_method = models.CharField(_('payment method'),
|
2017-06-12 08:10:42 +00:00
|
|
|
max_length=255,
|
|
|
|
choices=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD)
|
2016-08-04 22:50:42 +00:00
|
|
|
location = models.CharField(choices=settings.SITH_SUBSCRIPTION_LOCATIONS,
|
2017-06-12 08:10:42 +00:00
|
|
|
max_length=20, verbose_name=_('location'))
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2016-01-29 14:20:00 +00:00
|
|
|
class Meta:
|
2017-06-12 08:10:42 +00:00
|
|
|
ordering = ['subscription_start', ]
|
2016-01-29 14:20:00 +00:00
|
|
|
|
|
|
|
def clean(self):
|
2016-07-06 22:52:30 +00:00
|
|
|
try:
|
|
|
|
for s in Subscription.objects.filter(member=self.member).exclude(pk=self.pk).all():
|
|
|
|
if s.is_valid_now():
|
|
|
|
raise ValidationError(_("You can not subscribe many time for the same period"))
|
2017-06-12 08:10:42 +00:00
|
|
|
except: # This should not happen, because the form should have handled the data before, but sadly, it still
|
2016-07-06 22:52:30 +00:00
|
|
|
# calls the model validation :'(
|
|
|
|
# TODO see SubscriptionForm's clean method
|
2016-09-02 10:29:50 +00:00
|
|
|
raise ValidationError(_("Subscription error"))
|
2016-01-29 14:20:00 +00:00
|
|
|
|
2016-07-19 22:28:49 +00:00
|
|
|
def save(self):
|
|
|
|
super(Subscription, self).save()
|
|
|
|
from counter.models import Customer
|
|
|
|
if not Customer.objects.filter(user=self.member).exists():
|
2017-06-12 08:10:42 +00:00
|
|
|
last_id = Customer.objects.count() + 1504 # Number to keep a continuity with the old site
|
|
|
|
Customer(user=self.member, account_id=Customer.generate_account_id(last_id + 1), amount=0).save()
|
2016-08-31 16:40:17 +00:00
|
|
|
form = PasswordResetForm({'email': self.member.email})
|
|
|
|
if form.is_valid():
|
|
|
|
form.save(use_https=True, email_template_name='core/new_user_email.jinja',
|
2017-06-12 08:10:42 +00:00
|
|
|
subject_template_name='core/new_user_email_subject.jinja', from_email="ae@utbm.fr")
|
2016-08-10 14:23:12 +00:00
|
|
|
self.member.make_home()
|
2016-09-02 19:24:10 +00:00
|
|
|
if settings.IS_OLD_MYSQL_PRESENT:
|
2016-09-14 11:19:56 +00:00
|
|
|
import MySQLdb
|
2017-06-12 08:10:42 +00:00
|
|
|
try: # Create subscription on the old site: TODO remove me!
|
2016-09-02 19:24:10 +00:00
|
|
|
LOCATION = {
|
2017-06-12 08:10:42 +00:00
|
|
|
"SEVENANS": 5,
|
|
|
|
"BELFORT": 6,
|
|
|
|
"MONTBELIARD": 9,
|
|
|
|
"EBOUTIC": 5,
|
|
|
|
}
|
2016-09-02 19:24:10 +00:00
|
|
|
TYPE = {
|
2017-06-12 08:10:42 +00:00
|
|
|
'un-semestre': 0,
|
|
|
|
'deux-semestres': 1,
|
|
|
|
'cursus-tronc-commun': 2,
|
|
|
|
'cursus-branche': 3,
|
|
|
|
'membre-honoraire': 4,
|
|
|
|
'assidu': 5,
|
|
|
|
'amicale/doceo': 6,
|
|
|
|
'reseau-ut': 7,
|
|
|
|
'crous': 8,
|
|
|
|
'sbarro/esta': 9,
|
|
|
|
'cursus-alternant': 10,
|
2017-08-21 19:06:05 +00:00
|
|
|
'welcome-semestre': 11,
|
2017-06-12 08:10:42 +00:00
|
|
|
}
|
2016-09-02 19:24:10 +00:00
|
|
|
PAYMENT = {
|
2017-06-12 08:10:42 +00:00
|
|
|
"CHECK": 1,
|
|
|
|
"CARD": 2,
|
|
|
|
"CASH": 3,
|
|
|
|
"OTHER": 4,
|
|
|
|
"EBOUTIC": 5,
|
|
|
|
"OTHER": 0,
|
|
|
|
}
|
2016-09-02 19:24:10 +00:00
|
|
|
|
|
|
|
db = MySQLdb.connect(**settings.OLD_MYSQL_INFOS)
|
|
|
|
c = db.cursor()
|
|
|
|
c.execute("""INSERT INTO ae_cotisations (id_utilisateur, date_cotis, date_fin_cotis, mode_paiement_cotis,
|
|
|
|
type_cotis, id_comptoir) VALUES (%s, %s, %s, %s, %s, %s)""", (self.member.id, self.subscription_start,
|
2017-06-12 08:10:42 +00:00
|
|
|
self.subscription_end, PAYMENT[self.payment_method], TYPE[self.subscription_type],
|
|
|
|
LOCATION[self.location]))
|
2016-09-02 19:24:10 +00:00
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
2017-06-12 08:10:42 +00:00
|
|
|
with open(settings.BASE_DIR + "/subscription_fail.log", "a") as f:
|
2016-09-02 19:24:10 +00:00
|
|
|
print("FAIL to add subscription to %s to old site" % (self.member), file=f)
|
|
|
|
print("Reason: %s" % (repr(e)), file=f)
|
|
|
|
db.rollback()
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2016-01-29 14:20:00 +00:00
|
|
|
def get_absolute_url(self):
|
2016-08-22 00:56:27 +00:00
|
|
|
return reverse('core:user_edit', kwargs={'user_id': self.member.pk})
|
2016-01-29 14:20:00 +00:00
|
|
|
|
2016-01-28 16:42:22 +00:00
|
|
|
def __str__(self):
|
2016-07-06 22:52:30 +00:00
|
|
|
if hasattr(self, "member") and self.member is not None:
|
2017-06-12 08:10:42 +00:00
|
|
|
return self.member.username + ' - ' + str(self.pk)
|
2016-07-06 22:52:30 +00:00
|
|
|
else:
|
2017-06-12 08:10:42 +00:00
|
|
|
return 'No user - ' + str(self.pk)
|
2016-01-28 16:42:22 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
@staticmethod
|
2016-07-28 10:41:29 +00:00
|
|
|
def compute_start(d=date.today(), duration=1):
|
2015-12-15 16:50:50 +00:00
|
|
|
"""
|
|
|
|
This function computes the start date of the subscription with respect to the given date (default is today),
|
2016-03-31 08:36:00 +00:00
|
|
|
and the start date given in settings.SITH_START_DATE.
|
2015-12-15 16:50:50 +00:00
|
|
|
It takes the nearest past start date.
|
2016-03-31 08:36:00 +00:00
|
|
|
Exemples: with SITH_START_DATE = (8, 15)
|
2015-12-15 16:50:50 +00:00
|
|
|
Today -> Start date
|
|
|
|
2015-03-17 -> 2015-02-15
|
|
|
|
2015-01-11 -> 2014-08-15
|
|
|
|
"""
|
2017-06-12 08:10:42 +00:00
|
|
|
if duration <= 2: # Sliding subscriptions for 1 or 2 semesters
|
2016-07-28 10:41:29 +00:00
|
|
|
return d
|
2017-06-12 20:53:25 +00:00
|
|
|
return get_start_of_semester(d)
|
2015-12-15 16:50:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def compute_end(duration, start=None):
|
|
|
|
"""
|
|
|
|
This function compute the end date of the subscription given a start date and a duration in number of semestre
|
|
|
|
Exemple:
|
|
|
|
Start - Duration -> End date
|
|
|
|
2015-09-18 - 1 -> 2016-03-18
|
|
|
|
2015-09-18 - 2 -> 2016-09-18
|
|
|
|
2015-09-18 - 3 -> 2017-03-18
|
|
|
|
2015-09-18 - 4 -> 2017-09-18
|
|
|
|
"""
|
|
|
|
if start is None:
|
2016-07-28 10:41:29 +00:00
|
|
|
start = Subscription.compute_start(duration=duration)
|
2015-12-15 16:50:50 +00:00
|
|
|
# This can certainly be simplified, but it works like this
|
2016-07-28 10:41:29 +00:00
|
|
|
try:
|
2017-06-12 08:10:42 +00:00
|
|
|
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))
|
2016-07-28 10:41:29 +00:00
|
|
|
except ValueError as e:
|
2017-06-12 08:10:42 +00:00
|
|
|
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))
|
2016-07-28 10:41:29 +00:00
|
|
|
|
2016-03-22 10:42:00 +00:00
|
|
|
def can_be_edited_by(self, user):
|
2016-08-14 02:35:08 +00:00
|
|
|
return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_root
|
2016-03-22 10:42:00 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
def is_valid_now(self):
|
|
|
|
return self.subscription_start <= date.today() and date.today() <= self.subscription_end
|
|
|
|
|
2017-06-12 08:10:42 +00:00
|
|
|
|
2016-07-28 10:41:29 +00:00
|
|
|
def guy_test(date, duration=4):
|
2017-06-12 08:10:42 +00:00
|
|
|
print(str(date) + " - " + str(duration) + " -> " + str(Subscription.compute_start(date, duration)))
|
|
|
|
|
|
|
|
|
2016-07-28 10:41:29 +00:00
|
|
|
def bibou_test(duration, date=date.today()):
|
2017-06-12 08:10:42 +00:00
|
|
|
print(str(date) + " - " + str(duration) + " -> " + str(Subscription.compute_end(duration, Subscription.compute_start(date, duration))))
|
|
|
|
|
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
def guy():
|
|
|
|
guy_test(date(2015, 7, 11))
|
|
|
|
guy_test(date(2015, 8, 11))
|
|
|
|
guy_test(date(2015, 2, 17))
|
|
|
|
guy_test(date(2015, 3, 17))
|
|
|
|
guy_test(date(2015, 1, 11))
|
|
|
|
guy_test(date(2015, 2, 11))
|
|
|
|
guy_test(date(2015, 8, 17))
|
|
|
|
guy_test(date(2015, 9, 17))
|
2017-06-12 08:10:42 +00:00
|
|
|
print('=' * 80)
|
2016-07-28 10:41:29 +00:00
|
|
|
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)
|
2017-06-12 08:10:42 +00:00
|
|
|
print('=' * 80)
|
2015-12-15 16:50:50 +00:00
|
|
|
bibou_test(1, date(2015, 2, 18))
|
|
|
|
bibou_test(2, date(2015, 2, 18))
|
|
|
|
bibou_test(3, date(2015, 2, 18))
|
|
|
|
bibou_test(4, date(2015, 2, 18))
|
|
|
|
bibou_test(1, date(2015, 9, 18))
|
|
|
|
bibou_test(2, date(2015, 9, 18))
|
|
|
|
bibou_test(3, date(2015, 9, 18))
|
|
|
|
bibou_test(4, date(2015, 9, 18))
|
2017-06-12 08:10:42 +00:00
|
|
|
print('=' * 80)
|
2016-07-28 11:02:10 +00:00
|
|
|
bibou_test(1, date(2000, 2, 29))
|
2016-07-28 10:41:29 +00:00
|
|
|
bibou_test(2, date(2000, 2, 29))
|
|
|
|
bibou_test(1, date(2000, 5, 31))
|
|
|
|
bibou_test(1, date(2000, 7, 31))
|
2015-12-15 16:50:50 +00:00
|
|
|
bibou_test(1)
|
|
|
|
bibou_test(2)
|
|
|
|
bibou_test(3)
|
|
|
|
bibou_test(4)
|
|
|
|
|
2017-06-12 08:10:42 +00:00
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
guy()
|