22from __future__ import unicode_literals
33import requests
44import logging
5+ import os
56
6- from django .conf import settings
77from popresearch .models import CmixDataArchive , CmixSurvey , CmixSurveyXml
88
9-
109from .error import CmixError
1110from .parsing import generate_survey_xml_strings_and_secondary_keys
1211
1312log = logging .getLogger (__name__ )
1413
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+
1535
1636def default_cmix_api ():
1737 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" )
2242 )
2343
2444
@@ -60,7 +80,7 @@ def authenticate(self, *args, **kwargs):
6080 "password" : self .password
6181 }
6282
63- auth_url = '{}/access-token' .format (settings . CMIX_SERVICES ['auth' ]['BASE_URL' ])
83+ auth_url = '{}/access-token' .format (CMIX_SERVICES ['auth' ]['BASE_URL' ])
6484 try :
6585 auth_response = requests .post (auth_url , json = auth_payload , headers = {"Content-Type" : "application/json" })
6686 if auth_response .status_code != 200 :
@@ -88,7 +108,7 @@ def fetch_banner_filter(self, survey_id, question_a, question_b, response_id):
88108 response_id
89109 )
90110 )
91- base_url = settings . CMIX_SERVICES ['reporting' ]['BASE_URL' ]
111+ base_url = CMIX_SERVICES ['reporting' ]['BASE_URL' ]
92112 url = '{}/surveys/{}/response-counts' .format (base_url , survey_id )
93113 payload = {
94114 'testYN' : 'LIVE' ,
@@ -118,7 +138,7 @@ def fetch_raw_results(self, survey_id, payload):
118138 '''
119139 self .check_auth_headers ()
120140 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' ]
122142 url = '{}/surveys/{}/response-counts' .format (base_url , survey_id )
123143 response = requests .post (url , headers = self ._authentication_headers , json = payload )
124144 return response .json ()
@@ -133,7 +153,7 @@ def get_surveys(self, status, *args, **kwargs):
133153 get_surveys('status', extra_params=params)
134154 '''
135155 self .check_auth_headers ()
136- base_url = settings . CMIX_SERVICES ['survey' ]['BASE_URL' ]
156+ base_url = CMIX_SERVICES ['survey' ]['BASE_URL' ]
137157 surveys_url = '{}/surveys?status={}' .format (base_url , status )
138158 extra_params = kwargs .get ('extra_params' )
139159 if extra_params is not None :
@@ -149,25 +169,25 @@ def add_extra_url_params(self, url, params):
149169
150170 def get_survey_definition (self , cmix_survey_id ):
151171 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 )
153173 definition_response = requests .get (definition_url , headers = self ._authentication_headers )
154174 return definition_response .json ()
155175
156176 def get_survey_xml (self , cmix_survey_id ):
157177 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 )
159179 xml_response = requests .get (xml_url , headers = self ._authentication_headers )
160180 return xml_response .content
161181
162182 def get_survey_test_url (self , cmix_survey_id ):
163183 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 )
165185 survey_response = requests .get (survey_url , headers = self ._authentication_headers )
166186 test_token = survey_response .json ().get ('testToken' , None )
167187 if test_token is None :
168188 raise CmixError ('Survey endpoint for CMIX ID {} did not return a test token.' .format (cmix_survey_id ))
169189 test_link = '{}/#/?cmixSvy={}&cmixTest={}' .format (
170- settings . CMIX_SERVICES ['test' ]['BASE_URL' ],
190+ CMIX_SERVICES ['test' ]['BASE_URL' ],
171191 cmix_survey_id ,
172192 test_token
173193 )
@@ -176,7 +196,7 @@ def get_survey_test_url(self, cmix_survey_id):
176196 def get_survey_respondents (self , cmix_survey_id , respondent_type , live ):
177197 self .check_auth_headers ()
178198 respondents_url = '{}/surveys/{}/respondents?respondentType={}&respondentStatus={}' .format (
179- settings . CMIX_SERVICES ['reporting' ]['BASE_URL' ],
199+ CMIX_SERVICES ['reporting' ]['BASE_URL' ],
180200 cmix_survey_id ,
181201 "LIVE" if live else "TEST" ,
182202 respondent_type ,
@@ -186,7 +206,7 @@ def get_survey_respondents(self, cmix_survey_id, respondent_type, live):
186206
187207 def get_survey_status (self , cmix_survey_id ):
188208 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 )
190210 status_response = requests .get (status_url , headers = self ._authentication_headers )
191211 status = status_response .json ().get ('status' , None )
192212 if status is None :
@@ -198,7 +218,7 @@ def get_survey_completes(self, cmix_survey_id):
198218
199219 def create_export_archive (self , cmix_survey , export_type ):
200220 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 )
202222 headers = self ._authentication_headers .copy ()
203223 headers ['Content-Type' ] = "application/json"
204224 payload = {
@@ -226,7 +246,7 @@ def create_export_archive(self, cmix_survey, export_type):
226246 )
227247 archive_json = archive_response .json ()
228248
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 )
230250 layout_response = requests .get (layout_url , headers = self ._authentication_headers )
231251 if layout_response .status_code != 200 :
232252 raise CmixError (
@@ -269,7 +289,7 @@ def update_archive_status(self, archive):
269289 'Error while updating archie status: CMIX archive ID is None. Pop Archive ID: {}' .format (archive .id )
270290 )
271291 archive_url = '{}/surveys/{}/data-layouts/{}/archives/{}' .format (
272- settings . CMIX_SERVICES ['survey' ]["BASE_URL" ],
292+ CMIX_SERVICES ['survey' ]["BASE_URL" ],
273293 archive .cmix_survey .cmix_id ,
274294 layout_id ,
275295 archive_id # The archive ID on CMIX.
@@ -299,7 +319,7 @@ def update_project(self, projectId, status=None):
299319 if payload_json == {}:
300320 raise CmixError ("No update data was provided for CMIX Project {}" .format (projectId ))
301321
302- url = '{}/projects/{}' .format (settings . CMIX_SERVICES ['survey' ]['BASE_URL' ], projectId )
322+ url = '{}/projects/{}' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], projectId )
303323 response = requests .patch (url , json = payload_json , headers = self ._authentication_headers )
304324 if response .status_code > 299 :
305325 raise CmixError (
@@ -328,7 +348,7 @@ def create_survey(self, survey, wave_number=None):
328348 if survey .failed_creation_attempts >= 10 :
329349 continue
330350
331- url = '{}/surveys/data' .format (settings . CMIX_SERVICES ['file' ]['BASE_URL' ])
351+ url = '{}/surveys/data' .format (CMIX_SERVICES ['file' ]['BASE_URL' ])
332352 payload = {"data" : xml_string }
333353 response = requests .post (url , payload , headers = self ._authentication_headers )
334354 cmix_responses .append (response )
0 commit comments