nk20/apps/member/models.py

183 lines
4.7 KiB
Python
Raw Normal View History

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-09-18 12:26:42 +00:00
import datetime
2019-07-16 10:43:23 +00:00
from django.conf import settings
from django.db import models
2019-08-15 19:49:32 +00:00
from django.urls import reverse, reverse_lazy
2020-03-07 21:28:59 +00:00
from django.utils.translation import gettext_lazy as _
2019-07-16 10:43:23 +00:00
2019-09-18 12:26:42 +00:00
2019-07-16 10:43:23 +00:00
class Profile(models.Model):
"""
An user profile
We do not want to patch the Django Contrib :model:`auth.User`model;
2019-07-16 10:43:23 +00:00
so this model add an user profile with additional information.
2019-07-16 10:43:23 +00:00
"""
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
phone_number = models.CharField(
verbose_name=_('phone number'),
max_length=50,
blank=True,
null=True,
)
section = models.CharField(
verbose_name=_('section'),
help_text=_('e.g. "1A0", "9A♥", "SAPHIRE"'),
max_length=255,
2019-07-16 11:54:32 +00:00
blank=True,
null=True,
2019-07-16 10:43:23 +00:00
)
address = models.CharField(
verbose_name=_('address'),
max_length=255,
blank=True,
null=True,
)
paid = models.BooleanField(
verbose_name=_("paid"),
default=False,
)
class Meta:
verbose_name = _('user profile')
verbose_name_plural = _('user profile')
2020-03-11 16:10:16 +00:00
indexes = [models.Index(fields=['user'])]
2019-07-16 10:43:23 +00:00
2019-08-11 22:30:29 +00:00
def get_absolute_url(self):
2020-03-07 21:28:59 +00:00
return reverse('user_detail', args=(self.pk,))
2020-02-18 11:31:15 +00:00
2019-07-16 10:43:23 +00:00
class Club(models.Model):
"""
A club is a group of people, whose membership is handle by their
:model:`member.Membership`, and gives access to right defined by a :model:`member.Role`.
2019-07-16 10:43:23 +00:00
"""
name = models.CharField(
2019-07-16 13:17:57 +00:00
verbose_name=_('name'),
2019-07-16 10:43:23 +00:00
max_length=255,
2019-07-16 13:17:57 +00:00
unique=True,
2019-07-16 10:43:23 +00:00
)
email = models.EmailField(
verbose_name=_('email'),
)
# Memberships
membership_fee = models.PositiveIntegerField(
verbose_name=_('membership fee'),
)
membership_duration = models.DurationField(
null=True,
verbose_name=_('membership duration'),
2019-07-16 10:59:11 +00:00
help_text=_('The longest time a membership can last '
'(NULL = infinite).'),
2019-07-16 10:43:23 +00:00
)
membership_start = models.DurationField(
null=True,
verbose_name=_('membership start'),
2019-07-16 10:59:11 +00:00
help_text=_('How long after January 1st the members can renew '
'their membership.'),
2019-07-16 10:43:23 +00:00
)
membership_end = models.DurationField(
null=True,
verbose_name=_('membership end'),
2019-07-16 10:59:11 +00:00
help_text=_('How long the membership can last after January 1st '
'of the next year after members can renew their '
'membership.'),
2019-07-16 10:43:23 +00:00
)
2019-07-16 11:50:05 +00:00
class Meta:
verbose_name = _("club")
verbose_name_plural = _("clubs")
2019-07-16 13:17:57 +00:00
def __str__(self):
return self.name
2019-08-11 21:25:27 +00:00
def get_absolute_url(self):
2020-03-07 21:28:59 +00:00
return reverse_lazy('member:club_detail', args=(self.pk,))
2019-08-11 21:25:27 +00:00
2019-07-16 10:43:23 +00:00
class Role(models.Model):
"""
Role that an :model:`auth.User` can have in a :model:`member.Club`
TODO: Integrate the right management, and create some standard Roles at the
creation of the club.
2019-07-16 10:43:23 +00:00
"""
name = models.CharField(
verbose_name=_('name'),
max_length=255,
2019-07-16 13:17:57 +00:00
unique=True,
2019-07-16 10:43:23 +00:00
)
class Meta:
verbose_name = _('role')
verbose_name_plural = _('roles')
2020-02-04 00:18:03 +00:00
def __str__(self):
return str(self.name)
2019-07-16 10:43:23 +00:00
class Membership(models.Model):
2019-08-11 15:52:41 +00:00
"""
Register the membership of a user to a club, including roles and membership duration.
2019-08-11 15:52:41 +00:00
"""
2019-07-16 10:43:23 +00:00
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
2020-02-18 11:31:15 +00:00
on_delete=models.PROTECT,
2019-07-16 10:43:23 +00:00
)
club = models.ForeignKey(
Club,
2020-02-18 11:31:15 +00:00
on_delete=models.PROTECT,
2019-07-16 10:43:23 +00:00
)
roles = models.ForeignKey(
Role,
2020-02-18 11:31:15 +00:00
on_delete=models.PROTECT,
2019-07-16 10:43:23 +00:00
)
date_start = models.DateField(
verbose_name=_('membership starts on'),
)
date_end = models.DateField(
verbose_name=_('membership ends on'),
null=True,
)
fee = models.PositiveIntegerField(
verbose_name=_('fee'),
)
2019-09-18 12:26:42 +00:00
def valid(self):
2019-09-18 14:41:01 +00:00
if self.date_end is not None:
2020-03-07 12:12:17 +00:00
return self.date_start.toordinal() <= datetime.datetime.now().toordinal() < self.date_end.toordinal()
2019-09-18 14:41:01 +00:00
else:
2020-03-07 12:12:17 +00:00
return self.date_start.toordinal() <= datetime.datetime.now().toordinal()
2019-09-18 12:26:42 +00:00
2019-07-16 10:43:23 +00:00
class Meta:
verbose_name = _('membership')
verbose_name_plural = _('memberships')
2020-03-11 16:10:16 +00:00
indexes = [models.Index(fields=['user'])]
2019-07-16 10:43:23 +00:00
2020-03-20 01:14:43 +00:00
2019-09-18 12:26:42 +00:00
class RolePermissions(models.Model):
"""
Permissions associated with a Role
"""
role = models.ForeignKey(
Role,
on_delete=models.PROTECT,
related_name='+',
verbose_name=_('role'),
)
permissions = models.ManyToManyField(
'permission.Permission'
)
def __str__(self):
return str(self.role)