1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-11-27 10:53:02 +00:00
nk20/apps/note/models/notes.py

225 lines
5.9 KiB
Python
Raw Normal View History

2019-07-16 10:43:23 +00:00
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2018-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
import unicodedata
2019-07-24 20:03:07 +00:00
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
2019-07-16 10:43:23 +00:00
from django.db import models
from django.utils.translation import gettext_lazy as _
2019-07-17 09:17:50 +00:00
from polymorphic.models import PolymorphicModel
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
"""
2019-07-16 11:50:05 +00:00
An model, use to add transactions capabilities
2019-07-16 10:43:23 +00:00
"""
balance = models.IntegerField(
verbose_name=_('account balance'),
help_text=_('in centimes, money credited for this instance'),
default=0,
2019-07-16 10:43:23 +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.'
),
)
2019-07-16 13:22:38 +00:00
display_image = models.ImageField(
verbose_name=_('display image'),
max_length=255,
blank=True,
)
2019-07-17 10:14:23 +00:00
created_at = models.DateTimeField(
verbose_name=_('created at'),
auto_now_add=True,
)
2019-07-16 10:43:23 +00:00
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')
def save(self, *args, **kwargs):
"""
Save note with it's alias (called in polymorphic children)
"""
aliases = Alias.objects.filter(name=str(self))
if aliases.exists():
# Alias exists, so check if it is linked to this note
if aliases.first().note != self:
2019-07-24 20:40:31 +00:00
raise ValidationError(_('This alias is already taken.'))
2019-07-24 20:03:07 +00:00
# Save note
super().save(*args, **kwargs)
else:
# Alias does not exist yet, so check if it can exist
a = Alias(name=str(self))
a.clean()
# Save note and alias
super().save(*args, **kwargs)
a.note = self
a.save(force_insert=True)
2019-07-24 20:40:31 +00:00
def clean(self, *args, **kwargs):
"""
Verify alias (simulate save)
"""
aliases = Alias.objects.filter(name=str(self))
if aliases.exists():
# Alias exists, so check if it is linked to this note
if aliases.first().note != self:
raise ValidationError(_('This alias is already taken.'))
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):
"""
A Note associated to an User
"""
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
2019-07-16 10:43:23 +00:00
class NoteClub(Note):
"""
A Note associated to a Club
"""
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):
return _("Note for %(club)s club") % {'club': str(self.club)}
2019-07-19 13:00:44 +00:00
2019-07-16 10:43:23 +00:00
class NoteSpecial(Note):
"""
A Note for special account, where real money enter or leave the system
- bank check
- credit card
- bank transfer
- cash
- refund
"""
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):
"""
An alias labels a Note instance, only for user and clubs
"""
name = models.CharField(
verbose_name=_('name'),
max_length=255,
unique=True,
validators=[
RegexValidator(
regex=settings.ALIAS_VALIDATOR_REGEX,
message=_('Invalid alias')
)
] if settings.ALIAS_VALIDATOR_REGEX else []
)
normalized_name = models.CharField(
max_length=255,
unique=True,
default='',
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")
2019-07-17 11:03:10 +00:00
def __str__(self):
return self.name
@staticmethod
def normalize(string):
"""
Normalizes a string: removes most diacritics and does casefolding
"""
return ''.join(
char
for char in unicodedata.normalize('NFKD', string.casefold())
2019-07-24 20:03:07 +00:00
if all(not unicodedata.category(char).startswith(cat)
for cat in {'M', 'P', 'Z', 'C'})
)
def save(self, *args, **kwargs):
"""
Handle normalized_name
"""
self.normalized_name = Alias.normalize(self.name)
2019-07-24 20:03:07 +00:00
if len(self.normalized_name) < 256:
super().save(*args, **kwargs)
def clean(self):
normalized_name = Alias.normalize(self.name)
if len(normalized_name) >= 255:
raise ValidationError(_('Alias too long.'))
try:
if self != Alias.objects.get(normalized_name=normalized_name):
2019-07-24 20:03:07 +00:00
raise ValidationError(_('An alias with a similar name '
'already exists.'))
except Alias.DoesNotExist:
pass