35 lines
1.4 KiB
Python
35 lines
1.4 KiB
Python
# apps/notification/models.py
|
|
from django.db import models
|
|
from django.utils import timezone
|
|
import uuid
|
|
from apps.accounts.models import User
|
|
|
|
class Notification(models.Model):
|
|
"""通知模型"""
|
|
NOTIFICATION_TYPES = (
|
|
('permission_request', '权限申请'),
|
|
('permission_approved', '权限批准'),
|
|
('permission_rejected', '权限拒绝'),
|
|
('permission_expired', '权限过期'),
|
|
('system_notice', '系统通知'),
|
|
)
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
type = models.CharField(max_length=20, choices=NOTIFICATION_TYPES)
|
|
title = models.CharField(max_length=100)
|
|
content = models.TextField()
|
|
sender = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='sent_notifications')
|
|
receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name='received_notifications')
|
|
is_read = models.BooleanField(default=False)
|
|
related_resource = models.CharField(max_length=100, blank=True) # 相关资源ID
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
ordering = ['-created_at']
|
|
indexes = [
|
|
models.Index(fields=['receiver', '-created_at']),
|
|
models.Index(fields=['type', 'is_read']),
|
|
]
|
|
|
|
def __str__(self):
|
|
return f"{self.get_type_display()} - {self.title}" |