2
2
from __future__ import unicode_literals
3
3
import requests
4
4
import logging
5
+ import os
5
6
6
- from django .conf import settings
7
7
from popresearch .models import CmixDataArchive , CmixSurvey , CmixSurveyXml
8
8
9
-
10
9
from .error import CmixError
11
10
from .parsing import generate_survey_xml_strings_and_secondary_keys
12
11
13
12
log = logging .getLogger (__name__ )
14
13
14
+ CMIX_SERVICES = {
15
+ 'auth' : {
16
+ 'BASE_URL' : os .getenv ('CMIX_URL_AUTH' ),
17
+ },
18
+ 'launchpad' : {
19
+ 'BASE_URL' : os .getenv ('CMIX_URL_LAUNCHPAD' ),
20
+ },
21
+ 'reporting' : {
22
+ 'BASE_URL' : os .getenv ('CMIX_URL_REPORTING' ),
23
+ },
24
+ 'survey' : {
25
+ 'BASE_URL' : os .getenv ('CMIX_URL_SURVEY' ),
26
+ },
27
+ 'file' : {
28
+ 'BASE_URL' : os .getenv ('CMIX_URL_FILE' ),
29
+ },
30
+ 'test' : {
31
+ 'BASE_URL' : os .getenv ('CMIX_URL_TEST' ),
32
+ },
33
+ }
34
+
15
35
16
36
def default_cmix_api ():
17
37
return CmixAPI (
18
- username = settings . CMIX_USERNAME ,
19
- password = settings . CMIX_PASSWORD ,
20
- client_id = settings . CMIX_V2_CLIENT_ID ,
21
- client_secret = settings . CMIX_V2_CLIENT_SECRET
38
+ username = os . getenv ( " CMIX_USERNAME" ) ,
39
+ password = os . getenv ( " CMIX_PASSWORD" ) ,
40
+ client_id = os . getenv ( " CMIX_V2_CLIENT_ID" ) ,
41
+ client_secret = os . getenv ( " CMIX_V2_CLIENT_SECRET" )
22
42
)
23
43
24
44
@@ -60,7 +80,7 @@ def authenticate(self, *args, **kwargs):
60
80
"password" : self .password
61
81
}
62
82
63
- auth_url = '{}/access-token' .format (settings . CMIX_SERVICES ['auth' ]['BASE_URL' ])
83
+ auth_url = '{}/access-token' .format (CMIX_SERVICES ['auth' ]['BASE_URL' ])
64
84
try :
65
85
auth_response = requests .post (auth_url , json = auth_payload , headers = {"Content-Type" : "application/json" })
66
86
if auth_response .status_code != 200 :
@@ -88,7 +108,7 @@ def fetch_banner_filter(self, survey_id, question_a, question_b, response_id):
88
108
response_id
89
109
)
90
110
)
91
- base_url = settings . CMIX_SERVICES ['reporting' ]['BASE_URL' ]
111
+ base_url = CMIX_SERVICES ['reporting' ]['BASE_URL' ]
92
112
url = '{}/surveys/{}/response-counts' .format (base_url , survey_id )
93
113
payload = {
94
114
'testYN' : 'LIVE' ,
@@ -118,7 +138,7 @@ def fetch_raw_results(self, survey_id, payload):
118
138
'''
119
139
self .check_auth_headers ()
120
140
log .debug ('Requesting raw results for CMIX survey {}' .format (survey_id ))
121
- base_url = settings . CMIX_SERVICES ['reporting' ]['BASE_URL' ]
141
+ base_url = CMIX_SERVICES ['reporting' ]['BASE_URL' ]
122
142
url = '{}/surveys/{}/response-counts' .format (base_url , survey_id )
123
143
response = requests .post (url , headers = self ._authentication_headers , json = payload )
124
144
return response .json ()
@@ -133,7 +153,7 @@ def get_surveys(self, status, *args, **kwargs):
133
153
get_surveys('status', extra_params=params)
134
154
'''
135
155
self .check_auth_headers ()
136
- base_url = settings . CMIX_SERVICES ['survey' ]['BASE_URL' ]
156
+ base_url = CMIX_SERVICES ['survey' ]['BASE_URL' ]
137
157
surveys_url = '{}/surveys?status={}' .format (base_url , status )
138
158
extra_params = kwargs .get ('extra_params' )
139
159
if extra_params is not None :
@@ -149,25 +169,25 @@ def add_extra_url_params(self, url, params):
149
169
150
170
def get_survey_definition (self , cmix_survey_id ):
151
171
self .check_auth_headers ()
152
- definition_url = '{}/surveys/{}/definition' .format (settings . CMIX_SERVICES ['survey' ]['BASE_URL' ], cmix_survey_id )
172
+ definition_url = '{}/surveys/{}/definition' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], cmix_survey_id )
153
173
definition_response = requests .get (definition_url , headers = self ._authentication_headers )
154
174
return definition_response .json ()
155
175
156
176
def get_survey_xml (self , cmix_survey_id ):
157
177
self .check_auth_headers ()
158
- xml_url = '{}/surveys/{}' .format (settings . CMIX_SERVICES ['file' ]['BASE_URL' ], cmix_survey_id )
178
+ xml_url = '{}/surveys/{}' .format (CMIX_SERVICES ['file' ]['BASE_URL' ], cmix_survey_id )
159
179
xml_response = requests .get (xml_url , headers = self ._authentication_headers )
160
180
return xml_response .content
161
181
162
182
def get_survey_test_url (self , cmix_survey_id ):
163
183
self .check_auth_headers ()
164
- survey_url = '{}/surveys/{}' .format (settings . CMIX_SERVICES ['survey' ]['BASE_URL' ], cmix_survey_id )
184
+ survey_url = '{}/surveys/{}' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], cmix_survey_id )
165
185
survey_response = requests .get (survey_url , headers = self ._authentication_headers )
166
186
test_token = survey_response .json ().get ('testToken' , None )
167
187
if test_token is None :
168
188
raise CmixError ('Survey endpoint for CMIX ID {} did not return a test token.' .format (cmix_survey_id ))
169
189
test_link = '{}/#/?cmixSvy={}&cmixTest={}' .format (
170
- settings . CMIX_SERVICES ['test' ]['BASE_URL' ],
190
+ CMIX_SERVICES ['test' ]['BASE_URL' ],
171
191
cmix_survey_id ,
172
192
test_token
173
193
)
@@ -176,7 +196,7 @@ def get_survey_test_url(self, cmix_survey_id):
176
196
def get_survey_respondents (self , cmix_survey_id , respondent_type , live ):
177
197
self .check_auth_headers ()
178
198
respondents_url = '{}/surveys/{}/respondents?respondentType={}&respondentStatus={}' .format (
179
- settings . CMIX_SERVICES ['reporting' ]['BASE_URL' ],
199
+ CMIX_SERVICES ['reporting' ]['BASE_URL' ],
180
200
cmix_survey_id ,
181
201
"LIVE" if live else "TEST" ,
182
202
respondent_type ,
@@ -186,7 +206,7 @@ def get_survey_respondents(self, cmix_survey_id, respondent_type, live):
186
206
187
207
def get_survey_status (self , cmix_survey_id ):
188
208
self .check_auth_headers ()
189
- status_url = '{}/surveys/{}' .format (settings . CMIX_SERVICES ['survey' ]['BASE_URL' ], cmix_survey_id )
209
+ status_url = '{}/surveys/{}' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], cmix_survey_id )
190
210
status_response = requests .get (status_url , headers = self ._authentication_headers )
191
211
status = status_response .json ().get ('status' , None )
192
212
if status is None :
@@ -198,7 +218,7 @@ def get_survey_completes(self, cmix_survey_id):
198
218
199
219
def create_export_archive (self , cmix_survey , export_type ):
200
220
self .check_auth_headers ()
201
- archive_url = '{}/surveys/{}/archives' .format (settings . CMIX_SERVICES ['survey' ]['BASE_URL' ], cmix_survey .cmix_id )
221
+ archive_url = '{}/surveys/{}/archives' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], cmix_survey .cmix_id )
202
222
headers = self ._authentication_headers .copy ()
203
223
headers ['Content-Type' ] = "application/json"
204
224
payload = {
@@ -226,7 +246,7 @@ def create_export_archive(self, cmix_survey, export_type):
226
246
)
227
247
archive_json = archive_response .json ()
228
248
229
- layout_url = '{}/surveys/{}/data-layouts/' .format (settings . CMIX_SERVICES ['survey' ]["BASE_URL" ], cmix_survey .cmix_id )
249
+ layout_url = '{}/surveys/{}/data-layouts/' .format (CMIX_SERVICES ['survey' ]["BASE_URL" ], cmix_survey .cmix_id )
230
250
layout_response = requests .get (layout_url , headers = self ._authentication_headers )
231
251
if layout_response .status_code != 200 :
232
252
raise CmixError (
@@ -269,7 +289,7 @@ def update_archive_status(self, archive):
269
289
'Error while updating archie status: CMIX archive ID is None. Pop Archive ID: {}' .format (archive .id )
270
290
)
271
291
archive_url = '{}/surveys/{}/data-layouts/{}/archives/{}' .format (
272
- settings . CMIX_SERVICES ['survey' ]["BASE_URL" ],
292
+ CMIX_SERVICES ['survey' ]["BASE_URL" ],
273
293
archive .cmix_survey .cmix_id ,
274
294
layout_id ,
275
295
archive_id # The archive ID on CMIX.
@@ -299,7 +319,7 @@ def update_project(self, projectId, status=None):
299
319
if payload_json == {}:
300
320
raise CmixError ("No update data was provided for CMIX Project {}" .format (projectId ))
301
321
302
- url = '{}/projects/{}' .format (settings . CMIX_SERVICES ['survey' ]['BASE_URL' ], projectId )
322
+ url = '{}/projects/{}' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], projectId )
303
323
response = requests .patch (url , json = payload_json , headers = self ._authentication_headers )
304
324
if response .status_code > 299 :
305
325
raise CmixError (
@@ -328,7 +348,7 @@ def create_survey(self, survey, wave_number=None):
328
348
if survey .failed_creation_attempts >= 10 :
329
349
continue
330
350
331
- url = '{}/surveys/data' .format (settings . CMIX_SERVICES ['file' ]['BASE_URL' ])
351
+ url = '{}/surveys/data' .format (CMIX_SERVICES ['file' ]['BASE_URL' ])
332
352
payload = {"data" : xml_string }
333
353
response = requests .post (url , payload , headers = self ._authentication_headers )
334
354
cmix_responses .append (response )
0 commit comments