@@ -34,33 +34,41 @@ def read_survey_templates(account_id, source_id, language_tag, token_info):
34
34
with Transaction () as t :
35
35
source_repo = SourceRepo (t )
36
36
source = source_repo .get_source (account_id , source_id )
37
+
37
38
if source is None :
38
39
return jsonify (code = 404 , message = "No source found" ), 404
40
+
39
41
template_repo = SurveyTemplateRepo (t )
42
+
40
43
if source .source_type == Source .SOURCE_TYPE_HUMAN :
41
- return jsonify ([template_repo .get_survey_template_link_info (x )
42
- for x in [
43
- SurveyTemplateRepo .VIOSCREEN_ID ,
44
- SurveyTemplateRepo .POLYPHENOL_FFQ_ID ,
45
- SurveyTemplateRepo .SPAIN_FFQ_ID ,
46
- SurveyTemplateRepo .BASIC_INFO_ID ,
47
- SurveyTemplateRepo .AT_HOME_ID ,
48
- SurveyTemplateRepo .LIFESTYLE_ID ,
49
- SurveyTemplateRepo .GUT_ID ,
50
- SurveyTemplateRepo .GENERAL_HEALTH_ID ,
51
- SurveyTemplateRepo .HEALTH_DIAG_ID ,
52
- SurveyTemplateRepo .ALLERGIES_ID ,
53
- SurveyTemplateRepo .DIET_ID ,
54
- SurveyTemplateRepo .DETAILED_DIET_ID ,
55
- SurveyTemplateRepo .SKIN_ID ,
56
- SurveyTemplateRepo .SKIN_HEALTH_DIAGNOSIS_ID ,
57
- SurveyTemplateRepo .OTHER_ID
58
- ]]), 200
44
+ template_ids = [
45
+ SurveyTemplateRepo .VIOSCREEN_ID ,
46
+ SurveyTemplateRepo .POLYPHENOL_FFQ_ID ,
47
+ SurveyTemplateRepo .SPAIN_FFQ_ID ,
48
+ SurveyTemplateRepo .BASIC_INFO_ID ,
49
+ SurveyTemplateRepo .AT_HOME_ID ,
50
+ SurveyTemplateRepo .LIFESTYLE_ID ,
51
+ SurveyTemplateRepo .GUT_ID ,
52
+ SurveyTemplateRepo .GENERAL_HEALTH_ID ,
53
+ SurveyTemplateRepo .HEALTH_DIAG_ID ,
54
+ SurveyTemplateRepo .ALLERGIES_ID ,
55
+ SurveyTemplateRepo .DIET_ID ,
56
+ SurveyTemplateRepo .DETAILED_DIET_ID ,
57
+ SurveyTemplateRepo .SKIN_ID ,
58
+ SurveyTemplateRepo .SKIN_HEALTH_DIAGNOSIS_ID ,
59
+ SurveyTemplateRepo .OTHER_ID
60
+ ]
61
+ if template_repo .check_display_skin_scoring_app (
62
+ account_id , source_id
63
+ ):
64
+ template_ids .append (SurveyTemplateRepo .SKIN_SCORING_APP_ID )
59
65
elif source .source_type == Source .SOURCE_TYPE_ANIMAL :
60
- return jsonify ([template_repo .get_survey_template_link_info (x )
61
- for x in [2 ]]), 200
66
+ template_ids = [2 ]
62
67
else :
63
- return jsonify ([]), 200
68
+ template_ids = []
69
+
70
+ return jsonify ([template_repo .get_survey_template_link_info (x )
71
+ for x in template_ids ]), 200
64
72
65
73
66
74
def _remote_survey_url_vioscreen (transaction , account_id , source_id ,
@@ -183,6 +191,23 @@ def _remote_survey_url_spain_ffq(transaction, account_id, source_id):
183
191
return SERVER_CONFIG ['spain_ffq_url' ]
184
192
185
193
194
+ def _remote_survey_url_skin_scoring_app (transaction ,
195
+ account_id ,
196
+ source_id ):
197
+ st_repo = SurveyTemplateRepo (transaction )
198
+
199
+ # Confirm that the user has credentials allocated
200
+ ssa_u , _ = st_repo .get_skin_scoring_app_credentials_if_exists (
201
+ account_id ,
202
+ source_id
203
+ )
204
+
205
+ if ssa_u is None :
206
+ raise NotFound ("Sorry, you were not allocated credentials" )
207
+
208
+ return SERVER_CONFIG ['skin_app_url' ]
209
+
210
+
186
211
def read_survey_template (account_id , source_id , survey_template_id ,
187
212
language_tag , token_info , survey_redirect_url = None ,
188
213
vioscreen_ext_sample_id = None ,
@@ -222,6 +247,11 @@ def read_survey_template(account_id, source_id, survey_template_id,
222
247
url = _remote_survey_url_spain_ffq (t ,
223
248
account_id ,
224
249
source_id )
250
+ elif survey_template_id == \
251
+ SurveyTemplateRepo .SKIN_SCORING_APP_ID :
252
+ url = _remote_survey_url_skin_scoring_app (t ,
253
+ account_id ,
254
+ source_id )
225
255
else :
226
256
raise ValueError (f"Cannot generate URL for survey "
227
257
f"{ survey_template_id } " )
@@ -501,3 +531,59 @@ def read_myfoodrepo_available_slots():
501
531
resp = jsonify (code = 200 , number_of_available_slots = available ,
502
532
total_number_of_slots = total )
503
533
return resp , 200
534
+
535
+
536
+ def get_skin_scoring_app_credentials (account_id , source_id , token_info ):
537
+ _validate_account_access (token_info , account_id )
538
+
539
+ with Transaction () as t :
540
+ st_repo = SurveyTemplateRepo (t )
541
+ ssa_u , ssa_s = st_repo .get_skin_scoring_app_credentials_if_exists (
542
+ account_id , source_id
543
+ )
544
+ response_obj = {
545
+ "app_username" : ssa_u ,
546
+ "app_studycode" : ssa_s
547
+ }
548
+ return jsonify (response_obj ), 200
549
+
550
+
551
+ def post_skin_scoring_app_credentials (account_id , source_id , token_info ):
552
+ _validate_account_access (token_info , account_id )
553
+
554
+ with Transaction () as t :
555
+ st_repo = SurveyTemplateRepo (t )
556
+
557
+ # First, confirm that the source doesn't already have credentials
558
+ ssa_u , _ = st_repo .get_skin_scoring_app_credentials_if_exists (
559
+ account_id , source_id
560
+ )
561
+
562
+ # This shouldn't happen, but if it does, return an error
563
+ if ssa_u is not None :
564
+ return jsonify (
565
+ code = 400 ,
566
+ message = "Credentials already exist"
567
+ ), 400
568
+
569
+ # Now, try to allocate credentials and create an entry in the skin
570
+ # scoring app registry table
571
+ ssa_u , ssa_s = st_repo .create_skin_scoring_app_entry (
572
+ account_id , source_id
573
+ )
574
+ t .commit ()
575
+
576
+ if ssa_u is None :
577
+ # No credentials were available
578
+ return jsonify (
579
+ code = 404 ,
580
+ message = "No credentials available"
581
+ ), 404
582
+ else :
583
+ # Credentials were successfully allocated
584
+ return jsonify (
585
+ {
586
+ "app_username" : ssa_u ,
587
+ "app_studycode" : ssa_s
588
+ }
589
+ ), 201
0 commit comments