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
|
|
|
|
|
|
|
class School(models.Model):
|
2018-12-02 15:28:40 +00:00
|
|
|
"""
|
|
|
|
Stores school
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
verbose_name = "École"
|
|
|
|
|
2018-10-05 22:03:02 +00:00
|
|
|
name = models.CharField(max_length=255, verbose_name="Nom")
|
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
|
|
|
"""
|
|
|
|
Stores cotisations history, related to :model:`preferences.Cotisation`
|
|
|
|
"""
|
2018-11-22 21:52:15 +00:00
|
|
|
class Meta:
|
2018-12-02 15:28:40 +00:00
|
|
|
verbose_name = "Historique cotisation"
|
2018-11-22 21:52:15 +00:00
|
|
|
permissions = (
|
2018-12-02 15:28:40 +00:00
|
|
|
("validate_cotisationhistory", "Peut (in)valider les cotisations"),
|
2018-11-22 21:52:15 +00:00
|
|
|
)
|
|
|
|
|
2018-10-05 22:03:02 +00:00
|
|
|
WAITING = 0
|
|
|
|
VALID = 1
|
|
|
|
INVALID = 2
|
|
|
|
VALIDATION_CHOICES = (
|
|
|
|
(WAITING, 'En attente de validation'),
|
|
|
|
(VALID, 'Validée'),
|
|
|
|
(INVALID, 'Invalidée'),
|
|
|
|
)
|
|
|
|
user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="Client")
|
|
|
|
amount = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="Montant")
|
|
|
|
duration = models.PositiveIntegerField(verbose_name="Durée")
|
|
|
|
paymentDate = models.DateTimeField(auto_now_add=True, verbose_name="Date du paiement")
|
|
|
|
endDate = models.DateTimeField(verbose_name="Fin de la cotisation")
|
|
|
|
paymentMethod = models.ForeignKey(PaymentMethod, on_delete=models.PROTECT, verbose_name="Moyen de paiement")
|
|
|
|
cotisation = models.ForeignKey(Cotisation, on_delete=models.PROTECT, verbose_name="Type de cotisation")
|
|
|
|
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="cotisation_made")
|
|
|
|
valid = models.IntegerField(choices=VALIDATION_CHOICES, default=WAITING)
|
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
|
|
|
"""
|
|
|
|
Stores whitelist history
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
verbose_name = "Historique accès gracieux"
|
|
|
|
|
2018-08-31 12:46:35 +00:00
|
|
|
user = models.ForeignKey(User, on_delete=models.PROTECT)
|
|
|
|
paymentDate = models.DateTimeField(auto_now_add=True)
|
|
|
|
endDate = models.DateTimeField()
|
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")
|
|
|
|
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="whitelist_made")
|
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
|
|
|
"""
|
|
|
|
Stores user profile
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
verbose_name = "Profil"
|
|
|
|
|
2018-08-31 12:46:35 +00:00
|
|
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
2018-12-05 00:43:21 +00:00
|
|
|
credit = models.DecimalField(max_digits=7, decimal_places=2, default=0)
|
|
|
|
debit = models.DecimalField(max_digits=7, decimal_places=2, default=0)
|
2018-08-31 12:46:35 +00:00
|
|
|
school = models.ForeignKey(School, on_delete=models.PROTECT, blank=True, null=True)
|
|
|
|
cotisationEnd = models.DateTimeField(blank=True, null=True)
|
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
|
|
|
"""
|
|
|
|
Test if a user is adherent
|
|
|
|
"""
|
2018-11-25 12:52:32 +00:00
|
|
|
if(self.cotisationEnd and self.cotisationEnd > timezone.now()):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2018-08-31 12:46:35 +00:00
|
|
|
@property
|
2018-09-01 19:57:09 +00:00
|
|
|
def balance(self):
|
2018-12-02 15:28:40 +00:00
|
|
|
"""
|
|
|
|
Computes user balance
|
|
|
|
"""
|
2018-08-31 12:46:35 +00:00
|
|
|
return self.credit - self.debit
|
|
|
|
|
2018-09-01 19:57:09 +00:00
|
|
|
def positiveBalance(self):
|
2018-12-02 15:28:40 +00:00
|
|
|
"""
|
|
|
|
Test if the user balance is positive or null
|
|
|
|
"""
|
2018-10-05 22:03:02 +00:00
|
|
|
return self.balance >= 0
|
2018-08-31 12:46:35 +00:00
|
|
|
|
|
|
|
@property
|
2018-09-01 19:57:09 +00:00
|
|
|
def rank(self):
|
2018-12-02 15:28:40 +00:00
|
|
|
"""
|
|
|
|
Computes the rank (by debit) of the user
|
|
|
|
"""
|
2018-09-01 19:57:09 +00:00
|
|
|
return Profile.objects.filter(debit__gte=self.debit).count()
|
2018-08-31 12:46:35 +00:00
|
|
|
|
|
|
|
@property
|
2018-09-01 19:57:09 +00:00
|
|
|
def alcohol(self):
|
2018-12-02 15:28:40 +00:00
|
|
|
"""
|
|
|
|
Computes ingerated alcohol
|
|
|
|
"""
|
2018-11-22 21:52:15 +00:00
|
|
|
consumptions = ConsumptionHistory.objects.filter(customer=self.user).select_related('product')
|
|
|
|
alcohol = 0
|
|
|
|
for consumption in consumptions:
|
|
|
|
product = consumption.product
|
|
|
|
alcohol += consumption.quantity * float(product.deg) * product.volume * 0.79 /10 /1000
|
|
|
|
return alcohol
|
2018-08-31 12:46:35 +00:00
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
Tente de retourner l'attribut de l'instance et si l'attribut n'existe pas,
|
|
|
|
tente de retourner l'attribut de l'user associé à l'instance
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
r = super().__getattr__(name)
|
|
|
|
except AttributeError:
|
|
|
|
r = getattr(self.user, name)
|
|
|
|
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
|
|
|
"""
|
|
|
|
Create profile when user is created
|
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
Save profile when user is saved
|
|
|
|
"""
|
2018-11-22 21:52:15 +00:00
|
|
|
instance.profile.save()
|
|
|
|
|
|
|
|
def str_user(self):
|
2018-12-02 15:28:40 +00:00
|
|
|
"""
|
|
|
|
Rewrite str method for user
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
2018-12-02 15:28:40 +00:00
|
|
|
User.add_to_class("__str__", str_user)
|