from __future__ import unicode_literals
from django.db import models

from configurazione.models import *
from sicurezza.models import *
from domini.models import *
from template.models import *
from mail.models import *

class User(models.Model):
  id      = models.IntegerField(db_column='id',primary_key=True)
  mail    = models.CharField(db_column='mail',max_length=128)
  nome    = models.CharField(db_column='name',max_length=128)
  domain  = models.ForeignKey('domini.Domini',db_column='domain',on_delete=models.CASCADE)

  clear   = models.CharField(db_column='clear',max_length=128)
  crypt   = models.CharField(db_column='crypt',max_length=128)

  securitylevel = models.ForeignKey('sicurezza.Securitylevel',db_column='level',on_delete=models.PROTECT)

  password_change_enabled = models.BooleanField(db_column='password_change_enabled',default=1)
  last_password_update = models.DateField(db_column='last_password_update')

  enabled  = models.BooleanField(db_column='enabled')
  pop3 = models.BooleanField(db_column='pop3')
  imap = models.BooleanField(db_column='imap')
  smtp = models.BooleanField(db_column='smtp')
  smtp_relay = models.BooleanField(db_column='smtp_relay')
  webmail = models.BooleanField(db_column='webmail')
  sms = models.BooleanField(db_column='sms')
  lmtp = models.BooleanField(db_column='lmtp')
  nextcloud = models.BooleanField(db_column='nextcloud',default=False)
  nextcloud_pwd = models.BooleanField(db_column='nextcloud_pwd',default=True)
  sendafile = models.BooleanField(db_column='sendafile',default=False)

  tobedeleted = models.BooleanField(db_column='tobedeleted',default=False)

  #quota = models.IntegerField(db_column='mail_quota',default=0)
  mail_quota = models.IntegerField(db_column='mail_quota',default=0)

  nextcloud_quota = models.IntegerField(db_column='nextcloud_quota',default=0)
  date_nextcloud_quota = models.DateField(db_column='date_nextcloud_quota',blank=True)

  account_date_creation = models.DateField(db_column='account_date_creation',blank=True)
  account_date_disabled = models.DateField(db_column='account_date_disabled',blank=True)

  sms_communication = models.CharField(db_column='sms_communication',blank=True,max_length=128)

  nota = models.TextField(db_column='nota',blank=True)
  
  last_mail_checked = models.DateField(db_column='last_mail_checked')

  #proxy_host viene usato per forware le sessioni imap.
  #dovra' essere rimosso a favore di lmtp_server e mail_server
  proxy_host = models.CharField(db_column='proxy_host',blank=True,max_length=128)
  mail_server = models.ForeignKey('mail.MailServer',db_column='mail_server',on_delete=models.PROTECT)
  lmtp_server = models.ForeignKey('mail.LmtpServer',db_column='lmtp_server',on_delete=models.PROTECT)
  mail_proxy = models.ForeignKey('mail.MailProxy',db_column='mail_proxy',on_delete=models.PROTECT)
  sql_remote = models.ForeignKey('mail.SqlRemote',db_column='sql_remote',on_delete=models.PROTECT)

  warning_mail_quota = models.IntegerField(db_column='warning_mail_quota',default=0)
  limit_mail_quota = models.IntegerField(db_column='limit_mail_quota',default=0)

  mail_di_recupero = models.TextField(db_column='mail_di_recupero',max_length=128)
  chiave_temporanea = models.CharField(db_column='chiave_temporanea',max_length=128)

  accesso = models.DateTimeField(db_column='accesso')

  spf = models.BooleanField(db_column='spf')
  blacklist = models.BooleanField(db_column='blacklist')
  antivirus = models.BooleanField(db_column='antivirus')
  sieve = models.BooleanField(db_column='sieve')

  forward = models.CharField(db_column='forward',max_length=128,default="",null=True)

  '''
  mail_di_recupero: consente il recupero delle password smarrite dal pannello di login.
  viene inviata una mail a questo account.
  nel caso la mail non fosse stata inserita, viene visualizzato un messaggio di avviso.
  '''
  class Meta:
    ordering = ['id']
    db_table = 'mail_user'
    managed  = False

