Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic FCM device convenience model #615

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions push_notifications/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

from .apns import APNSServerError
from .gcm import GCMError
from .models import APNSDevice, GCMDevice, WebPushDevice, WNSDevice
from .models import APNSDevice, GCMDevice, WebPushDevice, WNSDevice, FCMDevice
from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
from .webpush import WebPushError


User = apps.get_model(*SETTINGS["USER_MODEL"].split("."))


Expand Down Expand Up @@ -135,7 +134,13 @@ class GCMDeviceAdmin(DeviceAdmin):
list_filter = ("active", "cloud_message_type")


class FCMDeviceAdmin(GCMDeviceAdmin):
list_display = GCMDeviceAdmin.list_display + ('platform',)
list_filter = GCMDeviceAdmin.list_filter + ('platform',)


admin.site.register(APNSDevice, DeviceAdmin)
admin.site.register(GCMDevice, GCMDeviceAdmin)
admin.site.register(FCMDevice, FCMDeviceAdmin)
admin.site.register(WNSDevice, DeviceAdmin)
admin.site.register(WebPushDevice, DeviceAdmin)
17 changes: 16 additions & 1 deletion push_notifications/api/rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework.viewsets import ModelViewSet

from ..fields import UNSIGNED_64BIT_INT_MAX_VALUE, hex_re
from ..models import APNSDevice, GCMDevice, WebPushDevice, WNSDevice
from ..models import APNSDevice, GCMDevice, WebPushDevice, WNSDevice, FCMDevice
from ..settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS


Expand Down Expand Up @@ -110,6 +110,12 @@ def validate_device_id(self, value):
return value


class FCMDeviceSerializer(GCMDeviceSerializer, ModelSerializer):
class Meta(GCMDeviceSerializer.Meta):
model = FCMDevice
fields = GCMDeviceSerializer.Meta.fields + ('platform',)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



class WNSDeviceSerializer(UniqueRegistrationSerializerMixin, ModelSerializer):
class Meta(DeviceSerializerMixin.Meta):
model = WNSDevice
Expand Down Expand Up @@ -211,3 +217,12 @@ class WebPushDeviceViewSet(DeviceViewSetMixin, ModelViewSet):

class WebPushDeviceAuthorizedViewSet(AuthorizedMixin, WebPushDeviceViewSet):
pass


class FCMDeviceViewSet(DeviceViewSetMixin, ModelViewSet):
queryset = FCMDevice.objects.all()
serializer_class = FCMDeviceSerializer


class FCMDeviceAuthorizedViewSet(AuthorizedMixin, FCMDeviceViewSet):
pass
25 changes: 25 additions & 0 deletions push_notifications/migrations/0008_fcmdevice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.1.12 on 2021-07-08 05:32

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('push_notifications', '0007_uniquesetting'),
]

operations = [
migrations.CreateModel(
name='FCMDevice',
fields=[
('gcmdevice_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='push_notifications.gcmdevice')),
('platform', models.CharField(blank=True, choices=[('i', 'ios'), ('a', 'android'), ('w', 'web')], help_text='Optional device platform: i - ios, a - android, w - web', max_length=1, null=True)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd much rather see a small positive integer field for faster filtering on the database.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a good idea indeed.

],
options={
'verbose_name': 'FCM device',
},
bases=('push_notifications.gcmdevice',),
),
]
29 changes: 27 additions & 2 deletions push_notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from .fields import HexIntegerField
from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS


CLOUD_MESSAGE_TYPES = (
("FCM", "Firebase Cloud Message"),
("GCM", "Google Cloud Message"),
Expand Down Expand Up @@ -124,7 +123,7 @@ def send_message(self, message, creds=None, **kwargs):
if self.exists():
from .apns import apns_send_bulk_message

app_ids = self.filter(active=True).order_by("application_id")\
app_ids = self.filter(active=True).order_by("application_id") \
.values_list("application_id", flat=True).distinct()
res = []
for app_id in app_ids:
Expand Down Expand Up @@ -257,3 +256,29 @@ def send_message(self, message, **kwargs):
return webpush_send_message(
uri=self.registration_id, message=message, browser=self.browser,
auth=self.auth, p256dh=self.p256dh, application_id=self.application_id, **kwargs)


class FCMDevice(GCMDevice):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The migration for existing users doesn't make sense. You should instead be renaming the current model to GCP no?

PLATFORM_IOS = 'i'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't seem to be indented properly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not indented properly. 4 chars.

PLATFORM_ANDROID = 'a'
PLATFORM_WEB = 'w'

PLATFORM_CHOICES = (
(PLATFORM_IOS, 'ios'),
(PLATFORM_ANDROID, 'android'),
(PLATFORM_WEB, 'web'),
)
platform = models.CharField(
max_length=1,
help_text=_("Optional device platform: i - ios, a - android, w - web"),
choices=PLATFORM_CHOICES,
null=True,
blank=True
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cloud_message_type = "FCM"

class Meta:
verbose_name = _("FCM device")