mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-11 04:19:25 +00:00
All: Apply Black coding rules
This commit is contained in:
@ -41,74 +41,101 @@ from core.utils import get_start_of_semester
|
||||
|
||||
def validate_type(value):
|
||||
if value not in settings.SITH_SUBSCRIPTIONS.keys():
|
||||
raise ValidationError(_('Bad subscription type'))
|
||||
raise ValidationError(_("Bad subscription type"))
|
||||
|
||||
|
||||
def validate_payment(value):
|
||||
if value not in settings.SITH_SUBSCRIPTION_PAYMENT_METHOD:
|
||||
raise ValidationError(_('Bad payment method'))
|
||||
raise ValidationError(_("Bad payment method"))
|
||||
|
||||
|
||||
class Subscription(models.Model):
|
||||
member = models.ForeignKey(User, related_name='subscriptions')
|
||||
subscription_type = models.CharField(_('subscription type'),
|
||||
max_length=255,
|
||||
choices=((k, v['name']) for k, v in sorted(settings.SITH_SUBSCRIPTIONS.items())))
|
||||
subscription_start = models.DateField(_('subscription start'))
|
||||
subscription_end = models.DateField(_('subscription end'))
|
||||
payment_method = models.CharField(_('payment method'),
|
||||
max_length=255,
|
||||
choices=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD)
|
||||
location = models.CharField(choices=settings.SITH_SUBSCRIPTION_LOCATIONS,
|
||||
max_length=20, verbose_name=_('location'))
|
||||
member = models.ForeignKey(User, related_name="subscriptions")
|
||||
subscription_type = models.CharField(
|
||||
_("subscription type"),
|
||||
max_length=255,
|
||||
choices=(
|
||||
(k, v["name"]) for k, v in sorted(settings.SITH_SUBSCRIPTIONS.items())
|
||||
),
|
||||
)
|
||||
subscription_start = models.DateField(_("subscription start"))
|
||||
subscription_end = models.DateField(_("subscription end"))
|
||||
payment_method = models.CharField(
|
||||
_("payment method"),
|
||||
max_length=255,
|
||||
choices=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD,
|
||||
)
|
||||
location = models.CharField(
|
||||
choices=settings.SITH_SUBSCRIPTION_LOCATIONS,
|
||||
max_length=20,
|
||||
verbose_name=_("location"),
|
||||
)
|
||||
|
||||
class Meta:
|
||||
ordering = ['subscription_start', ]
|
||||
ordering = ["subscription_start"]
|
||||
|
||||
def clean(self):
|
||||
try:
|
||||
for s in Subscription.objects.filter(member=self.member).exclude(pk=self.pk).all():
|
||||
if s.is_valid_now() and s.subscription_end - timedelta(weeks=settings.SITH_SUBSCRIPTION_END) > date.today():
|
||||
raise ValidationError(_("You can not subscribe many time for the same period"))
|
||||
for s in (
|
||||
Subscription.objects.filter(member=self.member)
|
||||
.exclude(pk=self.pk)
|
||||
.all()
|
||||
):
|
||||
if (
|
||||
s.is_valid_now()
|
||||
and s.subscription_end
|
||||
- timedelta(weeks=settings.SITH_SUBSCRIPTION_END)
|
||||
> date.today()
|
||||
):
|
||||
raise ValidationError(
|
||||
_("You can not subscribe many time for the same period")
|
||||
)
|
||||
except: # This should not happen, because the form should have handled the data before, but sadly, it still
|
||||
# calls the model validation :'(
|
||||
# TODO see SubscriptionForm's clean method
|
||||
# calls the model validation :'(
|
||||
# TODO see SubscriptionForm's clean method
|
||||
raise ValidationError(_("Subscription error"))
|
||||
|
||||
def save(self):
|
||||
super(Subscription, self).save()
|
||||
from counter.models import Customer
|
||||
|
||||
if not Customer.objects.filter(user=self.member).exists():
|
||||
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()
|
||||
form = PasswordResetForm({'email': self.member.email})
|
||||
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()
|
||||
form = PasswordResetForm({"email": self.member.email})
|
||||
if form.is_valid():
|
||||
form.save(use_https=True, email_template_name='core/new_user_email.jinja',
|
||||
subject_template_name='core/new_user_email_subject.jinja', from_email="ae@utbm.fr")
|
||||
form.save(
|
||||
use_https=True,
|
||||
email_template_name="core/new_user_email.jinja",
|
||||
subject_template_name="core/new_user_email_subject.jinja",
|
||||
from_email="ae@utbm.fr",
|
||||
)
|
||||
self.member.make_home()
|
||||
if settings.IS_OLD_MYSQL_PRESENT:
|
||||
import MySQLdb
|
||||
|
||||
try: # Create subscription on the old site: TODO remove me!
|
||||
LOCATION = {
|
||||
"SEVENANS": 5,
|
||||
"BELFORT": 6,
|
||||
"MONTBELIARD": 9,
|
||||
"EBOUTIC": 5,
|
||||
}
|
||||
LOCATION = {"SEVENANS": 5, "BELFORT": 6, "MONTBELIARD": 9, "EBOUTIC": 5}
|
||||
TYPE = {
|
||||
'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,
|
||||
'welcome-semestre': 11,
|
||||
'deux-mois-essai': 12,
|
||||
"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,
|
||||
"welcome-semestre": 11,
|
||||
"deux-mois-essai": 12,
|
||||
}
|
||||
PAYMENT = {
|
||||
"CHECK": 1,
|
||||
@ -121,25 +148,36 @@ class Subscription(models.Model):
|
||||
|
||||
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,
|
||||
self.subscription_end, PAYMENT[self.payment_method], TYPE[self.subscription_type],
|
||||
LOCATION[self.location]))
|
||||
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,
|
||||
self.subscription_end,
|
||||
PAYMENT[self.payment_method],
|
||||
TYPE[self.subscription_type],
|
||||
LOCATION[self.location],
|
||||
),
|
||||
)
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
with open(settings.BASE_DIR + "/subscription_fail.log", "a") as f:
|
||||
print("FAIL to add subscription to %s to old site" % (self.member), file=f)
|
||||
print(
|
||||
"FAIL to add subscription to %s to old site" % (self.member),
|
||||
file=f,
|
||||
)
|
||||
print("Reason: %s" % (repr(e)), file=f)
|
||||
db.rollback()
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('core:user_edit', kwargs={'user_id': self.member.pk})
|
||||
return reverse("core:user_edit", kwargs={"user_id": self.member.pk})
|
||||
|
||||
def __str__(self):
|
||||
if hasattr(self, "member") and self.member is not None:
|
||||
return self.member.username + ' - ' + str(self.pk)
|
||||
return self.member.username + " - " + str(self.pk)
|
||||
else:
|
||||
return 'No user - ' + str(self.pk)
|
||||
return "No user - " + str(self.pk)
|
||||
|
||||
@staticmethod
|
||||
def compute_start(d=None, duration=1, user=None):
|
||||
@ -176,21 +214,43 @@ class Subscription(models.Model):
|
||||
if start is None:
|
||||
start = Subscription.compute_start(duration=duration, user=user)
|
||||
|
||||
return start + relativedelta(months=round(6*duration),days=math.ceil((6*duration - round(6*duration)) * 30))
|
||||
|
||||
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
|
||||
|
||||
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, duration=4):
|
||||
print(str(date) + " - " + str(duration) + " -> " + str(Subscription.compute_start(date, duration)))
|
||||
print(
|
||||
str(date)
|
||||
+ " - "
|
||||
+ str(duration)
|
||||
+ " -> "
|
||||
+ str(Subscription.compute_start(date, duration))
|
||||
)
|
||||
|
||||
|
||||
def bibou_test(duration, date=date.today()):
|
||||
print(str(date) + " - " + str(duration) + " -> " + str(Subscription.compute_end(duration, Subscription.compute_start(date, duration))))
|
||||
print(
|
||||
str(date)
|
||||
+ " - "
|
||||
+ str(duration)
|
||||
+ " -> "
|
||||
+ str(
|
||||
Subscription.compute_end(
|
||||
duration, Subscription.compute_start(date, duration)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def guy():
|
||||
@ -202,7 +262,7 @@ def guy():
|
||||
guy_test(date(2015, 2, 11))
|
||||
guy_test(date(2015, 8, 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)
|
||||
@ -211,7 +271,7 @@ def guy():
|
||||
guy_test(date(2015, 2, 11), 2)
|
||||
guy_test(date(2015, 8, 17), 3)
|
||||
guy_test(date(2015, 9, 17), 4)
|
||||
print('=' * 80)
|
||||
print("=" * 80)
|
||||
bibou_test(1, date(2015, 2, 18))
|
||||
bibou_test(2, date(2015, 2, 18))
|
||||
bibou_test(3, date(2015, 2, 18))
|
||||
@ -220,7 +280,7 @@ def guy():
|
||||
bibou_test(2, date(2015, 9, 18))
|
||||
bibou_test(3, date(2015, 9, 18))
|
||||
bibou_test(4, date(2015, 9, 18))
|
||||
print('=' * 80)
|
||||
print("=" * 80)
|
||||
bibou_test(1, date(2000, 2, 29))
|
||||
bibou_test(2, date(2000, 2, 29))
|
||||
bibou_test(1, date(2000, 5, 31))
|
||||
|
Reference in New Issue
Block a user