Spaces:
Runtime error
Runtime error
File size: 3,831 Bytes
97f32ec 25039bc 97f32ec 25039bc 97f32ec 25039bc 85ff68e 25039bc 85ff68e 25039bc 116dc2c 25039bc 116dc2c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | 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()
|