3
3
from microsetta_private_api import localization
4
4
from microsetta_private_api .api ._account import \
5
5
_validate_account_access
6
- from microsetta_private_api .model .consent import ConsentSignature
6
+ from microsetta_private_api .model .consent import ConsentSignature ,\
7
+ HUMAN_CONSENT_AGE_GROUPS
7
8
from microsetta_private_api .repo .consent_repo import ConsentRepo
9
+ from microsetta_private_api .repo .source_repo import SourceRepo
8
10
from microsetta_private_api .repo .transaction import Transaction
9
11
from microsetta_private_api .api .literals import CONSENT_DOC_NOT_FOUND_MSG
10
12
from werkzeug .exceptions import NotFound
13
+ from microsetta_private_api .api .literals import SRC_NOT_FOUND_MSG
11
14
12
15
13
16
def render_consent_doc (account_id , language_tag , token_info ):
@@ -47,7 +50,12 @@ def check_consent_signature(account_id, source_id, consent_type, token_info):
47
50
48
51
with Transaction () as t :
49
52
consent_repo = ConsentRepo (t )
50
- res = consent_repo .is_consent_required (source_id , consent_type )
53
+ source_repo = SourceRepo (t )
54
+ source = source_repo .get_source (account_id , source_id )
55
+ age_range = source .source_data .age_range
56
+ res = consent_repo .is_consent_required (
57
+ source_id , age_range , consent_type
58
+ )
51
59
52
60
return jsonify ({"result" : res }), 200
53
61
@@ -56,6 +64,54 @@ def sign_consent_doc(account_id, source_id, consent_type, body, token_info):
56
64
_validate_account_access (token_info , account_id )
57
65
58
66
with Transaction () as t :
67
+ # Sources are now permitted to update their age range, but only if it
68
+ # moves the source to an older age group. For this purpose, "legacy"
69
+ # is treated as younger than "0-6", as they're choosing an age group
70
+ # for the first time.
71
+ source_repo = SourceRepo (t )
72
+ source = source_repo .get_source (account_id , source_id )
73
+ if source is None :
74
+ return jsonify (code = 404 , message = SRC_NOT_FOUND_MSG ), 404
75
+
76
+ if source .source_data .age_range != body ['age_range' ]:
77
+ # Let's make sure it's a valid change. First, grab the index of
78
+ # their current age range.
79
+ try :
80
+ cur_age_index = HUMAN_CONSENT_AGE_GROUPS .index (
81
+ source .source_data .age_range
82
+ )
83
+ except ValueError :
84
+ # Catch any sources that have a blank, "legacy", or faulty
85
+ # age_range
86
+ cur_age_index = - 1
87
+
88
+ # Next, make sure their new age range is valid
89
+ try :
90
+ new_age_index = HUMAN_CONSENT_AGE_GROUPS .index (
91
+ body ['age_range' ]
92
+ )
93
+ except ValueError :
94
+ # Shouldn't reach this point, but if we do, reject it
95
+ return jsonify (
96
+ code = 403 , message = "Invalid age_range update"
97
+ ), 403
98
+
99
+ # Finally, make sure the new age_range isn't younger than the
100
+ # current age_range.
101
+ if new_age_index < cur_age_index :
102
+ return jsonify (
103
+ code = 403 , message = "Invalid age_range update"
104
+ ), 403
105
+
106
+ update_success = source_repo .update_source_age_range (
107
+ source_id , body ['age_range' ]
108
+ )
109
+ if not update_success :
110
+ return jsonify (
111
+ code = 403 , message = "Invalid age_range update"
112
+ ), 403
113
+
114
+ # Now back to the normal flow of signing a consent document
59
115
consent_repo = ConsentRepo (t )
60
116
sign_id = str (uuid .uuid4 ())
61
117
consent_sign = ConsentSignature .from_dict (body , source_id , sign_id )
0 commit comments