# Path: apps/core/models.py
# class EmailAuthToken(models.Model):
#     """
#     denotes single email auth token for an user
#     - tokenid:     unique token id
#     - user:        user object
#     - expire_time: expire time; < 24 hours
#     """
#     tokenid = models.CharField(max_length=48, primary_key=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     expire_time = models.DateTimeField()
# 
#     def __str__(self):
#         return f'{self.user} - {self.tokenid}'
# 
# class ResetPWToken(models.Model):
#     """
#     denotes single password reset token for an user
#     - tokenid:     unique token id
#     - user:        user object
#     - expire_time: expire time; < 24 hours
#     """
#     tokenid = models.CharField(max_length=48, primary_key=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     expire_time = models.DateTimeField()
# 
#     def __str__(self):
#         return f'{self.user} - {self.tokenid}'


import datetime
from secrets import token_hex
from django.conf import settings
from django.core.mail import send_mail
from django.utils import timezone
from apps.core.models import EmailAuthToken, ResetPWToken

EMAIL_SENDER = 'noreply@sso.sparcs.org'


# issue reset pw token to user
def token_issue_reset_pw(user):
    ResetPWToken.objects.filter(user=user).delete()
    while True:
        tokenid = token_hex(24)
        if not ResetPWToken.objects.filter(tokenid=tokenid).count():
            break

    tomorrow = timezone.now() + datetime.timedelta(days=1)
    ResetPWToken.objects.filter(user=user).delete()
    ResetPWToken(tokenid=tokenid, expire_time=tomorrow, user=user).save()

    url = f'{settings.DOMAIN}/account/password/reset/{tokenid}/'
    msg = RESET_PW_MSG_BODY.format(
        name=user.first_name,
        link=url,
    )
    send_mail(RESET_PW_MSG_TITLE, '', EMAIL_SENDER,
              [user.email], html_message=msg)


# issue email auth token to user
def token_issue_email_auth(user, newbie=False):
    EmailAuthToken.objects.filter(user=user).delete()
    while True:
        tokenid = token_hex(24)

# Path: apps/core/models.py
# class EmailAuthToken(models.Model):
#     """
#     denotes single email auth token for an user
#     - tokenid:     unique token id
#     - user:        user object
#     - expire_time: expire time; < 24 hours
#     """
#     tokenid = models.CharField(max_length=48, primary_key=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     expire_time = models.DateTimeField()
# 
#     def __str__(self):
#         return f'{self.user} - {self.tokenid}'
# 
# class ResetPWToken(models.Model):
#     """
#     denotes single password reset token for an user
#     - tokenid:     unique token id
#     - user:        user object
#     - expire_time: expire time; < 24 hours
#     """
#     tokenid = models.CharField(max_length=48, primary_key=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     expire_time = models.DateTimeField()
# 
#     def __str__(self):
#         return f'{self.user} - {self.tokenid}'


# Path: apps/core/backends/token.py
import datetime
from secrets import token_hex
from django.conf import settings
from django.core.mail import send_mail
from django.utils import timezone
from apps.core.models import EmailAuthToken, ResetPWToken


EMAIL_SENDER = 'noreply@sso.sparcs.org'


# issue reset pw token to user
def token_issue_reset_pw(user):
    ResetPWToken.objects.filter(user=user).delete()
    while True:
        tokenid = token_hex(24)
        if not ResetPWToken.objects.filter(tokenid=tokenid).count():
            break

    tomorrow = timezone.now() + datetime.timedelta(days=1)
    ResetPWToken.objects.filter(user=user).delete()
    ResetPWToken(tokenid=tokenid, expire_time=tomorrow, user=user).save()

    url = f'{settings.DOMAIN}/account/password/reset/{tokenid}/'
    msg = RESET_PW_MSG_BODY.format(
        name=user.first_name,
        link=url,
    )
    send_mail(RESET_PW_MSG_TITLE, '', EMAIL_SENDER,
              [user.email], html_message=msg)


# issue email auth token to user
def token_issue_email_auth(user, newbie=False):
    EmailAuthToken.objects.filter(user=user).delete()
    while True:
        tokenid = token_hex(24)

        if not EmailAuthToken.objects.filter(tokenid=tokenid).count():