From 5391ec99d95c9cac267b45aa0b27d94c7ec2feaf Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 14 Dec 2023 18:51:13 -0800 Subject: [PATCH] models: Extract zerver.models.push_notifications. Signed-off-by: Anders Kaseorg --- zerver/models/__init__.py | 38 ++----------------------- zerver/models/push_notifications.py | 43 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 36 deletions(-) create mode 100644 zerver/models/push_notifications.py diff --git a/zerver/models/__init__.py b/zerver/models/__init__.py index 8651399862..1eac732724 100644 --- a/zerver/models/__init__.py +++ b/zerver/models/__init__.py @@ -75,6 +75,8 @@ from zerver.models.prereg_users import MultiuseInvite as MultiuseInvite from zerver.models.prereg_users import PreregistrationRealm as PreregistrationRealm from zerver.models.prereg_users import PreregistrationUser as PreregistrationUser from zerver.models.prereg_users import RealmReactivationStatus as RealmReactivationStatus +from zerver.models.push_notifications import AbstractPushDeviceToken as AbstractPushDeviceToken +from zerver.models.push_notifications import PushDeviceToken as PushDeviceToken from zerver.models.realm_emoji import RealmEmoji as RealmEmoji from zerver.models.realm_playgrounds import RealmPlayground as RealmPlayground from zerver.models.realms import Realm as Realm @@ -141,42 +143,6 @@ def query_for_ids( return query -class AbstractPushDeviceToken(models.Model): - APNS = 1 - GCM = 2 - - KINDS = ( - (APNS, "apns"), - (GCM, "gcm"), - ) - - kind = models.PositiveSmallIntegerField(choices=KINDS) - - # The token is a unique device-specific token that is - # sent to us from each device: - # - APNS token if kind == APNS - # - GCM registration id if kind == GCM - token = models.CharField(max_length=4096, db_index=True) - - # TODO: last_updated should be renamed date_created, since it is - # no longer maintained as a last_updated value. - last_updated = models.DateTimeField(auto_now=True) - - # [optional] Contains the app id of the device if it is an iOS device - ios_app_id = models.TextField(null=True) - - class Meta: - abstract = True - - -class PushDeviceToken(AbstractPushDeviceToken): - # The user whose device this is - user = models.ForeignKey(UserProfile, db_index=True, on_delete=CASCADE) - - class Meta: - unique_together = ("user", "kind", "token") - - def generate_email_token_for_stream() -> str: return secrets.token_hex(16) diff --git a/zerver/models/push_notifications.py b/zerver/models/push_notifications.py new file mode 100644 index 0000000000..079a2b2697 --- /dev/null +++ b/zerver/models/push_notifications.py @@ -0,0 +1,43 @@ +# https://github.com/typeddjango/django-stubs/issues/1698 +# mypy: disable-error-code="explicit-override" + +from django.db import models +from django.db.models import CASCADE + +from zerver.models.users import UserProfile + + +class AbstractPushDeviceToken(models.Model): + APNS = 1 + GCM = 2 + + KINDS = ( + (APNS, "apns"), + (GCM, "gcm"), + ) + + kind = models.PositiveSmallIntegerField(choices=KINDS) + + # The token is a unique device-specific token that is + # sent to us from each device: + # - APNS token if kind == APNS + # - GCM registration id if kind == GCM + token = models.CharField(max_length=4096, db_index=True) + + # TODO: last_updated should be renamed date_created, since it is + # no longer maintained as a last_updated value. + last_updated = models.DateTimeField(auto_now=True) + + # [optional] Contains the app id of the device if it is an iOS device + ios_app_id = models.TextField(null=True) + + class Meta: + abstract = True + + +class PushDeviceToken(AbstractPushDeviceToken): + # The user whose device this is + user = models.ForeignKey(UserProfile, db_index=True, on_delete=CASCADE) + + class Meta: + unique_together = ("user", "kind", "token")