nk20/adherents/models.py

111 lines
2.8 KiB
Python
Raw Normal View History

2019-07-08 10:55:48 +00:00
# -*- mode: python; coding: utf-8 -*-
2019-07-08 11:59:31 +00:00
# Copyright (C) 2018-2019 by BDE ENS Paris-Saclay
2019-07-08 10:55:48 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _
class Profile(models.Model):
"""
An user profile
We do not want to patch the Django Contrib Auth User class
so this model add an user profile with additional information.
"""
2019-07-08 14:00:02 +00:00
GENRES = [
(None, "ND"),
("M", "M"),
("F", "F"),
]
2019-07-08 10:55:48 +00:00
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
2019-07-08 14:00:02 +00:00
avatar = models.ImageField(
2019-07-08 10:55:48 +00:00
max_length=255,
2019-07-08 14:00:02 +00:00
blank=True,
verbose_name=_('profile picture')
)
phone_number = models.CharField(
max_length=50,
blank=True,
null=False,
default='',
2019-07-08 10:55:48 +00:00
verbose_name=_('phone number'),
)
section = models.CharField(
max_length=255,
verbose_name=_('section'),
help_text=_('e.g. "1A0", "9A♥", "SAPHIRE"'),
)
2019-07-08 14:00:02 +00:00
genre = models.CharField(max_length=1,
blank=False,
null=False,
choices=GENRES
)
address = models.TextField(
blank=True,
null=False,
default=''
)
remunere = models.BooleanField(verbose_name=_("rémunéré"),
default=False,
)
is_active = models.BooleanField(verbose_name=_("compte actif"),
default=True
)
is_deleted = models.BooleanField(verbose_name=_("compte supprimé"),
default=False
)
2019-07-08 10:55:48 +00:00
class Meta:
verbose_name = _('user profile')
verbose_name_plural = _('user profile')
def __str__(self):
return self.user.get_username()
class MembershipFee(models.Model):
"""
2019-07-08 11:51:15 +00:00
User can become member by paying a membership fee
2019-07-08 10:55:48 +00:00
"""
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
)
2019-07-08 11:51:15 +00:00
date = models.DateField(
2019-07-08 10:55:48 +00:00
max_length=255,
2019-07-08 11:51:15 +00:00
verbose_name=_('date'),
2019-07-08 10:55:48 +00:00
)
2019-07-08 11:51:15 +00:00
amount = models.DecimalField(
max_digits=5, # Max 999.99 €
decimal_places=2,
verbose_name=_('amount'),
2019-07-08 10:55:48 +00:00
)
class Meta:
2019-07-08 11:51:15 +00:00
verbose_name = _('membership fee')
verbose_name_plural = _('membership fees')
2019-07-08 10:55:48 +00:00
def __str__(self):
return self.user.get_username()
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, created, **_kwargs):
"""
Hook to save an user profile when an user is updated
"""
if created:
Profile.objects.create(user=instance)
instance.profile.save()