feat: pwa notifications

This commit is contained in:
2026-07-14 20:49:30 +02:00
parent 2cf7b283e0
commit 5198826f83
21 changed files with 1563 additions and 8 deletions
+33
View File
@@ -40,3 +40,36 @@ class ApiToken(models.Model):
def touch(self):
self.last_used_at = timezone.now()
self.save(update_fields=["last_used_at"])
class PushSubscription(models.Model):
"""One installed PWA that has agreed to receive reminders.
The endpoint is issued by the browser's push service; the two keys are what
let us encrypt a payload only that device can read.
"""
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="push_subscriptions",
)
endpoint = models.URLField(max_length=500, unique=True)
p256dh = models.CharField(max_length=200)
auth = models.CharField(max_length=100)
user_agent = models.CharField(max_length=300, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
last_success_at = models.DateTimeField(null=True, blank=True)
class Meta:
ordering = ["-created_at"]
def __str__(self):
return f"{self.user}{self.user_agent[:40] or self.endpoint[:40]}"
def as_subscription_info(self) -> dict:
"""The shape pywebpush expects, i.e. what the browser handed us."""
return {
"endpoint": self.endpoint,
"keys": {"p256dh": self.p256dh, "auth": self.auth},
}