LORIS_candidates.checkPSCIDExist()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 42
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 42
rs 8.4426
c 0
b 0
f 0
cc 6
nop 2
1
import sys
2
import os
3
import json
4
import argparse
5
import getpass
6
import logging
7
from dotenv import load_dotenv
8
from LORIS_query import getCNBP, postCNBP, is_response_success
9
10
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
11
#logger = logging.getLogger('LORISQuery')
12
13
14
def check_DCCID(DCCID):
15
    """
16
    Check if DCCID id conform.
17
    :param DCCID:
18
    :return:
19
    """
20
    if not len(str(DCCID)) == 6:
21
        return False
22
    elif not str(DCCID).isnumeric():
23
        return False
24
    elif DCCID > 999999:
25
        return False
26
    elif DCCID < 0:
27
        return False
28
    else:
29
        return True
30
31
32
def createCandidateCNBP(token, proposded_PSCID):
33
    """
34
    Create a candidate using the given PSCID
35
    :param proposded_PSCID:
36
    :return: DCCID
37
    """
38
    logger = logging.getLogger('LORIS_CreateCNBPCandidates')
39
    logger.info("Creating CNBP Candidates")
40
    logger.info(proposded_PSCID)
41
    PSCID_exist = checkPSCIDExist(token, proposded_PSCID)
42
    if PSCID_exist:
43
        return False
44
45
    Candidate = {}
46
    Candidate['Project'] = 'loris'
47
    Candidate['PSCID'] = proposded_PSCID
48
    Candidate['DoB'] = '2018-05-04'
49
    Candidate['Gender'] = 'Female'
50
51
    data = {"Candidate":Candidate}
52
53
    data_json = json.dumps(data)
54
55
    response_code, response = postCNBP(token, "candidates/", data_json)
56
    if not is_response_success(response_code, 201):
57
        return False, None
58
    elif response is not None:  # only try to decode if response is not empty!
59
        response_json = response.json()
60
        meta = response_json.get('Meta')
61
        CandID = meta.get('CandID')
62
        return True, CandID
63
    else:
64
        return False, None
65
66
67
def checkPSCIDExist(token, proposed_PSCID):
68
    '''
69
    Check if Site/Study already contain the PSCID
70
    :param site:
71
    :param study:
72
    :return: bool for connection, bool on if such PSCID (INSTITUTIONID + PROJECTID + SUBJECTID) exist already.
73
    '''
74
    logger = logging.getLogger('LORIS_checkPSCIDExist')
75
    logger.info("Checking if PSCID exist: "+proposed_PSCID)
76
    load_dotenv()
77
    institution_check = os.getenv("institutionID")
78
79
80
    #Get list of projects
81
    response_success, loris_project = getCNBP(token, r"projects/loris")
82
    if not response_success:
83
        return response_success, None
84
85
    #Get list of candidates (Candidates in v0.0.1)
86
    candidates = loris_project.get("Candidates")
87
    logger.info(candidates)
88
89
    for DCCID in candidates: #these candidates should really be only from the same ID regions.
90
        response_success, candidate_json = getCNBP(token, r"candidates/"+DCCID)
91
        if not response_success:
92
            return response_success, False
93
        # Each site following the naming convention should have SITE prefix and PI prefix and PROJECT prefix to avoid collision
94
95
        # A site check here.
96
        candidate_meta = candidate_json.get("Meta")
97
        candidate_pscID = candidate_meta.get("PSCID")
98
99
        # Not gonna check is institution ID doesn't even match.
100
        if candidate_pscID[0:3] != institution_check:
101
            continue
102
103
        elif candidate_pscID == proposed_PSCID:
104
            return response_success, True
105
106
            #latest_timepoint = findLatestTimePoint(DCCID)
107
108
    return False
109
110
111
def checkDCCIDExist(token, proposed_DCCID):
112
    """
113
    Check if Site/Study already contain the PSCID
114
    :param token: 
115
    :param proposed_DCCID: 
116
    :return: 
117
    """
118
    logger = logging.getLogger('LORIS_checkDCCIDExist')
119
    logger.info("Checking if DCCID exist: "+str(proposed_DCCID))
120
    load_dotenv()
121
    institution_check = os.getenv("institutionID")
122
123
    assert (check_DCCID(proposed_DCCID))
124
125
    #Get list of projects
126
    response, loris_project = getCNBP(token, r"projects/loris")
127
    response_success = is_response_success(response, 200)
128
129
    if not response_success:
130
        logger.info("FAILED log response: " + str(response))
131
        return response_success, None
132
133
    #Get list of candidates (Candidates in v0.0.1)
134
    candidates = loris_project.get("Candidates")
135
    logger.info(candidates)
136
137
    for DCCID in candidates:
138
        if str(proposed_DCCID) == DCCID:
139
            return response_success, True
140
        else:
141
            continue
142
    return response_success, False