3
0
Fork 0
mirror of https://github.com/nanoy42/coope synced 2024-05-09 10:42:24 +00:00
coope/users/models.py

263 lines
8.5 KiB
Python
Raw Permalink Normal View History

2018-08-31 12:46:35 +00:00
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
2018-11-25 12:52:32 +00:00
from django.utils import timezone
2018-11-27 08:07:12 +00:00
from simple_history.models import HistoricalRecords
2018-10-05 22:03:02 +00:00
from preferences.models import PaymentMethod, Cotisation
2018-11-22 21:52:15 +00:00
from gestion.models import ConsumptionHistory
2018-08-31 12:46:35 +00:00
2019-09-23 12:45:52 +00:00
2018-08-31 12:46:35 +00:00
class School(models.Model):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Stores school.
2018-12-02 15:28:40 +00:00
"""
class Meta:
verbose_name = "École"
2018-10-05 22:03:02 +00:00
name = models.CharField(max_length=255, verbose_name="Nom")
2019-02-28 12:18:41 +00:00
"""
The name of the school
"""
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-08-31 12:46:35 +00:00
def __str__(self):
return self.name
2018-10-05 22:03:02 +00:00
class CotisationHistory(models.Model):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Stores cotisation histories, related to :class:`preferences.models.Cotisation`.
2018-12-02 15:28:40 +00:00
"""
2018-11-22 21:52:15 +00:00
class Meta:
2018-12-02 15:28:40 +00:00
verbose_name = "Historique cotisation"
2018-10-05 22:03:02 +00:00
user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="Client")
2019-02-28 12:18:41 +00:00
"""
Client (:class:`django.contrib.auth.models.User`).
"""
2018-10-05 22:03:02 +00:00
amount = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="Montant")
2019-02-28 12:18:41 +00:00
"""
Price, in euros, of the cotisation.
"""
2018-10-05 22:03:02 +00:00
duration = models.PositiveIntegerField(verbose_name="Durée")
2019-02-28 12:18:41 +00:00
"""
Duration, in days, of the cotisation.
"""
2018-10-05 22:03:02 +00:00
paymentDate = models.DateTimeField(auto_now_add=True, verbose_name="Date du paiement")
2019-02-28 12:18:41 +00:00
"""
Date of the payment.
"""
2018-10-05 22:03:02 +00:00
endDate = models.DateTimeField(verbose_name="Fin de la cotisation")
2019-02-28 12:18:41 +00:00
"""
End date of the cotisation.
"""
2018-10-05 22:03:02 +00:00
paymentMethod = models.ForeignKey(PaymentMethod, on_delete=models.PROTECT, verbose_name="Moyen de paiement")
2019-02-28 12:18:41 +00:00
"""
:class:`Payment method <preferences.models.PaymentMethod>` used.
"""
2018-10-05 22:03:02 +00:00
cotisation = models.ForeignKey(Cotisation, on_delete=models.PROTECT, verbose_name="Type de cotisation")
2019-02-28 12:18:41 +00:00
"""
:class:`~preferences.models.Cotisation` related.
"""
2018-10-05 22:03:02 +00:00
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="cotisation_made")
2019-02-28 12:18:41 +00:00
"""
User (:class:`django.contrib.auth.models.User`) who registered the cotisation.
"""
2019-06-23 08:54:21 +00:00
divided = models.BooleanField(default=False, verbose_name="Répartition")
"""
True if money of cotisation have been divided between CTM and PTM
"""
2019-06-27 21:03:19 +00:00
amount_ptm = models.DecimalField(max_digits=5, decimal_places=2, null=True, verbose_name="Montant pour le club Phœnix Technopôle Metz", default=0)
2019-06-23 08:54:21 +00:00
"""
Amount of money given to the PTM club
"""
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-10-05 22:03:02 +00:00
class WhiteListHistory(models.Model):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Stores whitelist history.
2018-12-02 15:28:40 +00:00
"""
class Meta:
verbose_name = "Historique accès gracieux"
2019-02-27 07:59:41 +00:00
verbose_name_plural = "Historique accès gracieux"
2018-12-02 15:28:40 +00:00
2019-02-27 07:59:41 +00:00
user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="Client")
2019-02-28 12:18:41 +00:00
"""
Client (:class:`django.contrib.auth.models.User`).
"""
2019-02-27 07:59:41 +00:00
paymentDate = models.DateTimeField(auto_now_add=True, verbose_name="Date de début")
2019-02-28 12:18:41 +00:00
"""
Date of the beginning of the whitelist.
"""
2019-02-27 07:59:41 +00:00
endDate = models.DateTimeField(verbose_name="Date de fin")
2019-02-28 12:18:41 +00:00
"""
End date of the whitelist.
"""
2018-10-05 22:03:02 +00:00
duration = models.PositiveIntegerField(verbose_name="Durée", help_text="Durée de l'accès gracieux en jour")
2019-02-28 12:18:41 +00:00
"""
Duration, in days, of the whitelist
"""
2018-10-05 22:03:02 +00:00
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="whitelist_made")
2019-02-28 12:18:41 +00:00
"""
User (:class:`django.contrib.auth.models.User`) who registered the cotisation.
"""
2019-06-23 09:06:39 +00:00
reason = models.CharField(max_length=255, verbose_name="Raison", blank=True)
"""
Reason of the whitelist
"""
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-08-31 12:46:35 +00:00
class Profile(models.Model):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Stores user profile.
2018-12-02 15:28:40 +00:00
"""
class Meta:
verbose_name = "Profil"
2019-09-24 23:25:27 +00:00
permissions = (
('can_generate_invoices', 'Can generate invocies'),
('can_change_user_perm', 'Can change user groups'),
)
2018-12-02 15:28:40 +00:00
2019-02-27 07:59:41 +00:00
user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="Utilisateur")
2019-02-28 12:18:41 +00:00
"""
Client (:class:`django.contrib.auth.models.User`).
"""
2019-02-27 07:59:41 +00:00
credit = models.DecimalField(max_digits=7, decimal_places=2, default=0, verbose_name="Crédit")
2019-02-28 12:18:41 +00:00
"""
Amount of money, in euros, put on the account
"""
2019-02-27 07:59:41 +00:00
debit = models.DecimalField(max_digits=7, decimal_places=2, default=0, verbose_name="Débit")
2019-02-28 12:18:41 +00:00
"""
2019-06-23 15:11:58 +00:00
Amount of money, in euros, spent from the account
"""
direct_debit = models.DecimalField(max_digits=7, decimal_places=2, default=0, verbose_name="Débit (non compte)")
"""
Amount of money, in euro, spent with other mean than the account
2019-02-28 12:18:41 +00:00
"""
2019-02-27 07:59:41 +00:00
school = models.ForeignKey(School, on_delete=models.PROTECT, blank=True, null=True, verbose_name="École")
2019-02-28 12:18:41 +00:00
"""
:class:`~users.models.School` of the client
"""
2019-02-27 07:59:41 +00:00
cotisationEnd = models.DateTimeField(blank=True, null=True, verbose_name="Fin de cotisation")
2019-02-28 12:18:41 +00:00
"""
Date of end of cotisation for the client
"""
2019-06-10 23:28:22 +00:00
alcohol = models.DecimalField(max_digits=5, decimal_places=2, default=0, null=True)
"""
Ingerated alcohol
"""
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-08-31 12:46:35 +00:00
2018-11-25 12:52:32 +00:00
@property
def is_adherent(self):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Test if a client is adherent.
2018-12-02 15:28:40 +00:00
"""
2019-10-06 09:44:32 +00:00
banishments = self.user.banishmenthistory_set.order_by("-end_date")
if banishments and banishments[0].end_date > timezone.now():
return False
2019-02-21 19:08:29 +00:00
if(self.cotisationEnd and self.cotisationEnd > timezone.now()):
2018-11-25 12:52:32 +00:00
return True
else:
return False
2018-08-31 12:46:35 +00:00
@property
def balance(self):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Computes client balance (:attr:`gestion.models.Profile.credit` - :attr:`gestion.models.Profile.debit`).
2018-12-02 15:28:40 +00:00
"""
2018-08-31 12:46:35 +00:00
return self.credit - self.debit
def positiveBalance(self):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Test if the client balance is positive or null.
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
return self.balance >= 0
2018-08-31 12:46:35 +00:00
@property
def rank(self):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Computes the rank (by :attr:`gestion.models.Profile.debit`) of the client.
2018-12-02 15:28:40 +00:00
"""
return Profile.objects.filter(debit__gte=self.debit).count()
2018-08-31 12:46:35 +00:00
@property
def nb_pintes(self):
"""
2019-02-28 12:18:41 +00:00
Return the number of :class:`Pinte(s) <gestion.models.Pinte>` currently owned.
"""
return self.user.pintes_owned_currently.count()
2018-10-05 22:03:02 +00:00
def __str__(self):
return str(self.user)
2018-12-18 18:01:09 +00:00
def __getattr__(self, name):
"""
2019-02-28 12:18:41 +00:00
Try to return the attribute and it doesn't exist, try to return the attribute of the associated user (:class:`django.contrib.auth.models.User`).
2018-12-18 18:01:09 +00:00
"""
try:
r = self.__getattribute__(name)
2018-12-18 18:01:09 +00:00
except AttributeError:
try:
r = super().__getattr__(name)
except AttributeError:
r = getattr(self.user, name)
2018-12-18 18:01:09 +00:00
return r
2018-08-31 12:46:35 +00:00
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Create profile when user (:class:`django.contrib.auth.models.User`) is created.
2018-12-02 15:28:40 +00:00
"""
2018-08-31 12:46:35 +00:00
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Save profile when user (:class:`django.contrib.auth.models.User`) is saved.
2018-12-02 15:28:40 +00:00
"""
2018-11-22 21:52:15 +00:00
instance.profile.save()
def str_user(self):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Rewrite str method for user (:class:`django.contrib.auth.models.User`).
2018-12-02 15:28:40 +00:00
"""
if self.profile.is_adherent:
2018-11-27 08:07:12 +00:00
fin = "Adhérent"
else:
fin = "Non adhérent"
return self.username + " (" + self.first_name + " " + self.last_name + ", " + str(self.profile.balance) + "€, " + fin + ")"
2018-11-22 21:52:15 +00:00
2019-01-05 23:01:30 +00:00
2019-10-06 09:44:32 +00:00
User.add_to_class("__str__", str_user)
class BanishmentHistory(models.Model):
"""
Stores banishment history.
"""
class Meta:
verbose_name = "Historique banissement"
verbose_name_plural = "Historique banissements"
user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="Client")
"""
Client (:class:`django.contrib.auth.models.User`).
"""
ban_date = models.DateTimeField(auto_now_add=True, verbose_name="Date du banissement")
"""
Date of the beginning of the whitelist.
"""
end_date = models.DateTimeField(verbose_name="Date de fin")
"""
End date of the whitelist.
"""
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="banishment_made")
"""
User (:class:`django.contrib.auth.models.User`) who registered the cotisation.
"""
reason = models.CharField(max_length=255, verbose_name="Raison", blank=True)
"""
Reason of the whitelist
"""
history = HistoricalRecords()