Debian Codegraph @

push_notifications/conf/legacy.py

raw
1 from django.core.exceptions import ImproperlyConfigured
2 from django.utils.six import string_types
3 from .base import BaseConfig
4 from ..settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
5
6
7 __all__ = [
8 "LegacyConfig"
9 ]
10
11
12 class empty(object):
13 pass
14
15
16 class LegacyConfig(BaseConfig):
17 def _get_application_settings(self, application_id, settings_key, error_message):
18 """Legacy behaviour"""
19
20 if not application_id:
21 value = SETTINGS.get(settings_key, empty)
22 if value is empty:
23 raise ImproperlyConfigured(error_message)
24 return value
25 else:
26 msg = (
27 "LegacySettings does not support application_id. To enable "
28 "multiple application support, use push_notifications.conf.AppSettings."
29 )
30 raise ImproperlyConfigured(msg)
31
32 def get_gcm_api_key(self, application_id=None):
33 msg = (
34 "Set PUSH_NOTIFICATIONS_SETTINGS[\"GCM_API_KEY\"] to send messages through GCM."
35 )
36 return self._get_application_settings(application_id, "GCM_API_KEY", msg)
37
38 def get_fcm_api_key(self, application_id=None):
39 msg = (
40 "Set PUSH_NOTIFICATIONS_SETTINGS[\"FCM_API_KEY\"] to send messages through FCM."
41 )
42 return self._get_application_settings(application_id, "FCM_API_KEY", msg)
43
44 def get_post_url(self, cloud_type, application_id=None):
45 key = "{}_POST_URL".format(cloud_type)
46 msg = (
47 "Set PUSH_NOTIFICATIONS_SETTINGS[\"{}\"] to send messages through {}.".format(
48 key, cloud_type
49 )
50 )
51 return self._get_application_settings(application_id, key, msg)
52
53 def get_error_timeout(self, cloud_type, application_id=None):
54 key = "{}_ERROR_TIMEOUT".format(cloud_type)
55 msg = (
56 "Set PUSH_NOTIFICATIONS_SETTINGS[\"{}\"] to send messages through {}.".format(
57 key, cloud_type
58 )
59 )
60 return self._get_application_settings(application_id, key, msg)
61
62 def get_max_recipients(self, cloud_type, application_id=None):
63 key = "{}_MAX_RECIPIENTS".format(cloud_type)
64 msg = (
65 "Set PUSH_NOTIFICATIONS_SETTINGS[\"{}\"] to send messages through {}.".format(
66 key, cloud_type
67 )
68 )
69 return self._get_application_settings(application_id, key, msg)
70
71 def get_apns_certificate(self, application_id=None):
72 r = self._get_application_settings(
73 application_id, "APNS_CERTIFICATE",
74 "You need to setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
75 )
76 if not isinstance(r, string_types):
77 # probably the (Django) file, and file path should be got
78 if hasattr(r, "path"):
79 return r.path
80 elif (hasattr(r, "has_key") or hasattr(r, "__contains__")) and "path" in r:
81 return r["path"]
82 else:
83 msg = (
84 "The APNS certificate settings value should be a string, or "
85 "should have a 'path' attribute or key"
86 )
87 raise ImproperlyConfigured(msg)
88 return r
89
90 def get_apns_use_sandbox(self, application_id=None):
91 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
92 return self._get_application_settings(application_id, "APNS_USE_SANDBOX", msg)
93
94 def get_apns_use_alternative_port(self, application_id=None):
95 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
96 return self._get_application_settings(application_id, "APNS_USE_ALTERNATIVE_PORT", msg)
97
98 def get_apns_topic(self, application_id=None):
99 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
100 return self._get_application_settings(application_id, "APNS_TOPIC", msg)
101
102 def get_apns_host(self, application_id=None):
103 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
104 return self._get_application_settings(application_id, "APNS_HOST", msg)
105
106 def get_apns_port(self, application_id=None):
107 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
108 return self._get_application_settings(application_id, "APNS_PORT", msg)
109
110 def get_apns_feedback_host(self, application_id=None):
111 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
112 return self._get_application_settings(application_id, "APNS_FEEDBACK_HOST", msg)
113
114 def get_apns_feedback_port(self, application_id=None):
115 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
116 return self._get_application_settings(application_id, "APNS_FEEDBACK_PORT", msg)
117
118 def get_wns_package_security_id(self, application_id=None):
119 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
120 return self._get_application_settings(application_id, "WNS_PACKAGE_SECURITY_ID", msg)
121
122 def get_wns_secret_key(self, application_id=None):
123 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
124 return self._get_application_settings(application_id, "WNS_SECRET_KEY", msg)
125
126 def get_wp_post_url(self, application_id, browser):
127 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
128 return self._get_application_settings(application_id, "WP_POST_URL", msg)[browser]
129
130 def get_wp_private_key(self, application_id=None):
131 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
132 return self._get_application_settings(application_id, "WP_PRIVATE_KEY", msg)
133
134 def get_wp_claims(self, application_id=None):
135 msg = "Setup PUSH_NOTIFICATIONS_SETTINGS properly to send messages"
136 return self._get_application_settings(application_id, "WP_CLAIMS", msg)