nk20/note/models.py

96 lines
2.3 KiB
Python
Raw Normal View History

2019-07-08 13:24:19 +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.contrib.auth.models import User
2019-07-08 12:30:58 +00:00
from django.db import models
2019-07-08 13:29:03 +00:00
from django.utils.translation import gettext_lazy as _
2019-07-08 13:24:19 +00:00
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
class Alias(models.Model):
"""
A alias labels a Note instance, only for user and clubs
"""
alias = models.TextField(
"alias",
2019-07-16 07:05:30 +00:00
unique=True,
blank=False,
null=False,
2019-07-08 13:24:19 +00:00
)
2019-07-16 07:05:30 +00:00
# Owner can be linked to an user note or a club note
limit = models.Q(app_label="note", model="NoteUser") | models.Q(app_label="note", model="NoteClub")
2019-07-08 13:24:19 +00:00
owner_id = models.PositiveIntegerField()
2019-07-16 07:05:30 +00:00
owner_type = models.ForeignKey(
ContentType,
on_delete=models.CASCADE,
limit_choices_to=limit
)
owner = GenericForeignKey('owner_type', 'owner_id')
2019-07-08 13:24:19 +00:00
class Note(models.Model):
"""
An abstract model, use to add transactions capabilities to a user
"""
2019-07-16 07:05:30 +00:00
balance = models.DecimalField(
verbose_name=_('account balance'),
help_text=_("money credited for this instance"),
decimal_places=2, # Limit to centimes
2019-07-08 13:24:19 +00:00
)
2019-07-16 07:05:30 +00:00
is_active = models.BooleanField(
default=True,
verbose_name=_('is active')
2019-07-08 13:24:19 +00:00
)
class Meta:
abstract = True
2019-07-16 07:05:30 +00:00
2019-07-08 13:24:19 +00:00
class NoteUser(Note):
"""
2019-07-16 07:05:30 +00:00
A Note associated to an User
2019-07-08 13:24:19 +00:00
"""
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
2019-07-16 07:05:30 +00:00
2019-07-08 13:24:19 +00:00
class Meta:
2019-07-16 07:05:30 +00:00
verbose_name = _("one's note")
verbose_name_plural = _("users note")
2019-07-08 13:24:19 +00:00
def __str__(self):
return self.user.get_username()
class NoteSpec(Note):
"""
A Note for special Account, where real money enter or leave the system.
- Cash
- Credit Card
- Bank Transfert
- Bank Check
- Refund
"""
2019-07-08 13:26:32 +00:00
account_type = models.CharField(
2019-07-16 07:05:30 +00:00
max_length=2,
choices=(
("CH", "chèques"),
("CB", "Carte Bancaire"),
("VB", "Virement Bancaire"),
("CA", "Cash"),
("RB", "Remboursement")
2019-07-08 13:24:19 +00:00
),
2019-07-16 07:05:30 +00:00
unique=True,
2019-07-08 13:24:19 +00:00
)
2019-07-16 07:05:30 +00:00
2019-07-08 13:24:19 +00:00
class NoteClub(Note):
2019-07-16 07:05:30 +00:00
# to be added
2019-07-08 13:24:19 +00:00
pass