from django.db import models

# Create your models here.
from domini.models import *

class Contatti(models.Model):
  id   = models.IntegerField(db_column='id',primary_key=True)
  nome = models.CharField(db_column='nome',max_length=256)
  mail = models.CharField(db_column='mail',max_length=128)
  sms = models.CharField(db_column='sms',max_length=50)
  nota = models.TextField(db_column='nota')
  enabled = models.BooleanField(db_column='enabled')

  def CalcolaNumeroDomini(self):
    return ContattixDomini.objects.filter(contatti=self).count()
  dominicontatore=property(CalcolaNumeroDomini)

  def ListaDominiAssociati(self):
    return ContattixDomini.objects.filter(contatti=self)
  dominilista=property(ListaDominiAssociati)

  class Meta:   
    ordering = ['nome']
    db_table = 'domini_contatti'
    managed = False

class ContattixDominio(models.Model):
  id      = models.IntegerField(db_column='id',primary_key=True)
  domini  = models.ForeignKey('domini.Domini',db_column='domain',on_delete=models.CASCADE)
  contatti = models.ForeignKey('Contatti',db_column='contact',on_delete=models.CASCADE)
  titolare_dominio = models.BooleanField(db_column='titolare_dominio')
  notifica_dominio = models.BooleanField(db_column='notifica_dominio')
  notifica_servizio = models.BooleanField(db_column='notifica_servizio')

  class Meta:
    ordering = ['domini']
    db_table = 'domini_contatti_assegnati'
    managed  = False

