Files
shopship/apps/notifications/models.py
T

51 lines
2.2 KiB
Python
Raw Normal View History

2026-08-24 15:54:08 +03:30
from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _
# اعلان‌ها دو جورند: عمومی (user=None، برای همه‌ی کاربران — مثلا خبر جدید)
# یا شخصی (user مشخص — مثلا «با کد معرفت جم گرفتی»). چون یک اعلانِ عمومی
# بین همه مشترک است، وضعیت «خوانده‌شد» را نمی‌شود مستقیم روی خودِ رکورد
# نگه داشت؛ به همین خاطر NotificationRead یک جدول واسط جداست.
# هشدار «دوره‌ی آزمایشی رو به پایان» ذخیره نمی‌شود — همیشه زنده از
# User.trial_expires_at محاسبه می‌شود، چون دقیق‌تر و بدون نیاز به cron است.
class Notification(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
blank=True,
on_delete=models.CASCADE,
related_name="notifications",
verbose_name=_("کاربر (خالی = عمومی برای همه)"),
)
title = models.CharField(max_length=150, verbose_name=_("عنوان"))
body = models.TextField(blank=True, verbose_name=_("متن"))
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name = _("اعلان")
verbose_name_plural = _("اعلان‌ها")
ordering = ["-created_at"]
def __str__(self):
return self.title
class NotificationRead(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="notification_reads"
)
notification = models.ForeignKey(Notification, on_delete=models.CASCADE, related_name="reads")
read_at = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name = _("خوانده‌شدنِ اعلان")
verbose_name_plural = _("خوانده‌شدنِ اعلان‌ها")
constraints = [
models.UniqueConstraint(fields=["user", "notification"], name="unique_user_notification_read")
]
def __str__(self):
return f"{self.user.phone} — {self.notification.title}"