Verified Commit 78af8b13 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Replace pyFCM by Firebase Admin SDK

parent d24295de
......@@ -15,7 +15,6 @@ django-ical = "*"
django-libsass = "*"
django-cors-headers = "*"
python-magic = "*"
pyfcm = "*"
celery = "*"
redis = "*"
raven = "*"
......@@ -28,6 +27,7 @@ bcrypt = "*"
"argon2_cffi" = "*"
uWSGI = "*"
"django-bootstrap4" = "*"
firebase-admin = "*"
[dev-packages]
django-template-check = "*"
......
{
"_meta": {
"hash": {
"sha256": "351d600cf5450eb307c94e01e950568ba6b0283c36697cb59013d315868580b7"
"sha256": "578ce12347a823b00a53ce5996d4778ba357b368eaf677a6b767ebdfa79d7f80"
},
"pipfile-spec": 6,
"requires": {},
......@@ -98,6 +98,19 @@
"index": "pypi",
"version": "==3.0.2"
},
"cachecontrol": {
"hashes": [
"sha256:cef77effdf51b43178f6a2d3b787e3734f98ade253fa3187f3bb7315aaa42ff7"
],
"version": "==0.12.5"
},
"cachetools": {
"hashes": [
"sha256:0a258d82933a1dd18cb540aca4ac5d5690731e24d1239a08577b814998f49785",
"sha256:4621965b0d9d4c82a79a29edbad19946f5e7702df4afae7d1ed2df951559a8cc"
],
"version": "==3.0.0"
},
"celery": {
"hashes": [
"sha256:77dab4677e24dc654d42dfbdfed65fa760455b6bb563a0877ecc35f4cfcfc678",
......@@ -248,6 +261,13 @@
"index": "pypi",
"version": "==3.9.0"
},
"firebase-admin": {
"hashes": [
"sha256:4f74ecd2be9d4ba45272bf44e87f7d70bf73f01956b4d1b39ef56a02942c70b2"
],
"index": "pypi",
"version": "==2.13.0"
},
"freezegun": {
"hashes": [
"sha256:6cb82b276f83f2acce67f121dc2656f4df26c71e32238334eb071170b892a278",
......@@ -256,6 +276,91 @@
"index": "pypi",
"version": "==0.3.11"
},
"google-api-core": {
"hashes": [
"sha256:35c0fdb7d0ea8e699d46611c31cdbbaef55c4b3905b394f16a8c41800be5de51",
"sha256:7cf597628cb9c5ceb24834b30a325dc271d3ba15d868d81c20aa80a77e13be65"
],
"version": "==1.5.1"
},
"google-auth": {
"hashes": [
"sha256:9ca363facbf2622d9ba828017536ccca2e0f58bd15e659b52f312172f8815530",
"sha256:a4cf9e803f2176b5de442763bd339b313d3f1ed3002e3e1eb6eec1d7c9bbc9b4"
],
"version": "==1.5.1"
},
"google-cloud-core": {
"hashes": [
"sha256:0090df83dbc5cb2405fa90844366d13176d1c0b48181c1807ab15f53be403f73",
"sha256:89e8140a288acec20c5e56159461d3afa4073570c9758c05d4e6cb7f2f8cc440"
],
"version": "==0.28.1"
},
"google-cloud-firestore": {
"hashes": [
"sha256:198ba208c7d2d189770e9ad5d7d00db4978cd11acbe0fe5e858266bcde113668",
"sha256:7f990572ace890867bbbc63c9d700c1d2635ba4c799e05f30b6fdca490021243"
],
"version": "==0.30.0"
},
"google-cloud-storage": {
"hashes": [
"sha256:936c859c47f8e94fd0005e98235a10d5e75828d2c6c3a8caacae18344a572a0a",
"sha256:fc32b9be41a45016ba2387e3ad23e70ccba399d626ef596409316f7cee477956"
],
"version": "==1.13.0"
},
"google-resumable-media": {
"hashes": [
"sha256:116de90b9cd483b17c53618ee6a5a20f33e741c648140c8cc9c2141e07616ff1",
"sha256:97de518f8166d442cc0b61fab308bcd319dbb970981e667ec8ded44f5ce49836"
],
"version": "==0.3.1"
},
"googleapis-common-protos": {
"hashes": [
"sha256:0946967c4c29b1339bb211949e1e17dbe0ae9ff8265fafa7bf4cf2164ef5a3b1"
],
"version": "==1.5.5"
},
"grpcio": {
"hashes": [
"sha256:0b09e82027f27cb540999404acf1be19cb50073d76ab257d7369aa3730bec3c0",
"sha256:0cc5f2d3ee21c642d8982f197c83053fd3a8cbcd6a60240d8c87c6c256b10d57",
"sha256:13b498b0415715a4214574c67ac6d0d7b565a861eb4490238a828fac17a51506",
"sha256:314c557efecec7f901cf394beb184b31414f906785e4811d2392859576d4d7b5",
"sha256:32d2859b68e185d05d6b5f5814121e786088f5e3483da0a7359f5d7fc0401ee3",
"sha256:3bf1b9d72a05a855762c36bd458d3750bedb5fd7b957a44443a62facf80afba4",
"sha256:41614ec2df4776a7d1b46183543d5c508bfc4972f092ec1ea83e98f808e5fa4d",
"sha256:4a7fab9f8ed8352d63585d221ee9c1fc58fb9b3d12535e777e36e855b0cab3db",
"sha256:4b4a2faa53e0f8d2b1479173dbce1523a7daaf2644fb835fb9fff04beb29ed8d",
"sha256:5526bf9f6615e22d0290aa83324f87fcc1fee51c3a9580ebeb2a52271c21a563",
"sha256:5bf2c9ec1d55c28ca1221f7b2d1914f20b2819c44579da89f447789baaba1386",
"sha256:62b24446d447ebe3a7002a6e3bd2c7372159e094868eb61ea2426327fe9f1992",
"sha256:63afda9d946fff727107ebbef25f6b45497f29486e462725dc9942391f3714a8",
"sha256:6dd039527b7333c947b9757ad40adf93b917f3734aed1da4fdeb28fd17ec63f0",
"sha256:6e719d17ca8fa06260a427cd1fab58abfd0672e8e625fcad81595bd125e0e367",
"sha256:76b3dbff4c775f5f8667c405b909ab2f80440c7579ad56f823476b011124a8a5",
"sha256:7be774ca3c8faa0e126d1e41e11fd82c9c114efb5437b36f651fe25add7f8c2e",
"sha256:7d74c3c6d8c7aadd505c8cef2b4b5324588bee645e6d20a6493940b24d394603",
"sha256:84afdfbf88c0ed2426a4f029fae3e677e8f1b2f3370feeae939d64670926c981",
"sha256:84d62107eb5bc9fe4e3682b038434c709ca7a2ae19e621e08ed7e8d908046cfb",
"sha256:8a1f4bee826b0edb123157f19843f46ca9ef29f12ed0b54eeffde5ff65101340",
"sha256:93edd492a1c6865e15db1ff7d98228b7351221bf815286a41834e10934c0cde0",
"sha256:9907fcb03a9fd327b114919dbb7a4577d5d5aeed2d6d000e6e6d002ad5cb959d",
"sha256:9dd008cd45a646b0e3761f0963c95b0dcd07d880d278a3c1ce23dd4ecb9cd174",
"sha256:a440935203be2581f68de7a4c5ca7ca22e948a21af70d7279ba9a2e32f73ae40",
"sha256:a9144b8a0f73be76aff348e4d558a5c3f43a8378a17c6327d56dbea8efda4aeb",
"sha256:b14629835e796f7905db2f7d10035958f995bae67bf9e652b13be156ed4a8457",
"sha256:b4fe851428b630bdf6f3a99c3761ce3d304b194162812fc1312bfe7bd138e620",
"sha256:c4318cea2d85f13811655e5d1c30fe97074aeb8105b16cc6da2d1d5d64a9f4f7",
"sha256:e46d3d702198d164474078140e008e8961e95dfb5a100f2890eb201c94c48c6e",
"sha256:e986100947cdafa2817701ffe616f2dc0221cc27eb301d654b9462b98ee62912",
"sha256:f94ae68c43b4bba0272e565882db2709d8827910ccc427f0a89d8cf070180f61"
],
"version": "==1.16.0"
},
"icalendar": {
"hashes": [
"sha256:07c2447a1d44cbb27c90b8c6a5c98e890cc1853c6223e2a52195cddec26c6356",
......@@ -301,6 +406,26 @@
],
"version": "==0.15.1"
},
"msgpack": {
"hashes": [
"sha256:0b3b1773d2693c70598585a34ca2715873ba899565f0a7c9a1545baef7e7fbdc",
"sha256:0bae5d1538c5c6a75642f75a1781f3ac2275d744a92af1a453c150da3446138b",
"sha256:0ee8c8c85aa651be3aa0cd005b5931769eaa658c948ce79428766f1bd46ae2c3",
"sha256:1369f9edba9500c7a6489b70fdfac773e925342f4531f1e3d4c20ac3173b1ae0",
"sha256:22d9c929d1d539f37da3d1b0e16270fa9d46107beab8c0d4d2bddffffe895cee",
"sha256:2ff43e3247a1e11d544017bb26f580a68306cec7a6257d8818893c1fda665f42",
"sha256:31a98047355d34d047fcdb55b09cb19f633cf214c705a765bd745456c142130c",
"sha256:8767eb0032732c3a0da92cbec5ac186ef89a3258c6edca09161472ca0206c45f",
"sha256:8acc8910218555044e23826980b950e96685dc48124a290c86f6f41a296ea172",
"sha256:ab189a6365be1860a5ecf8159c248f12d33f79ea799ae9695fa6a29896dcf1d4",
"sha256:cfd6535feb0f1cf1c7cdb25773e965cc9f92928244a8c3ef6f8f8a8e1f7ae5c4",
"sha256:e274cd4480d8c76ec467a85a9c6635bbf2258f0649040560382ab58cabb44bcf",
"sha256:f86642d60dca13e93260187d56c2bef2487aa4d574a669e8ceefcf9f4c26fd00",
"sha256:f8a57cbda46a94ed0db55b73e6ab0c15e78b4ede8690fa491a0e55128d552bb0",
"sha256:fcea97a352416afcbccd7af9625159d80704a25c519c251c734527329bb20d0e"
],
"version": "==0.5.6"
},
"pillow": {
"hashes": [
"sha256:00203f406818c3f45d47bb8fe7e67d3feddb8dcbbd45a289a1de7dd789226360",
......@@ -338,6 +463,27 @@
"markers": "python_version != '3.0.*' and python_version != '3.1.*' and python_version != '3.2.*' and python_version >= '2.7' and python_version != '3.3.*'",
"version": "==5.3.0"
},
"protobuf": {
"hashes": [
"sha256:10394a4d03af7060fa8a6e1cbf38cea44be1467053b0aea5bbfcb4b13c4b88c4",
"sha256:1489b376b0f364bcc6f89519718c057eb191d7ad6f1b395ffd93d1aa45587811",
"sha256:1931d8efce896981fe410c802fd66df14f9f429c32a72dd9cfeeac9815ec6444",
"sha256:196d3a80f93c537f27d2a19a4fafb826fb4c331b0b99110f985119391d170f96",
"sha256:46e34fdcc2b1f2620172d3a4885128705a4e658b9b62355ae5e98f9ea19f42c2",
"sha256:4b92e235a3afd42e7493b281c8b80c0c65cbef45de30f43d571d1ee40a1f77ef",
"sha256:574085a33ca0d2c67433e5f3e9a0965c487410d6cb3406c83bdaf549bfc2992e",
"sha256:59cd75ded98094d3cf2d79e84cdb38a46e33e7441b2826f3838dcc7c07f82995",
"sha256:5ee0522eed6680bb5bac5b6d738f7b0923b3cafce8c4b1a039a6107f0841d7ed",
"sha256:65917cfd5da9dfc993d5684643063318a2e875f798047911a9dd71ca066641c9",
"sha256:685bc4ec61a50f7360c9fd18e277b65db90105adbf9c79938bd315435e526b90",
"sha256:92e8418976e52201364a3174e40dc31f5fd8c147186d72380cbda54e0464ee19",
"sha256:9335f79d1940dfb9bcaf8ec881fb8ab47d7a2c721fb8b02949aab8bbf8b68625",
"sha256:a7ee3bb6de78185e5411487bef8bc1c59ebd97e47713cba3c460ef44e99b3db9",
"sha256:ceec283da2323e2431c49de58f80e1718986b79be59c266bb0509cbf90ca5b9e",
"sha256:fcfc907746ec22716f05ea96b7f41597dfe1a1c088f861efb8a0d4f4196a6f10"
],
"version": "==3.6.1"
},
"psycopg2-binary": {
"hashes": [
"sha256:04afb59bbbd2eab3148e6816beddc74348078b8c02a1113ea7f7822f5be4afe3",
......@@ -374,6 +520,20 @@
"index": "pypi",
"version": "==2.7.5"
},
"pyasn1": {
"hashes": [
"sha256:b9d3abc5031e61927c82d4d96c1cec1e55676c1a991623cfed28faea73cdd7ca",
"sha256:f58f2a3d12fd754aa123e9fa74fb7345333000a035f3921dbdaa08597aa53137"
],
"version": "==0.4.4"
},
"pyasn1-modules": {
"hashes": [
"sha256:a0cf3e1842e7c60fde97cb22d275eb6f9524f5c5250489e292529de841417547",
"sha256:a38a8811ea784c0136abfdba73963876328f66172db21a05a82f9515909bfb4e"
],
"version": "==0.2.2"
},
"pycparser": {
"hashes": [
"sha256:a988718abfad80b6b157acce7bf130a30876d27603738ac39f140993246b25b3"
......@@ -381,14 +541,6 @@
"markers": "python_version != '3.2.*' and python_version != '3.1.*' and python_version != '3.3.*' and python_version != '3.0.*' and python_version >= '2.7'",
"version": "==2.19"
},
"pyfcm": {
"hashes": [
"sha256:de3c7292e63186f30852c78cbbbc1bfa47b3996deb4e905be38e89a0692285ee",
"sha256:eca0d93481a441ecad865ac7d81b3b237a3e7a11cb872fd6961523391858af73"
],
"index": "pypi",
"version": "==1.4.5"
},
"python-dateutil": {
"hashes": [
"sha256:063df5763652e21de43de7d9e00ccf239f953a832941e37be541614732cdfc93",
......@@ -441,20 +593,19 @@
],
"version": "==2.20.0"
},
"requests-toolbelt": {
"hashes": [
"sha256:42c9c170abc2cacb78b8ab23ac957945c7716249206f90874651971a4acff237",
"sha256:f6a531936c6fa4c6cfce1b9c10d5c4f498d16528d2a54a22ca00011205a187b5"
],
"markers": "python_version != '3.2.*' and python_version >= '2.6' and python_version != '3.1.*' and python_version != '3.3.*' and python_version != '3.0.*' and python_version < '4'",
"version": "==0.8.0"
},
"rjsmin": {
"hashes": [
"sha256:dd9591aa73500b08b7db24367f8d32c6470021f39d5ab4e50c7c02e4401386f1"
],
"version": "==1.0.12"
},
"rsa": {
"hashes": [
"sha256:14ba45700ff1ec9eeb206a2ce76b32814958a98e372006c8fb76ba820211be66",
"sha256:1a836406405730121ae9823e19c6e806c62bbad73f890574fff50efa4122c487"
],
"version": "==4.0"
},
"six": {
"hashes": [
"sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9",
......@@ -464,11 +615,11 @@
},
"urllib3": {
"hashes": [
"sha256:a68ac5e15e76e7e5dd2b8f94007233e01effe3e50e8daddf69acfd81cb686baf",
"sha256:b5725a0bd4ba422ab0e66e89e030c806576753ea3ee08554382c14e685d117b5"
"sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39",
"sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22"
],
"markers": "python_version != '3.2.*' and python_version >= '2.6' and python_version != '3.1.*' and python_version != '3.3.*' and python_version != '3.0.*' and python_version < '4'",
"version": "==1.23"
"markers": "python_version >= '2.7' and python_version != '3.3.*' and python_version != '3.2.*' and python_version < '4' and python_version != '3.1.*' and python_version != '3.0.*'",
"version": "==1.24.1"
},
"uwsgi": {
"hashes": [
......
"""The models defined by the pushnotifications package"""
import datetime
from django.conf import settings
from django.db import models
from django.utils.translation import override
from django.utils.translation import ugettext_lazy as _
from pyfcm import FCMNotification
from firebase_admin import messaging
from utils.tasks import revoke_task, schedule_task
from utils.translation import MultilingualField, ModelTranslateMeta
......@@ -127,10 +129,9 @@ class Message(models.Model, metaclass=ModelTranslateMeta):
def send(self, **kwargs):
if self:
any_reg_ids = False
success_total = 0
failure_total = 0
result_list = []
ttl = kwargs.get('ttl', 3600)
for lang in settings.LANGUAGES:
with override(lang[0]):
......@@ -142,52 +143,46 @@ class Message(models.Model, metaclass=ModelTranslateMeta):
language=lang[0]
).values_list('registration_id', flat=True))
if len(reg_ids) == 0:
continue
any_reg_ids = True
data = {}
data = kwargs.get('data', {})
if self.url is not None:
data['url'] = self.url
result = FCMNotification(
api_key=settings.PUSH_NOTIFICATIONS_API_KEY
).notify_multiple_devices(
registration_ids=reg_ids,
message_title=self.title,
message_body=str(self.body),
color='#E62272',
sound='default',
data_message=data,
**kwargs
message = messaging.Message(
notification=messaging.Notification(
title=self.title,
body=str(self.body),
),
data=data,
android=messaging.AndroidConfig(
ttl=datetime.timedelta(seconds=ttl),
priority='normal',
notification=messaging.AndroidNotification(
color='#E62272',
sound='default',
),
),
)
results = result['results']
for (index, item) in enumerate(results):
if 'error' in item:
reg_id = reg_ids[index]
if (item['error'] == 'NotRegistered'
or item['error'] == 'InvalidRegistration'):
Device.objects.filter(
registration_id=reg_id).delete()
else:
Device.objects.filter(
registration_id=reg_id
).update(active=False)
success_total += result['success']
failure_total += result['failure']
result_list.append(result)
if any_reg_ids:
for reg_id in reg_ids:
message.token = reg_id
try:
messaging.send(message)
success_total += 1
except messaging.ApiCallError as e:
failure_total += 1
d = Device.objects.filter(registration_id=reg_id)
if e.code == 'registration-token-not-registered':
d.delete()
elif (e.code == 'invalid-argument'
or e.code == 'invalid-recipient'
or e.code == 'invalid-registration-token'):
d.update(active=False)
if success_total > 0 or failure_total > 0:
self.sent = True
self.success = success_total
self.failure = failure_total
self.save()
return result_list
return None
......
......@@ -8,13 +8,12 @@ overrides.
"""
# flake8: noqa: ignore F403
import os
from firebase_admin import initialize_app, credentials
# Load all default settings because we need to use settings.configure
# for sphinx documentation generation.
from django.conf.global_settings import * # pylint: disable=wildcard-import
# Load base settings
from .settings import * # pylint: disable=wildcard-import
......@@ -31,3 +30,9 @@ if os.environ.get('DJANGO_PRODUCTION'): # pragma: nocover
# Load testing settings if GITLAB_CI is set
if os.environ.get('GITLAB_CI'): # pragma: nocover
from .testing import * # pylint: disable=wildcard-import
try:
initialize_app(
credential=credentials.Certificate(FIREBASE_CREDENTIALS))
except ValueError as e:
print('Firebase application failed to initialise')
......@@ -8,7 +8,7 @@ This file is loaded by __init__.py if the environment variable
See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
"""
import json
import os
from . import settings
......@@ -76,12 +76,14 @@ PASSWORD_HASHERS = [
WIKI_API_KEY = os.environ.get('WIKI_API_KEY', 'changeme')
MIGRATION_KEY = os.environ.get('MIGRATION_KEY')
PUSH_NOTIFICATIONS_API_KEY = os.environ.get('PUSH_NOTIFICATIONS_API_KEY', '')
MAILINGLIST_API_SECRET = os.environ.get('MAILINGLIST_API_SECRET', '')
MEMBERS_SENTRY_API_SECRET = os.environ.get('MEMBERS_SENTRY_API_SECRET', '')
GOOGLE_MAPS_API_KEY = os.environ.get('GOOGLE_MAPS_API_KEY', '')
GOOGLE_MAPS_API_SECRET = os.environ.get('GOOGLE_MAPS_API_SECRET', '')
FIREBASE_CREDENTIALS = os.environ.get('FIREBASE_CREDENTIALS', '{}')
if FIREBASE_CREDENTIALS.startswith('{'):
FIREBASE_CREDENTIALS = json.loads(FIREBASE_CREDENTIALS)
if os.environ.get('DJANGO_SSLONLY'):
SECURE_SSL_REDIRECT = True
......
......@@ -265,6 +265,9 @@ THUMBNAIL_SIZES = {
'slide': '2000x430'
}
# Placeholder Firebase config
FIREBASE_CREDENTIALS = {}
# Default FROM email
DEFAULT_FROM_EMAIL = 'noreply@thalia.nu'
SERVER_EMAIL = DEFAULT_FROM_EMAIL
......@@ -281,9 +284,6 @@ BOARD_NOTIFICATION_ADDRESS = 'info@thalia.nu'
# Partners notification email
PARTNER_EMAIL = "samenwerking@thalia.nu"
# Push notifications API key
PUSH_NOTIFICATIONS_API_KEY = ''
# Mailinglist API key
MAILINGLIST_API_SECRET = ''
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment