2020-02-18 20:30:26 +00:00
|
|
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
2019-07-16 10:43:23 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2019-07-23 00:13:09 +00:00
|
|
|
import unicodedata
|
2019-07-24 20:03:07 +00:00
|
|
|
|
|
|
|
from django.conf import settings
|
2020-09-01 13:52:27 +00:00
|
|
|
from django.conf.global_settings import DEFAULT_FROM_EMAIL
|
2019-07-23 00:13:09 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2020-09-01 13:52:27 +00:00
|
|
|
from django.core.mail import send_mail
|
2019-07-23 00:13:09 +00:00
|
|
|
from django.core.validators import RegexValidator
|
2019-07-16 10:43:23 +00:00
|
|
|
from django.db import models
|
2020-08-01 19:44:16 +00:00
|
|
|
from django.template.loader import render_to_string
|
2020-08-01 15:49:23 +00:00
|
|
|
from django.utils import timezone
|
2019-07-16 10:43:23 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-07-17 09:17:50 +00:00
|
|
|
from polymorphic.models import PolymorphicModel
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
"""
|
|
|
|
Defines each note types
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-07-17 09:17:50 +00:00
|
|
|
class Note(PolymorphicModel):
|
2019-07-16 10:43:23 +00:00
|
|
|
"""
|
2020-01-21 21:06:06 +00:00
|
|
|
Gives transactions capabilities. Note is a Polymorphic Model, use as based
|
|
|
|
for the models :model:`note.NoteUser` and :model:`note.NoteClub`.
|
|
|
|
A Note principaly store the actual balance of someone/some club.
|
|
|
|
A Note can be searched find throught an :model:`note.Alias`
|
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
"""
|
2020-08-31 18:15:48 +00:00
|
|
|
|
2020-08-13 16:04:28 +00:00
|
|
|
balance = models.BigIntegerField(
|
2019-07-16 10:43:23 +00:00
|
|
|
verbose_name=_('account balance'),
|
|
|
|
help_text=_('in centimes, money credited for this instance'),
|
2019-07-17 07:37:11 +00:00
|
|
|
default=0,
|
2019-07-16 10:43:23 +00:00
|
|
|
)
|
2020-08-31 18:15:48 +00:00
|
|
|
|
2020-03-07 21:28:59 +00:00
|
|
|
last_negative = models.DateTimeField(
|
2020-02-25 13:15:36 +00:00
|
|
|
verbose_name=_('last negative date'),
|
|
|
|
help_text=_('last time the balance was negative'),
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
)
|
2020-08-31 18:15:48 +00:00
|
|
|
|
2019-07-16 13:22:38 +00:00
|
|
|
display_image = models.ImageField(
|
|
|
|
verbose_name=_('display image'),
|
|
|
|
max_length=255,
|
2020-03-04 15:34:12 +00:00
|
|
|
blank=False,
|
|
|
|
null=False,
|
2020-03-06 19:07:15 +00:00
|
|
|
upload_to='pic/',
|
2020-03-03 13:24:07 +00:00
|
|
|
default='pic/default.png'
|
2019-07-16 13:22:38 +00:00
|
|
|
)
|
2020-08-31 18:15:48 +00:00
|
|
|
|
2019-07-17 10:14:23 +00:00
|
|
|
created_at = models.DateTimeField(
|
|
|
|
verbose_name=_('created at'),
|
2020-08-01 15:49:23 +00:00
|
|
|
default=timezone.now,
|
2019-07-17 10:14:23 +00:00
|
|
|
)
|
2019-07-16 10:43:23 +00:00
|
|
|
|
2020-08-31 18:15:48 +00:00
|
|
|
is_active = models.BooleanField(
|
|
|
|
_('active'),
|
|
|
|
default=True,
|
|
|
|
help_text=_(
|
|
|
|
'Designates whether this note should be treated as active. '
|
|
|
|
'Unselect this instead of deleting notes.'),
|
|
|
|
)
|
|
|
|
|
|
|
|
inactivity_reason = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
choices=[
|
|
|
|
('manual', _("The user blocked his/her note manually, eg. when he/she left the school for holidays. "
|
|
|
|
"It can be reactivated at any time.")),
|
|
|
|
('forced', _("The note is blocked by the the BDE and can't be manually reactivated.")),
|
|
|
|
],
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
|
2019-07-16 11:50:05 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("note")
|
|
|
|
verbose_name_plural = _("notes")
|
|
|
|
|
2019-07-24 20:03:07 +00:00
|
|
|
def pretty(self):
|
|
|
|
"""
|
|
|
|
:return: Pretty name of this note
|
|
|
|
"""
|
2019-07-24 20:40:31 +00:00
|
|
|
return str(self)
|
2019-07-24 20:03:07 +00:00
|
|
|
|
|
|
|
pretty.short_description = _('Note')
|
|
|
|
|
2020-08-01 19:44:16 +00:00
|
|
|
@property
|
|
|
|
def last_negative_duration(self):
|
|
|
|
if self.balance >= 0 or self.last_negative is None:
|
|
|
|
return None
|
|
|
|
delta = timezone.now() - self.last_negative
|
|
|
|
return "{:d} jours".format(delta.days)
|
|
|
|
|
2019-07-24 20:03:07 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Save note with it's alias (called in polymorphic children)
|
|
|
|
"""
|
2020-09-01 13:51:47 +00:00
|
|
|
# Check that we can save the alias
|
|
|
|
self.clean()
|
2019-07-24 20:03:07 +00:00
|
|
|
|
2020-09-01 13:51:47 +00:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2020-09-05 11:52:03 +00:00
|
|
|
if not Alias.objects.filter(normalized_name=Alias.normalize(str(self))).exists():
|
2019-07-24 20:03:07 +00:00
|
|
|
a = Alias(name=str(self))
|
|
|
|
a.clean()
|
|
|
|
|
2020-09-01 13:51:47 +00:00
|
|
|
# Save alias
|
2019-07-24 20:03:07 +00:00
|
|
|
a.note = self
|
|
|
|
a.save(force_insert=True)
|
2020-09-05 13:41:47 +00:00
|
|
|
else:
|
|
|
|
# Check if the name of the note changed without changing the normalized form of the alias
|
|
|
|
alias = Alias.objects.get(normalized_name=Alias.normalize(str(self)))
|
|
|
|
if alias.name != str(self):
|
|
|
|
alias.name = str(self)
|
|
|
|
alias.save()
|
2019-07-24 20:03:07 +00:00
|
|
|
|
2019-07-24 20:40:31 +00:00
|
|
|
def clean(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Verify alias (simulate save)
|
|
|
|
"""
|
2020-02-18 11:31:15 +00:00
|
|
|
aliases = Alias.objects.filter(
|
|
|
|
normalized_name=Alias.normalize(str(self)))
|
2019-07-24 20:40:31 +00:00
|
|
|
if aliases.exists():
|
|
|
|
# Alias exists, so check if it is linked to this note
|
|
|
|
if aliases.first().note != self:
|
2020-02-23 12:46:25 +00:00
|
|
|
raise ValidationError(_('This alias is already taken.'),
|
2020-03-07 21:28:59 +00:00
|
|
|
code="same_alias", )
|
2019-07-24 20:40:31 +00:00
|
|
|
else:
|
|
|
|
# Alias does not exist yet, so check if it can exist
|
|
|
|
a = Alias(name=str(self))
|
|
|
|
a.clean()
|
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
|
|
|
|
class NoteUser(Note):
|
|
|
|
"""
|
2020-01-21 21:06:06 +00:00
|
|
|
A :model:`note.Note` associated to an unique :model:`auth.User`.
|
2019-07-16 10:43:23 +00:00
|
|
|
"""
|
|
|
|
user = models.OneToOneField(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='note',
|
2019-07-17 10:14:23 +00:00
|
|
|
verbose_name=_('user'),
|
2019-07-16 10:43:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("one's note")
|
|
|
|
verbose_name_plural = _("users note")
|
|
|
|
|
2019-07-17 09:17:50 +00:00
|
|
|
def __str__(self):
|
2019-07-24 20:03:07 +00:00
|
|
|
return str(self.user)
|
2019-07-17 09:17:50 +00:00
|
|
|
|
2019-07-24 20:03:07 +00:00
|
|
|
def pretty(self):
|
|
|
|
return _("%(user)s's note") % {'user': str(self.user)}
|
2019-07-19 13:00:44 +00:00
|
|
|
|
2020-08-01 19:44:16 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.pk and self.balance < 0:
|
|
|
|
old_note = NoteUser.objects.get(pk=self.pk)
|
2020-09-01 13:51:47 +00:00
|
|
|
super().save(*args, **kwargs)
|
2020-08-01 19:44:16 +00:00
|
|
|
if old_note.balance >= 0:
|
|
|
|
# Passage en négatif
|
|
|
|
self.last_negative = timezone.now()
|
2020-08-31 18:15:48 +00:00
|
|
|
self._force_save = True
|
|
|
|
self.save(*args, **kwargs)
|
2020-08-01 19:44:16 +00:00
|
|
|
self.send_mail_negative_balance()
|
2020-08-31 18:15:48 +00:00
|
|
|
else:
|
|
|
|
super().save(*args, **kwargs)
|
2020-08-01 19:44:16 +00:00
|
|
|
|
|
|
|
def send_mail_negative_balance(self):
|
|
|
|
plain_text = render_to_string("note/mails/negative_balance.txt", dict(note=self))
|
|
|
|
html = render_to_string("note/mails/negative_balance.html", dict(note=self))
|
|
|
|
self.user.email_user("[Note Kfet] Passage en négatif (compte n°{:d})"
|
|
|
|
.format(self.user.pk), plain_text, html_message=html)
|
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
|
|
|
|
class NoteClub(Note):
|
|
|
|
"""
|
2020-01-21 21:06:06 +00:00
|
|
|
A :model:`note.Note` associated to an unique :model:`member.Club`
|
2019-07-16 10:43:23 +00:00
|
|
|
"""
|
2019-07-16 12:38:52 +00:00
|
|
|
club = models.OneToOneField(
|
2019-07-16 10:43:23 +00:00
|
|
|
'member.Club',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='note',
|
2019-07-17 10:14:23 +00:00
|
|
|
verbose_name=_('club'),
|
2019-07-16 10:43:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("club note")
|
|
|
|
verbose_name_plural = _("clubs notes")
|
|
|
|
|
2019-07-17 10:14:23 +00:00
|
|
|
def __str__(self):
|
2019-07-24 20:03:07 +00:00
|
|
|
return str(self.club)
|
2019-07-17 10:14:23 +00:00
|
|
|
|
2019-07-24 20:03:07 +00:00
|
|
|
def pretty(self):
|
2020-01-21 21:06:06 +00:00
|
|
|
return _("Note of %(club)s club") % {'club': str(self.club)}
|
2019-07-19 13:00:44 +00:00
|
|
|
|
2020-08-31 14:13:26 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.pk and self.balance < 0:
|
|
|
|
old_note = NoteClub.objects.get(pk=self.pk)
|
2020-09-01 13:51:47 +00:00
|
|
|
super().save(*args, **kwargs)
|
2020-08-31 14:13:26 +00:00
|
|
|
if old_note.balance >= 0:
|
|
|
|
# Passage en négatif
|
|
|
|
self.last_negative = timezone.now()
|
2020-08-31 18:15:48 +00:00
|
|
|
self._force_save = True
|
|
|
|
self.save(*args, **kwargs)
|
2020-08-31 14:13:26 +00:00
|
|
|
self.send_mail_negative_balance()
|
2020-09-01 13:51:47 +00:00
|
|
|
else:
|
|
|
|
super().save(*args, **kwargs)
|
2020-08-31 14:13:26 +00:00
|
|
|
|
|
|
|
def send_mail_negative_balance(self):
|
|
|
|
plain_text = render_to_string("note/mails/negative_balance.txt", dict(note=self))
|
|
|
|
html = render_to_string("note/mails/negative_balance.html", dict(note=self))
|
2020-09-01 13:52:27 +00:00
|
|
|
send_mail("[Note Kfet] Passage en négatif (club {})".format(self.club.name), plain_text, DEFAULT_FROM_EMAIL,
|
|
|
|
[self.club.email], html_message=html)
|
2020-08-31 14:13:26 +00:00
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
|
|
|
|
class NoteSpecial(Note):
|
|
|
|
"""
|
2020-01-21 21:06:06 +00:00
|
|
|
A :model:`note.Note` for special accounts, where real money enter or leave the system
|
2019-07-16 10:43:23 +00:00
|
|
|
- bank check
|
|
|
|
- credit card
|
|
|
|
- bank transfer
|
|
|
|
- cash
|
|
|
|
- refund
|
2020-01-21 21:06:06 +00:00
|
|
|
This Type of Note is not associated to a :model:`auth.User` or :model:`member.Club` .
|
2019-07-16 10:43:23 +00:00
|
|
|
"""
|
|
|
|
special_type = models.CharField(
|
|
|
|
verbose_name=_('type'),
|
|
|
|
max_length=255,
|
|
|
|
unique=True,
|
|
|
|
)
|
|
|
|
|
2019-07-16 11:50:05 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("special note")
|
|
|
|
verbose_name_plural = _("special notes")
|
|
|
|
|
2019-07-17 10:14:23 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.special_type
|
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
|
|
|
|
class Alias(models.Model):
|
|
|
|
"""
|
2020-01-21 21:06:06 +00:00
|
|
|
points toward a :model:`note.NoteUser` or :model;`note.NoteClub` instance.
|
|
|
|
Alias are unique, but a :model:`note.NoteUser` or :model:`note.NoteClub` can
|
|
|
|
have multiples aliases.
|
|
|
|
|
|
|
|
Aliases name are also normalized, two differents :model:`note.Note` can not
|
|
|
|
have the same normalized alias, to avoid confusion when referring orally to
|
|
|
|
it.
|
2019-07-16 10:43:23 +00:00
|
|
|
"""
|
|
|
|
name = models.CharField(
|
|
|
|
verbose_name=_('name'),
|
|
|
|
max_length=255,
|
|
|
|
unique=True,
|
2019-07-23 00:13:09 +00:00
|
|
|
validators=[
|
|
|
|
RegexValidator(
|
|
|
|
regex=settings.ALIAS_VALIDATOR_REGEX,
|
2020-02-18 11:31:15 +00:00
|
|
|
message=_('Invalid alias'),
|
2019-07-23 00:13:09 +00:00
|
|
|
)
|
2020-02-18 11:31:15 +00:00
|
|
|
] if settings.ALIAS_VALIDATOR_REGEX else [],
|
2019-07-23 00:13:09 +00:00
|
|
|
)
|
|
|
|
normalized_name = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
unique=True,
|
2020-08-24 10:41:51 +00:00
|
|
|
blank=False,
|
2020-02-18 11:31:15 +00:00
|
|
|
editable=False,
|
2019-07-16 10:43:23 +00:00
|
|
|
)
|
|
|
|
note = models.ForeignKey(
|
|
|
|
Note,
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
)
|
2019-07-16 11:50:05 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("alias")
|
|
|
|
verbose_name_plural = _("aliases")
|
2020-03-11 12:46:47 +00:00
|
|
|
indexes = [
|
|
|
|
models.Index(fields=['name']),
|
|
|
|
models.Index(fields=['normalized_name']),
|
|
|
|
]
|
2019-07-17 07:37:11 +00:00
|
|
|
|
2019-07-17 11:03:10 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2019-07-23 00:13:09 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def normalize(string):
|
|
|
|
"""
|
2020-08-10 16:36:47 +00:00
|
|
|
Normalizes a string: removes most diacritics, does casefolding and ignore non-ASCII characters
|
2019-07-23 00:13:09 +00:00
|
|
|
"""
|
|
|
|
return ''.join(
|
2020-08-30 15:28:36 +00:00
|
|
|
char for char in unicodedata.normalize('NFKD', string.casefold().replace('æ', 'ae').replace('œ', 'oe'))
|
2019-07-24 20:03:07 +00:00
|
|
|
if all(not unicodedata.category(char).startswith(cat)
|
2020-08-30 15:28:36 +00:00
|
|
|
for cat in {'M', 'Pc', 'Pe', 'Pf', 'Pi', 'Po', 'Ps', 'Z', 'C'}))\
|
|
|
|
.casefold().encode('ascii', 'ignore').decode('ascii')
|
2019-07-23 00:13:09 +00:00
|
|
|
|
|
|
|
def clean(self):
|
2020-03-26 16:45:24 +00:00
|
|
|
normalized_name = self.normalize(self.name)
|
2019-07-23 00:13:09 +00:00
|
|
|
if len(normalized_name) >= 255:
|
2020-02-23 12:46:25 +00:00
|
|
|
raise ValidationError(_('Alias is too long.'),
|
|
|
|
code='alias_too_long')
|
2020-08-24 10:41:51 +00:00
|
|
|
if not normalized_name:
|
|
|
|
raise ValidationError(_('This alias contains only complex character. Please use a more simple alias.'))
|
2019-07-23 00:13:09 +00:00
|
|
|
try:
|
2020-02-23 12:46:25 +00:00
|
|
|
sim_alias = Alias.objects.get(normalized_name=normalized_name)
|
|
|
|
if self != sim_alias:
|
2020-03-11 11:05:29 +00:00
|
|
|
raise ValidationError(_('An alias with a similar name already exists: {} ').format(sim_alias),
|
2020-03-07 21:28:59 +00:00
|
|
|
code="same_alias"
|
|
|
|
)
|
2019-07-23 00:13:09 +00:00
|
|
|
except Alias.DoesNotExist:
|
|
|
|
pass
|
2020-03-01 12:42:22 +00:00
|
|
|
self.normalized_name = normalized_name
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-03-27 13:19:55 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2020-09-01 13:51:47 +00:00
|
|
|
self.clean()
|
2020-03-27 13:19:55 +00:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2020-02-17 10:21:05 +00:00
|
|
|
def delete(self, using=None, keep_parents=False):
|
|
|
|
if self.name == str(self.note):
|
2020-02-23 12:46:25 +00:00
|
|
|
raise ValidationError(_("You can't delete your main alias."),
|
2020-03-26 22:05:37 +00:00
|
|
|
code="main_alias")
|
2020-02-17 10:21:05 +00:00
|
|
|
return super().delete(using, keep_parents)
|