nk20/note/models/notes.py

133 lines
3.1 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
from django.conf import settings
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
2019-07-16 10:43:23 +00:00
from django.utils.translation import gettext_lazy as _
"""
Defines each note types
"""
class Note(models.Model):
"""
2019-07-16 11:50:05 +00:00
An model, use to add transactions capabilities
We do not use an abstract model to simplify the transfer between two notes.
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-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-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',
)
class Meta:
verbose_name = _("one's note")
verbose_name_plural = _("users note")
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',
)
class Meta:
verbose_name = _("club note")
verbose_name_plural = _("clubs notes")
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-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,
)
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")
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def save_user_note(instance, created, **_kwargs):
"""
Hook to create and save a note when an user is updated
"""
if created:
NoteUser.objects.create(user=instance)
instance.note.save()
@receiver(post_save, sender='member.Club')
def save_club_note(instance, created, **_kwargs):
"""
Hook to create and save a note when a club is updated
"""
if created:
NoteClub.objects.create(club=instance)
instance.note.save()