Skip to content

Commit

Permalink
WIP support FCM changes
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Jul 24, 2024
1 parent 146e81e commit ef8ff52
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
18 changes: 15 additions & 3 deletions temba/channels/types/firebase/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def setUp(self):
address="87654",
role="SR",
schemes=["fcm"],
config={"FCM_TITLE": "Title", "FCM_KEY": "87654"},
config={"FCM_TITLE": "Title", "FCM_AUTH_JSON": {"foo": "bar", "project_id": "87654"}},
)

@patch("requests.get")
Expand All @@ -34,14 +34,26 @@ def test_claim(self, mock_get):
self.assertContains(response, url)

response = self.client.post(
url, {"title": "FCM Channel", "address": "abcde12345", "send_notification": "True"}, follow=True
url,
{
"title": "FCM Channel",
"address": "abcde12345",
"authentication_json": '{"foo": "bar", "baz": "abc", "project_id": "abcde12345"}',
"send_notification": "True",
},
follow=True,
)

channel = Channel.objects.get(address="abcde12345")
self.assertRedirects(response, reverse("channels.channel_configuration", args=[channel.uuid]))
self.assertEqual(channel.channel_type, "FCM")
self.assertEqual(
channel.config, {"FCM_KEY": "abcde12345", "FCM_TITLE": "FCM Channel", "FCM_NOTIFICATION": True}
channel.config,
{
"FCM_AUTH_JSON": {"foo": "bar", "baz": "abc", "project_id": "abcde12345"},
"FCM_TITLE": "FCM Channel",
"FCM_NOTIFICATION": True,
},
)

response = self.client.get(reverse("channels.channel_configuration", args=[channel.uuid]))
Expand Down
22 changes: 20 additions & 2 deletions temba/channels/types/firebase/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import json

from smartmin.views import SmartFormView

from django import forms
from django.utils.translation import gettext_lazy as _

from temba.utils.fields import InputWidget

from ...models import Channel
from ...views import ClaimViewMixin


class PrettyJSONEncoder(json.JSONEncoder):
def __init__(self, *args, indent, sort_keys, **kwargs):
super().__init__(*args, indent=4, sort_keys=False, **kwargs)


class ClaimView(ClaimViewMixin, SmartFormView):
class Form(ClaimViewMixin.Form):
title = forms.CharField(label=_("Notification Title"))
address = forms.CharField(
label=_("FCM Key"), help_text=_("The key provided on the the Firebase Console when you created your app.")
label="Project ID",
help_text=_("FCM Project ID"),
)

authentication_json = forms.JSONField(
widget=InputWidget({"textarea": True}),
help_text=_("FCM autentication JSON"),
encoder=PrettyJSONEncoder,
initial={},
)
send_notification = forms.CharField(
label=_("Send notification"),
Expand All @@ -24,8 +41,9 @@ class Form(ClaimViewMixin.Form):

def form_valid(self, form):
title = form.cleaned_data.get("title")
authentication_json = form.cleaned_data.get("authentication_json")
address = form.cleaned_data.get("address")
config = {"FCM_TITLE": title, "FCM_KEY": address}
config = {"FCM_TITLE": title, "FCM_AUTH_JSON": authentication_json}

if form.cleaned_data.get("send_notification") == "True":
config["FCM_NOTIFICATION"] = True
Expand Down

0 comments on commit ef8ff52

Please sign in to comment.