Passed
Pull Request — master (#2)
by Yang
01:44
created

Python.LORIS.candidates.checkPSCIDExist()   B

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
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import os
3
import json
4
import logging
5
from dotenv import load_dotenv
6
from LORIS.query import getCNBP, postCNBP, is_response_success
0 ignored issues
show
introduced by
Unable to import 'LORIS.query'
Loading history...
7
8
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
9
#logger = logging.getLogger('LORISQuery')
10
11
12
def check_DCCID(DCCID):
0 ignored issues
show
Coding Style Naming introduced by
The name check_DCCID does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name DCCID does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
13
    """
14
    Check if DCCID id conform.
15
    :param DCCID:
16
    :return:
17
    """
18
    if not len(str(DCCID)) == 6:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
Unused Code introduced by
Consider changing "not len(str(DCCID)) == 6" to "len(str(DCCID)) != 6"
Loading history...
19
        return False
20
    elif not str(DCCID).isnumeric():
21
        return False
22
    elif DCCID > 999999:
23
        return False
24
    elif DCCID < 0:
25
        return False
26
    else:
27
        return True
28
29
30
def createCandidateCNBP(token, proposded_PSCID):
0 ignored issues
show
Coding Style Naming introduced by
The name createCandidateCNBP does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name proposded_PSCID does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
31
    """
32
    Create a candidate using the given PSCID
33
    :param proposded_PSCID:
34
    :return: DCCID
35
    """
36
    logger = logging.getLogger('LORIS_CreateCNBPCandidates')
37
    logger.info("Creating CNBP Candidates")
38
    logger.info(proposded_PSCID)
39
    PSCID_exist = checkPSCIDExist(token, proposded_PSCID)
0 ignored issues
show
Coding Style Naming introduced by
The name PSCID_exist does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
40
    if PSCID_exist:
41
        return False
42
43
    Candidate = {}
0 ignored issues
show
Coding Style Naming introduced by
The name Candidate does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
44
    Candidate['Project'] = 'loris'
45
    Candidate['PSCID'] = proposded_PSCID
46
    Candidate['DoB'] = '2018-05-04'
47
    Candidate['Gender'] = 'Female'
48
49
    data = {"Candidate":Candidate}
50
51
    data_json = json.dumps(data)
52
53
    response_code, response = postCNBP(token, "candidates/", data_json)
54
    if not is_response_success(response_code, 201):
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
55
        return False, None
56
    elif response is not None:  # only try to decode if response is not empty!
57
        response_json = response.json()
58
        meta = response_json.get('Meta')
59
        CandID = meta.get('CandID')
0 ignored issues
show
Coding Style Naming introduced by
The name CandID does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
60
        return True, CandID
61
    else:
62
        return False, None
63
64
65
def checkPSCIDExist(token, proposed_PSCID):
0 ignored issues
show
Coding Style Naming introduced by
The name checkPSCIDExist does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name proposed_PSCID does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
66
    '''
67
    Check if Site/Study already contain the PSCID
68
    :param site:
69
    :param study:
70
    :return: bool for connection, bool on if such PSCID (INSTITUTIONID + PROJECTID + SUBJECTID) exist already.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (110/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
71
    '''
72
    logger = logging.getLogger('LORIS_checkPSCIDExist')
73
    logger.info("Checking if PSCID exist: "+proposed_PSCID)
74
    load_dotenv()
75
    institution_check = os.getenv("institutionID")
76
77
78
    #Get list of projects
79
    response_success, loris_project = getCNBP(token, r"projects/loris")
80
    if not response_success:
81
        return response_success, None
82
83
    #Get list of candidates (Candidates in v0.0.1)
84
    candidates = loris_project.get("Candidates")
85
    logger.info(candidates)
86
87
    for DCCID in candidates: #these candidates should really be only from the same ID regions.
0 ignored issues
show
Coding Style Naming introduced by
The name DCCID does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
88
        response_success, candidate_json = getCNBP(token, r"candidates/"+DCCID)
89
        if not response_success:
90
            return response_success, False
91
        # Each site following the naming convention should have SITE prefix and PI prefix and PROJECT prefix to avoid collision
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (127/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
92
93
        # A site check here.
94
        candidate_meta = candidate_json.get("Meta")
95
        candidate_pscID = candidate_meta.get("PSCID")
0 ignored issues
show
Coding Style Naming introduced by
The name candidate_pscID does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
96
97
        # Not gonna check is institution ID doesn't even match.
98
        if candidate_pscID[0:3] != institution_check:
99
            continue
100
101
        elif candidate_pscID == proposed_PSCID:
102
            return response_success, True
103
104
            #latest_timepoint = findLatestTimePoint(DCCID)
105
106
    return False
107
108
109
def checkDCCIDExist(token, proposed_DCCID):
0 ignored issues
show
Coding Style Naming introduced by
The name checkDCCIDExist does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name proposed_DCCID does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
110
    """
111
    Check if Site/Study already contain the PSCID
112
    :param token: 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
113
    :param proposed_DCCID: 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
114
    :return: 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
115
    """
116
    logger = logging.getLogger('LORIS_checkDCCIDExist')
117
    logger.info("Checking if DCCID exist: "+str(proposed_DCCID))
118
    load_dotenv()
119
    institution_check = os.getenv("institutionID")
0 ignored issues
show
Unused Code introduced by
The variable institution_check seems to be unused.
Loading history...
120
121
    assert (check_DCCID(proposed_DCCID))
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after assert.
Loading history...
122
123
    #Get list of projects
124
    response, loris_project = getCNBP(token, r"projects/loris")
125
    response_success = is_response_success(response, 200)
126
127
    if not response_success:
128
        logger.info("FAILED log response: " + str(response))
129
        return response_success, None
130
131
    #Get list of candidates (Candidates in v0.0.1)
132
    candidates = loris_project.get("Candidates")
133
    logger.info(candidates)
134
135
    for DCCID in candidates:
0 ignored issues
show
Coding Style Naming introduced by
The name DCCID does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
136
        if str(proposed_DCCID) == DCCID:
137
            return response_success, True
138
        else:
139
            continue
140
    return response_success, False
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...