@@ -39,7 +39,7 @@ def get_user_role(self, user: User, survey: Survey) -> Optional[str]:
39
39
try :
40
40
return survey .project .organisation .get_user_role (user )
41
41
except (
42
- AttributeError
42
+ AttributeError
43
43
): # In case user is AnonymousUser or organisation method fails
44
44
return None
45
45
@@ -90,12 +90,12 @@ def initialise_survey(self, user: User, project: Project, survey: Survey):
90
90
91
91
@requires_permission ("edit" , obj_param = "survey" )
92
92
def update_consent_demography_config (
93
- self ,
94
- user : User ,
95
- survey : Survey ,
96
- consent_config ,
97
- demography_config ,
98
- survey_body_path ,
93
+ self ,
94
+ user : User ,
95
+ survey : Survey ,
96
+ consent_config ,
97
+ demography_config ,
98
+ survey_body_path ,
99
99
) -> Survey :
100
100
survey .consent_config = consent_config
101
101
survey .demography_config = demography_config
@@ -108,9 +108,9 @@ def update_consent_demography_config(
108
108
with open (settings .SURVEY_TEMPLATE_DIR / body_path ) as f :
109
109
sort_config = json .load (f )
110
110
merged_sections = (
111
- survey .consent_config ["sections" ]
112
- + sort_config ["sections" ]
113
- + survey .demography_config ["sections" ]
111
+ survey .consent_config ["sections" ]
112
+ + sort_config ["sections" ]
113
+ + survey .demography_config ["sections" ]
114
114
)
115
115
survey .survey_config = {"sections" : merged_sections }
116
116
@@ -139,7 +139,7 @@ def duplicate_survey(self, user: User, survey: Survey):
139
139
return new_survey
140
140
141
141
def _create_survey_evidence_sections (
142
- self , survey : Survey , clear_existing_sections : bool = True
142
+ self , survey : Survey , clear_existing_sections : bool = True
143
143
):
144
144
if not survey .survey_config ["sections" ]:
145
145
raise ValueError ("No sections available" )
@@ -156,11 +156,11 @@ def _create_survey_evidence_sections(
156
156
survey_evidence_section .save ()
157
157
158
158
def _create_survey_improvement_sections (
159
- self , survey : Survey , clear_existing_sections : bool = True
159
+ self , survey : Survey , clear_existing_sections : bool = True
160
160
):
161
161
if clear_existing_sections :
162
162
for improve_section in SurveyImprovementPlanSection .objects .filter (
163
- survey = survey
163
+ survey = survey
164
164
):
165
165
improve_section .delete () # Delete all previous section first
166
166
@@ -175,18 +175,18 @@ def _create_survey_improvement_sections(
175
175
176
176
@requires_permission ("edit" , obj_param = "survey" )
177
177
def update_evidence_section (
178
- self , user : User , survey : Survey , evidence_section : SurveyEvidenceSection , text
178
+ self , user : User , survey : Survey , evidence_section : SurveyEvidenceSection , text
179
179
):
180
180
evidence_section .text = text
181
181
evidence_section .save ()
182
182
183
183
@requires_permission ("edit" , obj_param = "survey" )
184
184
def update_improvement_section (
185
- self ,
186
- user : User ,
187
- survey : Survey ,
188
- improve_section : SurveyImprovementPlanSection ,
189
- text ,
185
+ self ,
186
+ user : User ,
187
+ survey : Survey ,
188
+ improve_section : SurveyImprovementPlanSection ,
189
+ text ,
190
190
):
191
191
improve_section .plan = text
192
192
improve_section .save ()
@@ -280,7 +280,7 @@ def _is_extension_supported(self, file_name: str) -> bool:
280
280
281
281
@requires_permission ("edit" , obj_param = "survey" )
282
282
def add_uploaded_files (
283
- self , user : User , survey : Survey , files : Dict [str , UploadedFile ]
283
+ self , user : User , survey : Survey , files : Dict [str , UploadedFile ]
284
284
):
285
285
286
286
for field_name , uploaded_file in files .items ():
@@ -297,11 +297,11 @@ def add_uploaded_files(
297
297
298
298
@requires_permission ("edit" , obj_param = "survey" )
299
299
def add_uploaded_files_to_evidence_section (
300
- self ,
301
- user : User ,
302
- survey : Survey ,
303
- evidence_section : SurveyEvidenceSection ,
304
- files : Dict [str , UploadedFile ],
300
+ self ,
301
+ user : User ,
302
+ survey : Survey ,
303
+ evidence_section : SurveyEvidenceSection ,
304
+ files : Dict [str , UploadedFile ],
305
305
):
306
306
307
307
for field_name , uploaded_file in files .items ():
@@ -324,84 +324,16 @@ def remove_file(self, user: User, survey: Survey, file: SurveyFile):
324
324
325
325
@requires_permission ("edit" , obj_param = "survey" )
326
326
def remove_evidence_file (
327
- self , user : User , survey : Survey , file : SurveyEvidenceFile
327
+ self , user : User , survey : Survey , file : SurveyEvidenceFile
328
328
):
329
329
file .delete ()
330
330
331
- def generate_mock_responses (self , user : User , survey : Survey , num_responses ):
331
+ @staticmethod
332
+ def generate_mock_responses (user : User , survey : Survey , num_responses : int = 10 ):
332
333
"""
333
334
Generate a number of mock responses
334
335
"""
335
336
if not user .is_superuser :
336
- return PermissionDenied ("Must be superuser to use this feature" )
337
+ raise PermissionDenied ("Must be superuser to use this feature" )
337
338
338
- for i in range (num_responses ):
339
- self .accept_response (
340
- survey , responseValues = self .generate_mock_response (survey .survey_config )
341
- )
342
-
343
- def generate_mock_response (self , survey_config ):
344
- output_data = []
345
-
346
- for section in survey_config ["sections" ]:
347
- section_data_output = []
348
- for field in section ["fields" ]:
349
- section_data_output .append (self .generate_random_field_value (field ))
350
-
351
- output_data .append (section_data_output )
352
-
353
- return output_data
354
-
355
- def generate_random_field_value (self , field_config ):
356
- type = field_config ["type" ]
357
- if type == "radio" or type == "select" :
358
- # Pick one option
359
- num_options = len (field_config ["options" ])
360
- option_index = random .randint (0 , num_options - 1 )
361
- return str (field_config ["options" ][option_index ])
362
- elif type == "checkbox" :
363
- # Pick one random option
364
- num_options = len (field_config ["options" ])
365
- option_index = random .randint (0 , num_options - 1 )
366
- return [str (field_config ["options" ][option_index ])]
367
- elif type == "likert" :
368
- likert_output = []
369
- # Pick something
370
- for sublabel in field_config ["sublabels" ]:
371
- num_options = len (field_config ["options" ])
372
- option_index = random .randint (0 , num_options - 1 )
373
- likert_output .append (str (field_config ["options" ][option_index ]))
374
- return likert_output
375
-
376
- elif type == "text" :
377
- if "textType" in field_config :
378
- if field_config ["textType" ] == "INTEGER_TEXT" :
379
- min_value = (
380
- field_config ["minNumValue" ]
381
- if "minNumValue" in field_config
382
- else 0
383
- )
384
- max_value = (
385
- field_config ["maxNumValue" ]
386
- if "maxNumValue" in field_config
387
- else 100
388
- )
389
- return str (random .randint (min_value , max_value ))
390
- elif field_config ["textType" ] == "DECIMALS_TEXT" :
391
- min_value = (
392
- field_config ["minNumValue" ]
393
- if "minNumValue" in field_config
394
- else 0
395
- )
396
- max_value = (
397
- field_config ["maxNumValue" ]
398
- if "maxNumValue" in field_config
399
- else 100
400
- )
401
- return str (random .uniform (min_value , max_value ))
402
- elif field_config ["textType" ] == "EMAIL_TEXT" :
403
-
404
- else :
405
- return "Test plaintext field"
406
- else :
407
- return f"Test string for textarea field { field_config ['label' ]} "
339
+ survey .generate_mock_responses (num_responses = num_responses )
0 commit comments