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
+9 -1
View File
@@ -1,7 +1,7 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from accounts.models import ApiToken, User
from accounts.models import ApiToken, PushSubscription, User
@admin.register(User)
@@ -9,6 +9,14 @@ class OsirisUserAdmin(UserAdmin):
pass
@admin.register(PushSubscription)
class PushSubscriptionAdmin(admin.ModelAdmin):
"""Devices receiving reminders. Delete a row to stop pushing to that device."""
list_display = ["user", "user_agent", "created_at", "last_success_at"]
readonly_fields = ["endpoint", "p256dh", "auth", "created_at", "last_success_at"]
@admin.register(ApiToken)
class ApiTokenAdmin(admin.ModelAdmin):
"""Tokens are created by the Flutter app via /api/auth/login; this is for revoking."""
@@ -0,0 +1,45 @@
# Generated by Django 6.0.7 on 2026-07-14 18:46
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("accounts", "0001_initial"),
]
operations = [
migrations.CreateModel(
name="PushSubscription",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("endpoint", models.URLField(max_length=500, unique=True)),
("p256dh", models.CharField(max_length=200)),
("auth", models.CharField(max_length=100)),
("user_agent", models.CharField(blank=True, max_length=300)),
("created_at", models.DateTimeField(auto_now_add=True)),
("last_success_at", models.DateTimeField(blank=True, null=True)),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="push_subscriptions",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"ordering": ["-created_at"],
},
),
]
+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},
}