from django.db import models
import datetime
from azienda.models import *
from documento.models import *
from django.utils import timezone

class Utente(models.Model):
    codicefiscale = models.CharField(max_length=16,null=False)
    pin = models.CharField(max_length=10,null=False,default="")
    nome = models.CharField(max_length=128,null=False,default="")
    datanascita = models.DateField(null=True)
    luogonascita = models.CharField(max_length=256,null=False,default="")
    inserimento = models.DateField(null=False,default=timezone.now)
    mail = models.CharField(max_length=128,null=False,default="")
    azienda = models.ForeignKey(Azienda,on_delete=models.PROTECT)
    @property
    def documenti(self):
      return len(self.documento_set.all())
    
    class Meta:
      constraints = [ models.UniqueConstraint(fields=['nome','codicefiscale'], name="unique-utente") ]
      indexes = [ models.Index(fields=['codicefiscale','nome']) ]

