Skip to content

Commit 872d127

Browse files
committed
Don't init onesignal if missing secrets
1 parent bda5ecb commit 872d127

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

packet/__init__.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,25 @@
5050
app.config['OIDC_CLIENT_SECRET']))
5151

5252
# Initialize Onesignal Notification apps
53-
csh_onesignal_client = onesignal.Client(user_auth_key=app.config['ONESIGNAL_USER_AUTH_KEY'],
54-
app_auth_key=app.config['ONESIGNAL_CSH_APP_AUTH_KEY'],
55-
app_id=app.config['ONESIGNAL_CSH_APP_ID'])
56-
57-
intro_onesignal_client = onesignal.Client(user_auth_key=app.config['ONESIGNAL_USER_AUTH_KEY'],
58-
app_auth_key=app.config['ONESIGNAL_INTRO_APP_AUTH_KEY'],
59-
app_id=app.config['ONESIGNAL_INTRO_APP_ID'])
53+
csh_onesignal_client = None
54+
if app.config['ONESIGNAL_USER_AUTH_KEY'] and \
55+
app.config['ONESIGNAL_CSH_APP_AUTH_KEY'] and \
56+
app.config['ONESIGNAL_CSH_APP_ID']:
57+
csh_onesignal_client = onesignal.Client(
58+
user_auth_key=app.config['ONESIGNAL_USER_AUTH_KEY'],
59+
app_auth_key=app.config['ONESIGNAL_CSH_APP_AUTH_KEY'],
60+
app_id=app.config['ONESIGNAL_CSH_APP_ID']
61+
)
62+
63+
intro_onesignal_client = None
64+
if app.config['ONESIGNAL_USER_AUTH_KEY'] and \
65+
app.config['ONESIGNAL_INTRO_APP_AUTH_KEY'] and \
66+
app.config['ONESIGNAL_INTRO_APP_ID']:
67+
intro_onesignal_client = onesignal.Client(
68+
user_auth_key=app.config['ONESIGNAL_USER_AUTH_KEY'],
69+
app_auth_key=app.config['ONESIGNAL_INTRO_APP_AUTH_KEY'],
70+
app_id=app.config['ONESIGNAL_INTRO_APP_ID']
71+
)
6072

6173
# OIDC Auth
6274
auth = OIDCAuthentication({'app': APP_CONFIG}, app)

packet/notifications.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@
1111
'url': app.config['PROTOCOL'] + app.config['SERVER_NAME']
1212
}
1313

14+
def require_onesignal_intro(func):
15+
def require_onesignal_intro_wrapper(*args, **kwargs):
16+
if intro_onesignal_client:
17+
return func(*args, **kwargs)
18+
return None
19+
return require_onesignal_intro_wrapper
20+
21+
def require_onesignal_csh(func):
22+
def require_onesignal_csh_wrapper(*args, **kwargs):
23+
if csh_onesignal_client:
24+
return func(*args, **kwargs)
25+
return None
26+
return require_onesignal_csh_wrapper
27+
1428

1529
def send_notification(notification_body, subscriptions, client):
1630
tokens = list(map(lambda subscription: subscription.token, subscriptions))
@@ -24,6 +38,7 @@ def send_notification(notification_body, subscriptions, client):
2438
app.logger.warn('The notification ({}) was unsuccessful'.format(notification.post_body))
2539

2640

41+
@require_onesignal_intro
2742
def packet_signed_notification(packet, signer):
2843
subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username)
2944
if subscriptions:
@@ -36,6 +51,8 @@ def packet_signed_notification(packet, signer):
3651
send_notification(notification_body, subscriptions, intro_onesignal_client)
3752

3853

54+
@require_onesignal_csh
55+
@require_onesignal_intro
3956
def packet_100_percent_notification(packet):
4057
member_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.member.isnot(None))
4158
intro_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.freshman_username.isnot(None))
@@ -50,6 +67,7 @@ def packet_100_percent_notification(packet):
5067
send_notification(notification_body, intro_subscriptions, intro_onesignal_client)
5168

5269

70+
@require_onesignal_intro
5371
def packet_starting_notification(packet):
5472
subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username)
5573
if subscriptions:
@@ -62,6 +80,7 @@ def packet_starting_notification(packet):
6280
send_notification(notification_body, subscriptions, intro_onesignal_client)
6381

6482

83+
@require_onesignal_csh
6584
def packets_starting_notification(start_date):
6685
member_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.member.isnot(None))
6786
if member_subscriptions:

0 commit comments

Comments
 (0)