70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
"""
|
|
Notification models for user notifications.
|
|
"""
|
|
from django.db import models
|
|
from django.conf import settings
|
|
|
|
|
|
class Notification(models.Model):
|
|
"""User notifications."""
|
|
|
|
class Type(models.TextChoices):
|
|
BOUNTY_ACCEPTED = 'bounty_accepted', '悬赏被接受'
|
|
BOUNTY_COMPLETED = 'bounty_completed', '悬赏已完成'
|
|
NEW_COMMENT = 'new_comment', '新评论'
|
|
PAYMENT_RECEIVED = 'payment_received', '收到付款'
|
|
PRICE_ALERT = 'price_alert', '价格提醒'
|
|
SYSTEM = 'system', '系统通知'
|
|
|
|
id = models.AutoField(primary_key=True)
|
|
user = models.ForeignKey(
|
|
settings.AUTH_USER_MODEL,
|
|
on_delete=models.CASCADE,
|
|
related_name='notifications',
|
|
verbose_name='用户'
|
|
)
|
|
type = models.CharField(
|
|
'类型',
|
|
max_length=30,
|
|
choices=Type.choices
|
|
)
|
|
title = models.CharField('标题', max_length=200)
|
|
content = models.TextField('内容', blank=True, null=True)
|
|
related_id = models.IntegerField('关联ID', blank=True, null=True)
|
|
related_type = models.CharField('关联类型', max_length=50, blank=True, null=True)
|
|
is_read = models.BooleanField('是否已读', default=False)
|
|
created_at = models.DateTimeField('创建时间', auto_now_add=True)
|
|
|
|
class Meta:
|
|
db_table = 'notifications'
|
|
verbose_name = '通知'
|
|
verbose_name_plural = '通知'
|
|
ordering = ['-created_at']
|
|
|
|
def __str__(self):
|
|
return f"{self.title} -> {self.user}"
|
|
|
|
|
|
class NotificationPreference(models.Model):
|
|
"""User notification preferences."""
|
|
|
|
id = models.AutoField(primary_key=True)
|
|
user = models.OneToOneField(
|
|
settings.AUTH_USER_MODEL,
|
|
on_delete=models.CASCADE,
|
|
related_name='notification_preference',
|
|
verbose_name='用户'
|
|
)
|
|
enable_bounty = models.BooleanField('悬赏通知', default=True)
|
|
enable_price_alert = models.BooleanField('价格提醒', default=True)
|
|
enable_system = models.BooleanField('系统通知', default=True)
|
|
updated_at = models.DateTimeField('更新时间', auto_now=True)
|
|
|
|
class Meta:
|
|
db_table = 'notificationPreferences'
|
|
verbose_name = '通知偏好'
|
|
verbose_name_plural = '通知偏好'
|
|
|
|
def __str__(self):
|
|
return f"{self.user} preferences"
|