@@ -34,31 +34,40 @@ 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 .OTHER_ID
56
- ]]), 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 .OTHER_ID
58
+ ]
59
+ if template_repo .check_display_skin_scoring_app (
60
+ account_id , source_id
61
+ ):
62
+ template_ids .append (SurveyTemplateRepo .SKIN_SCORING_APP_ID )
63
+
57
64
elif source .source_type == Source .SOURCE_TYPE_ANIMAL :
58
- return jsonify ([template_repo .get_survey_template_link_info (x )
59
- for x in [2 ]]), 200
65
+ template_ids = [2 ]
60
66
else :
61
- return jsonify ([]), 200
67
+ template_ids = []
68
+
69
+ return jsonify ([template_repo .get_survey_template_link_info (x )
70
+ for x in template_ids ]), 200
62
71
63
72
64
73
def _remote_survey_url_vioscreen (transaction , account_id , source_id ,
@@ -181,6 +190,23 @@ def _remote_survey_url_spain_ffq(transaction, account_id, source_id):
181
190
return SERVER_CONFIG ['spain_ffq_url' ]
182
191
183
192
193
+ def _remote_survey_url_skin_scoring_app (transaction ,
194
+ account_id ,
195
+ source_id ):
196
+ st_repo = SurveyTemplateRepo (transaction )
197
+
198
+ # Confirm that the user has credentials allocated
199
+ ssa_u , _ = st_repo .get_skin_scoring_app_credentials_if_exists (
200
+ account_id ,
201
+ source_id
202
+ )
203
+
204
+ if ssa_u is None :
205
+ raise NotFound ("Sorry, you were not allocated credentials" )
206
+
207
+ return SERVER_CONFIG ['skin_app_url' ]
208
+
209
+
184
210
def read_survey_template (account_id , source_id , survey_template_id ,
185
211
language_tag , token_info , survey_redirect_url = None ,
186
212
vioscreen_ext_sample_id = None ,
@@ -220,6 +246,11 @@ def read_survey_template(account_id, source_id, survey_template_id,
220
246
url = _remote_survey_url_spain_ffq (t ,
221
247
account_id ,
222
248
source_id )
249
+ elif survey_template_id == \
250
+ SurveyTemplateRepo .SKIN_SCORING_APP_ID :
251
+ url = _remote_survey_url_skin_scoring_app (t ,
252
+ account_id ,
253
+ source_id )
223
254
else :
224
255
raise ValueError (f"Cannot generate URL for survey "
225
256
f"{ survey_template_id } " )
@@ -499,3 +530,59 @@ def read_myfoodrepo_available_slots():
499
530
resp = jsonify (code = 200 , number_of_available_slots = available ,
500
531
total_number_of_slots = total )
501
532
return resp , 200
533
+
534
+
535
+ def get_skin_scoring_app_credentials (account_id , source_id , token_info ):
536
+ _validate_account_access (token_info , account_id )
537
+
538
+ with Transaction () as t :
539
+ st_repo = SurveyTemplateRepo (t )
540
+ ssa_u , ssa_s = st_repo .get_skin_scoring_app_credentials_if_exists (
541
+ account_id , source_id
542
+ )
543
+ response_obj = {
544
+ "app_username" : ssa_u ,
545
+ "app_studycode" : ssa_s
546
+ }
547
+ return jsonify (response_obj ), 200
548
+
549
+
550
+ def post_skin_scoring_app_credentials (account_id , source_id , token_info ):
551
+ _validate_account_access (token_info , account_id )
552
+
553
+ with Transaction () as t :
554
+ st_repo = SurveyTemplateRepo (t )
555
+
556
+ # First, confirm that the source doesn't already have credentials
557
+ ssa_u , _ = st_repo .get_skin_scoring_app_credentials_if_exists (
558
+ account_id , source_id
559
+ )
560
+
561
+ # This shouldn't happen, but if it does, return an error
562
+ if ssa_u is not None :
563
+ return jsonify (
564
+ code = 400 ,
565
+ message = "Credentials already exist"
566
+ ), 400
567
+
568
+ # Now, try to allocate credentials and create an entry in the skin
569
+ # scoring app registry table
570
+ ssa_u , ssa_s = st_repo .create_skin_scoring_app_entry (
571
+ account_id , source_id
572
+ )
573
+ t .commit ()
574
+
575
+ if ssa_u is None :
576
+ # No credentials were available
577
+ return jsonify (
578
+ code = 404 ,
579
+ message = "No credentials available"
580
+ ), 404
581
+ else :
582
+ # Credentials were successfully allocated
583
+ return jsonify (
584
+ {
585
+ "app_username" : ssa_u ,
586
+ "app_studycode" : ssa_s
587
+ }
588
+ ), 201
0 commit comments