from django.db import models
from django.contrib.auth.models import AbstractUser
from datetime import timedelta, date

class User(AbstractUser):
    password_expiry_date = models.DateField(null=True, blank=True)

    groups = models.ManyToManyField(
        'auth.Group',
        verbose_name='groups',
        blank=True,
        related_name='custom_user_permissions'
    )
    user_permissions = models.ManyToManyField(
        'auth.Permission',
        verbose_name='user permissions',
        blank=True,
        related_name='custom_user_permissions'
    )

    def save(self, *args, **kwargs):
        if not self.pk:
            self.password_expiry_date = date.today() + timedelta(days=14)
        super().save(*args, **kwargs)

    def is_password_expired(self):

        if self.password_expiry_date is None:
            return False
        return date.today() > self.password_expiry_date

