1
1
# -*- coding: utf-8 -*-
2
2
from __future__ import unicode_literals
3
- import json
4
3
import requests
5
4
import logging
6
5
7
6
from django .conf import settings
8
- from popresearch .models import SURVEY_TYPE_CUSTOM , SURVEY_TYPE_CREATIVE , \
9
- SURVEY_TYPE_COMPARISON , SURVEY_TYPE_FORCEDEXPOSURE , SURVEY_TYPE_TRACKING ,\
10
- CmixDataArchive , CmixSurvey , CmixSurveyXml , Survey
7
+ from popresearch .models import CmixDataArchive , CmixSurvey , CmixSurveyXml
11
8
12
9
13
10
from .error import CmixError
@@ -67,7 +64,12 @@ def authenticate(self, *args, **kwargs):
67
64
try :
68
65
auth_response = requests .post (auth_url , json = auth_payload , headers = {"Content-Type" : "application/json" })
69
66
if auth_response .status_code != 200 :
70
- raise CmixError ('CMIX returned a non-200 response code: {} and error {}' .format (auth_response .status_code , auth_response .text ))
67
+ raise CmixError (
68
+ 'CMIX returned a non-200 response code: {} and error {}' .format (
69
+ auth_response .status_code ,
70
+ auth_response .text
71
+ )
72
+ )
71
73
except Exception as e :
72
74
raise CmixError ('Could not request authorization from CMIX. Error: {}' .format (e ))
73
75
auth_json = auth_response .json ()
@@ -78,7 +80,14 @@ def authenticate(self, *args, **kwargs):
78
80
79
81
def fetch_banner_filter (self , survey_id , question_a , question_b , response_id ):
80
82
self .check_auth_headers ()
81
- log .debug ('Requesting banner filter for CMIX survey {}, question A: {}, question B: {}, response ID: {}' .format (survey_id , question_a , question_b , response_id ))
83
+ log .debug (
84
+ 'Requesting banner filter for CMIX survey {}, question A: {}, question B: {}, response ID: {}' .format (
85
+ survey_id ,
86
+ question_a ,
87
+ question_b ,
88
+ response_id
89
+ )
90
+ )
82
91
base_url = settings .CMIX_SERVICES ['reporting' ]['BASE_URL' ]
83
92
url = '{}/surveys/{}/response-counts' .format (base_url , survey_id )
84
93
payload = {
@@ -202,21 +211,41 @@ def create_export_archive(self, cmix_survey, export_type):
202
211
203
212
archive_response = requests .post (archive_url , json = payload , headers = headers )
204
213
if archive_response .status_code != 200 :
205
- raise CmixError ('CMIX returned a non-200 response code: {} and error {}' .format (archive_response .status_code , archive_response .text ))
214
+ raise CmixError (
215
+ 'CMIX returned a non-200 response code: {} and error {}' .format (
216
+ archive_response .status_code ,
217
+ archive_response .text
218
+ )
219
+ )
206
220
if archive_response .json ().get ('error' , None ) is not None :
207
- raise CmixError ('CMIX returned an error with status code {}: {}' .format (archive_response .status_code , archive_response .text ))
221
+ raise CmixError (
222
+ 'CMIX returned an error with status code {}: {}' .format (
223
+ archive_response .status_code ,
224
+ archive_response .text
225
+ )
226
+ )
208
227
archive_json = archive_response .json ()
209
228
210
229
layout_url = '{}/surveys/{}/data-layouts/' .format (settings .CMIX_SERVICES ['survey' ]["BASE_URL" ], cmix_survey .cmix_id )
211
230
layout_response = requests .get (layout_url , headers = self ._authentication_headers )
212
231
if layout_response .status_code != 200 :
213
- raise CmixError ('CMIX returned a non-200 response code: {} and error {}' .format (layout_response .status_code , layout_response .text ))
232
+ raise CmixError (
233
+ 'CMIX returned a non-200 response code: {} and error {}' .format (
234
+ layout_response .status_code ,
235
+ layout_response .text
236
+ )
237
+ )
214
238
layout_id = None
215
239
for layout in layout_response .json ():
216
240
if layout .get ('name' ) == 'Default' :
217
241
layout_id = layout .get ('id' )
218
242
if layout_id is None :
219
- raise CmixError ('Layouts response did not contain a "Default" layout. Response Code: {}, Body {}' .format (layout_response .status_code , layout_response .content ))
243
+ raise CmixError (
244
+ 'Layouts response did not contain a "Default" layout. Response Code: {}, Body {}' .format (
245
+ layout_response .status_code ,
246
+ layout_response .content
247
+ )
248
+ )
220
249
221
250
archive_json ['dataLayoutId' ] = layout_id
222
251
cda = CmixDataArchive .objects .create (
@@ -236,7 +265,9 @@ def update_archive_status(self, archive):
236
265
if layout_id is None :
237
266
raise CmixError ('Error while updating archie status: layout ID is None. Archive ID: {}' .format (archive .id ))
238
267
if archive_id is None :
239
- raise CmixError ('Error while updating archie status: CMIX archive ID is None. Pop Archive ID: {}' .format (archive .id ))
268
+ raise CmixError (
269
+ 'Error while updating archie status: CMIX archive ID is None. Pop Archive ID: {}' .format (archive .id )
270
+ )
240
271
archive_url = '{}/surveys/{}/data-layouts/{}/archives/{}' .format (
241
272
settings .CMIX_SERVICES ['survey' ]["BASE_URL" ],
242
273
archive .cmix_survey .cmix_id ,
@@ -271,7 +302,12 @@ def update_project(self, projectId, status=None):
271
302
url = '{}/projects/{}' .format (settings .CMIX_SERVICES ['survey' ]['BASE_URL' ], projectId )
272
303
response = requests .patch (url , json = payload_json , headers = self ._authentication_headers )
273
304
if response .status_code > 299 :
274
- raise CmixError ('CMIX returned an invalid response code during project update: HTTP {} and error {}' .format (response .status_code , response .text ))
305
+ raise CmixError (
306
+ 'CMIX returned an invalid response code during project update: HTTP {} and error {}' .format (
307
+ response .status_code ,
308
+ response .text
309
+ )
310
+ )
275
311
return response
276
312
277
313
def create_survey (self , survey , wave_number = None ):
0 commit comments