import datetime import math import time from django.core.validators import MinValueValidator, MaxValueValidator from django.db import models from django.conf import settings from django.utils import timezone from django.contrib import admin #test BASES_CHOICES = [ ('1', 'Monthey'), ('2', 'Uvrier'), ] class Collabs_hour_types(models.Model): name = models.CharField("Dénomination", max_length=100) dtUpdate = models.DateTimeField('Date de modification', auto_now=True) dtCreated = models.DateTimeField('Date de création', auto_now_add=True) def __str__(self): return self.name class Meta: verbose_name = "type" verbose_name_plural = "types" class Collabs_hour(models.Model): userName = models.CharField("Auteur", max_length=100) user = models.ForeignKey(settings.AUTH_USER_MODEL, limit_choices_to={'groups__name': "Intervenants"}, verbose_name="Collaborateur", on_delete=models.DO_NOTHING) nHour = models.PositiveIntegerField("Heures", default=0, validators=[MinValueValidator(0), MaxValueValidator(23)]) nMinutes = models.PositiveIntegerField("Minutes", default=0, validators=[MinValueValidator(0), MaxValueValidator(60)]) sRemarques = models.TextField("Remarques", blank=True) bNoticed = models.BooleanField("Vérifiée", blank=True, default=False) dtDate = models.DateField('Date', blank=False) sBases = models.CharField('Employé de la base de', max_length=1, choices=BASES_CHOICES,default=1) type = models.ForeignKey(Collabs_hour_types, verbose_name="Types", on_delete=models.DO_NOTHING) dtUpdate = models.DateTimeField('Date de modification', auto_now=True) dtCreated = models.DateTimeField('Date de création', auto_now_add=True) @admin.display( description='Total du mois') def get_total_hour_by_user(self): #objs = Collabs_hour.objects.filter(user_id=self.user_id,dtCreated__year=self.dtCreated.year, dtCreated__month=self.dtCreated.month) objs = Collabs_hour.objects.filter(user_id=self.user_id, dtDate__year=self.dtDate.year , dtDate__month=self.dtDate.month) # print(f"user {self.user} dtCreated__year {self.dtDate.year} dtCreated__month {self.dtDate.month}") #print( Collabs_hour.objects.all()) total = 0.0 for object in objs: total += object.nHour + object.nMinutes/60 # print(f"hour = {object.nHour} minutes = {object.nMinutes} = {object.nMinutes/60}") if total > 0.0: hours = int(total) minutes = (total*60) % 60 ret_string = ("%d:%02d" % (hours, minutes)) #ret_string = str(int(math.floor(total))) + ':' + str(int((total%(math.floor(total)))*60)) + f" ({total})" #ret_string = time.strftime("%H:%M", time.gmtime(total)) else: ret_string = "0" return ret_string def get_bases_name(self): return BASES_CHOICES[int(self.sBases)-1][1] @admin.display(description='Présence de remarques', boolean=True) def has_remarques(self): ret = False if self.sRemarques != None and self.sRemarques != '': ret = True return ret def __str__(self): return f"Heure supplémentaire: {self.user} : {self.dtCreated.strftime('%d.%m.%Y')} " class Meta: verbose_name = "heure supplémentaire" verbose_name_plural = "heures supplémentaires" permissions = ( ("can_notice", "Peut noter comme traitée"), )