pms_server / admin_app /models.py
Noel-Teens's picture
implemented resend api
116dc2c
Raw History Blame Contribute Delete
3.83 kB
from django.db import models
import uuid
from datetime import timedelta
from django.utils import timezone
from django.conf import settings
from auth_app.models import User
def default_expiry():
return timezone.now() + timedelta(hours=48) # 48-hour expiry
class PaperWork(models.Model):
STATUS_CHOICES = (
('ASSIGNED', 'Assigned'),
('SUBMITTED', 'Submitted'),
('CHANGES_REQUESTED', 'Changes Requested'),
('APPROVED', 'Approved'),
)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=255)
researcher = models.ForeignKey(User, on_delete=models.CASCADE, related_name='paperworks')
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='ASSIGNED')
assigned_at = models.DateTimeField(auto_now_add=True)
deadline = models.DateTimeField(null=True, blank=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = 'Paper Work'
verbose_name_plural = 'Paper Works'
class Invitation(models.Model):
ROLE_CHOICES = (
('ADMIN', 'Admin'),
('RESEARCHER', 'Researcher'),
)
email = models.EmailField(unique=True)
role = models.CharField(max_length=50, choices=ROLE_CHOICES, default="RESEARCHER")
token = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
invited_by = models.ForeignKey(
settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="sent_invitations"
)
is_accepted = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
expires_at = models.DateTimeField(default=default_expiry)
# Email sending tracking fields
email_attempts = models.PositiveIntegerField(default=0)
last_email_attempt = models.DateTimeField(null=True, blank=True)
email_sent_successfully = models.BooleanField(default=False)
class Meta:
ordering = ["-created_at"]
def __str__(self):
return f"Invitation({self.email})"
def is_expired(self):
return timezone.now() > self.expires_at
def time_remaining(self):
return max(self.expires_at - timezone.now(), timedelta(seconds=0))
def can_send_email(self, max_attempts=5):
"""Check if we can send another email invitation.
Args:
max_attempts: Maximum number of email attempts allowed (default: 5)
Returns:
bool: True if another email can be sent, False otherwise
"""
# If already accepted, no need to send more emails
if self.is_accepted:
return False
# If expired, no point in sending more emails
if self.is_expired():
return False
# Check if we haven't exceeded max attempts
if self.email_attempts >= max_attempts:
return False
# If email was sent successfully, don't send again unless it's been a while
# (you can adjust this logic based on your needs)
if self.email_sent_successfully and self.last_email_attempt:
# Only allow resending if it's been more than 1 hour since last successful send
time_since_last = timezone.now() - self.last_email_attempt
if time_since_last < timedelta(hours=1):
return False
return True
def record_email_attempt(self, success=False):
"""Record an email sending attempt.
Args:
success: Whether the email was sent successfully
"""
self.email_attempts += 1
self.last_email_attempt = timezone.now()
if success:
self.email_sent_successfully = True
self.save()