med/users/models.py

114 lines
3.3 KiB
Python
Raw Normal View History

2019-08-02 12:57:53 +00:00
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
import datetime
2019-08-02 12:57:53 +00:00
import uuid
2019-08-08 10:16:40 +00:00
from django.contrib.auth.models import AbstractUser
2019-08-02 12:57:53 +00:00
from django.db import models
2019-08-08 10:16:40 +00:00
from django.utils import timezone
2019-08-02 12:57:53 +00:00
from django.utils.translation import gettext_lazy as _
from med.settings import MAX_EMPRUNT, REQ_EXPIRE_HRS
2019-08-08 10:16:40 +00:00
class User(AbstractUser):
telephone = models.CharField(
verbose_name=_('phone number'),
max_length=15,
null=True,
blank=True,
2019-08-02 19:35:30 +00:00
)
2019-08-08 10:16:40 +00:00
address = models.CharField(
verbose_name=_('address'),
max_length=255,
null=True,
blank=True,
2019-08-02 16:37:54 +00:00
)
2019-08-08 10:16:40 +00:00
maxemprunt = models.IntegerField(
verbose_name=_('maximum borrowed'),
help_text=_('Maximal amount of simultaneous borrowed item authorized.'),
default=MAX_EMPRUNT,
)
comment = models.CharField(
verbose_name=_('comment'),
help_text=_('Promotion...'),
max_length=255,
blank=True,
2019-08-02 16:37:54 +00:00
)
2019-08-08 10:16:40 +00:00
REQUIRED_FIELDS = ['first_name', 'last_name', 'email']
def has_perms(self, perms, obj=None):
for perm in perms:
2017-06-30 01:25:07 +00:00
try:
Right.objects.get(user=self, right__listright=perm)
return True
except Right.DoesNotExist:
return False
@property
def is_adherent(self):
2019-08-02 19:35:30 +00:00
last_adh_year = Adhesion.objects.all().order_by('annee_debut').reverse().first()
return last_adh_year and self in last_adh_year.adherent.all()
class Request(models.Model):
PASSWD = 'PW'
EMAIL = 'EM'
TYPE_CHOICES = (
(PASSWD, 'Mot de passe'),
(EMAIL, 'Email'),
)
type = models.CharField(max_length=2, choices=TYPE_CHOICES)
token = models.CharField(max_length=32)
user = models.ForeignKey('User', on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
expires_at = models.DateTimeField()
def save(self):
if not self.expires_at:
self.expires_at = timezone.now() \
2019-08-02 12:57:53 +00:00
+ datetime.timedelta(hours=REQ_EXPIRE_HRS)
if not self.token:
self.token = str(uuid.uuid4()).replace('-', '') # remove hyphens
2019-08-08 10:16:40 +00:00
super().save()
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
class Right(models.Model):
PRETTY_NAME = "Droits affectés à des users"
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
user = models.ForeignKey('User', on_delete=models.PROTECT)
right = models.ForeignKey('ListRight', on_delete=models.PROTECT)
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
class Meta:
2019-08-02 12:57:53 +00:00
verbose_name = _("right")
verbose_name_plural = _("rights")
2017-06-30 01:25:07 +00:00
unique_together = ("user", "right")
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
def __str__(self):
return str(self.user)
2017-06-30 01:25:07 +00:00
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
class ListRight(models.Model):
PRETTY_NAME = "Liste des droits existants"
listright = models.CharField(max_length=255, unique=True)
details = models.CharField(help_text="Description", max_length=255, blank=True)
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
def __str__(self):
return self.listright
2019-08-02 12:57:53 +00:00
2017-07-03 18:06:21 +00:00
class Clef(models.Model):
nom = models.CharField(max_length=255, unique=True)
proprio = models.ForeignKey('User', on_delete=models.PROTECT, blank=True, null=True)
commentaire = models.CharField(max_length=255, null=True, blank=True)
2019-08-02 12:57:53 +00:00
class Adhesion(models.Model):
annee_debut = models.IntegerField(unique=True)
annee_fin = models.IntegerField(unique=True)
adherent = models.ManyToManyField('User', blank=True)