|
9 | 9 | from fhir.resources.valueset import ValueSet |
10 | 10 | from fhir.resources.practitioner import Practitioner |
11 | 11 | from pydantic import ValidationError |
12 | | -from pydantic.functional_validators import model_validator |
| 12 | +from pydantic import model_validator |
13 | 13 | from django.db import connection |
14 | 14 | from .models import Nucc, C80PracticeCodes |
15 | 15 |
|
16 | 16 |
|
17 | | -@model_validator(mode="after") |
18 | | -def verify_codes(self): |
19 | | - """ |
20 | | - Verifies the code items present in the valueset |
21 | | -
|
22 | | - Args: |
23 | | - self (ValueSet): Pydantic Object representing a FHIR ValueSet |
| 17 | +class NPDValueSet(ValueSet): |
| 18 | + @model_validator(mode="after") |
| 19 | + def verify_codes(self): |
| 20 | + """ |
| 21 | + Verifies the code items present in the valueset |
24 | 22 |
|
25 | | - Raises: |
26 | | - ValidationError: raises a validationError if the codes are not part of a |
27 | | - known valid code |
28 | | - """ |
| 23 | + Args: |
| 24 | + self (ValueSet): Pydantic Object representing a FHIR ValueSet |
29 | 25 |
|
30 | | - def get_nucc_codes_from_db(): |
31 | | - """ |
32 | | - Function that fetches nucc codes from the db |
| 26 | + Raises: |
| 27 | + ValidationError: raises a validationError if the codes are not part of a |
| 28 | + known valid code |
33 | 29 | """ |
34 | 30 |
|
35 | | - data = dict( |
36 | | - Nucc.objects.values_list("code", "display_name") |
37 | | - ) |
38 | | - |
39 | | - return data |
| 31 | + def get_nucc_codes_from_db(): |
| 32 | + """ |
| 33 | + Function that fetches nucc codes from the db |
| 34 | + """ |
40 | 35 |
|
| 36 | + data = dict( |
| 37 | + Nucc.objects.values_list("code", "display_name") |
| 38 | + ) |
41 | 39 |
|
42 | | - def get_c80_codes_from_db(): |
43 | | - """ |
44 | | - Function that fetches c80 codes from db |
45 | | - """ |
| 40 | + return data |
46 | 41 |
|
47 | | - data = dict( |
48 | | - C80PracticeCodes.objects.values_list("code", "display_name") |
49 | | - ) |
50 | 42 |
|
51 | | - return data |
| 43 | + def get_c80_codes_from_db(): |
| 44 | + """ |
| 45 | + Function that fetches c80 codes from db |
| 46 | + """ |
52 | 47 |
|
53 | | - nucc_codes = get_nucc_codes_from_db() |
54 | | - c80_codes = get_c80_codes_from_db() |
| 48 | + data = dict( |
| 49 | + C80PracticeCodes.objects.values_list("code", "display_name") |
| 50 | + ) |
55 | 51 |
|
56 | | - for include_index, code_item in enumerate(self.compose['include']): |
57 | | - #Check if code in ValueSet is part of nucc |
| 52 | + return data |
58 | 53 |
|
59 | | - for concept_index, code_value in enumerate(code_item['concept']): |
60 | | - try: |
61 | | - if "nucc.org/provider-taxonomy" in code_item['system']: |
62 | | - assert code_value['code'] in nucc_codes |
63 | | - elif "snomed.info/sct" in code_item['system']: |
64 | | - assert code_value['code'] in c80_codes |
65 | | - except AssertionError as e: |
66 | | - raise ValueError( |
67 | | - f"Code {code_value['code']} is not a valid {code_value['system']} code!" |
68 | | - f"(at include[{include_index}].concept[{concept_index}])" |
69 | | - ) from e |
70 | | - |
71 | | - return self |
| 54 | + nucc_codes = get_nucc_codes_from_db() |
| 55 | + c80_codes = get_c80_codes_from_db() |
72 | 56 |
|
73 | | -#ValueSet.add_root_validator(verify_codes, pre=False) |
74 | | -ValueSet.model_rebuild(validators=[verify_codes]) |
| 57 | + for include_index, code_item in enumerate(self.compose.include): |
| 58 | + #Check if code in ValueSet is part of nucc |
75 | 59 |
|
76 | | -@model_validator(mode="after") |
77 | | -def verify_identifier_code_from_data(self): |
78 | | - """ |
79 | | - Iterates through json data and verifies the identifiers present |
| 60 | + for concept_index, code_value in enumerate(code_item.concept): |
| 61 | + try: |
| 62 | + if "nucc.org/provider-taxonomy" in code_item.system: |
| 63 | + assert code_value.code in nucc_codes |
| 64 | + elif "snomed.info/sct" in code_item.system: |
| 65 | + assert code_value.code in c80_codes |
| 66 | + except AssertionError as e: |
| 67 | + raise ValueError( |
| 68 | + f"Code {code_value.code} is not a valid {code_item.system} code!" |
| 69 | + f"(at include[{include_index}].concept[{concept_index}])" |
| 70 | + ) from e |
| 71 | + |
| 72 | + return self |
80 | 73 |
|
81 | | - Args: |
82 | | - data (dict): json dictionary representing fhir data |
83 | | - """ |
| 74 | +#ValueSet.add_root_validator(verify_codes, pre=False) |
| 75 | +#ValueSet.model_rebuild(validators=[verify_codes]) |
84 | 76 |
|
85 | | - def verify_npi(npi,position): |
| 77 | +class NPDPractitioner(Practitioner): |
| 78 | + @model_validator(mode="after") |
| 79 | + def verify_identifier_code_from_data(self): |
86 | 80 | """ |
87 | | - Checks the npi code against various checks and throws errors if they fail. |
| 81 | + Iterates through json data and verifies the identifiers present |
88 | 82 |
|
89 | 83 | Args: |
90 | | - npi (str): npi identifier string |
91 | | - position (int): index where npi is found in the json |
92 | | -
|
93 | | - Raises: |
94 | | - ValidationError: Error thrown when npi value is out of range |
95 | | - ValidationError: Error thrown when npi value fails luhn algo |
| 84 | + data (dict): json dictionary representing fhir data |
96 | 85 | """ |
97 | 86 |
|
98 | | - def is_valid_npi_format(npi_value): |
| 87 | + def verify_npi(npi,position): |
99 | 88 | """ |
100 | | - Checks npi value string for correct range and digits |
| 89 | + Checks the npi code against various checks and throws errors if they fail. |
101 | 90 |
|
102 | 91 | Args: |
103 | | - npi_value (str): npi identifier string |
| 92 | + npi (str): npi identifier string |
| 93 | + position (int): index where npi is found in the json |
104 | 94 |
|
105 | | - Returns: |
106 | | - bool: True if value is valid |
| 95 | + Raises: |
| 96 | + ValidationError: Error thrown when npi value is out of range |
| 97 | + ValidationError: Error thrown when npi value fails luhn algo |
107 | 98 | """ |
108 | 99 |
|
109 | | - digits_only = re.sub(r'\D', '', str(npi_value)) |
110 | | - npi_num = int(digits_only) |
111 | | - return 999999999 <= npi_num <= 10000000000 |
| 100 | + def is_valid_npi_format(npi_value): |
| 101 | + """ |
| 102 | + Checks npi value string for correct range and digits |
112 | 103 |
|
113 | | - #NPI luhn algo defined here: |
114 | | - #https://www.cms.gov/Regulations-and-Guidance/Administrative-Simplification/NationalProvIdentStand/Downloads/NPIcheckdigit.pdf |
115 | | - def npi_check_luhn_algorithm(npi_value): |
116 | | - """ |
117 | | - Checks npi value string based on given luhn algorithm |
| 104 | + Args: |
| 105 | + npi_value (str): npi identifier string |
118 | 106 |
|
119 | | - Transforms every other digit and sums them together plus 24 |
| 107 | + Returns: |
| 108 | + bool: True if value is valid |
| 109 | + """ |
120 | 110 |
|
121 | | - If the result is divisible by 10 then the npi value is valid |
| 111 | + digits_only = re.sub(r'\D', '', str(npi_value)) |
| 112 | + npi_num = int(digits_only) |
| 113 | + return 999999999 <= npi_num <= 10000000000 |
122 | 114 |
|
123 | | - Args: |
124 | | - npi_value (str): npi identifier string |
125 | | - """ |
| 115 | + #NPI luhn algo defined here: |
| 116 | + #https://www.cms.gov/Regulations-and-Guidance/Administrative-Simplification/NationalProvIdentStand/Downloads/NPIcheckdigit.pdf |
| 117 | + def npi_check_luhn_algorithm(npi_value): |
| 118 | + """ |
| 119 | + Checks npi value string based on given luhn algorithm |
| 120 | +
|
| 121 | + Transforms every other digit and sums them together plus 24 |
| 122 | +
|
| 123 | + If the result is divisible by 10 then the npi value is valid |
| 124 | +
|
| 125 | + Args: |
| 126 | + npi_value (str): npi identifier string |
| 127 | + """ |
126 | 128 |
|
127 | | - def transform(d): |
128 | | - return d * 2 if d < 5 else d * 2 - 9 |
| 129 | + def transform(d): |
| 130 | + return d * 2 if d < 5 else d * 2 - 9 |
129 | 131 |
|
130 | | - digits_only = re.sub(r'\D', '', str(npi_value)) |
| 132 | + digits_only = re.sub(r'\D', '', str(npi_value)) |
131 | 133 |
|
132 | | - digits = [int(ch) for ch in digits_only[:10]] |
| 134 | + digits = [int(ch) for ch in digits_only[:10]] |
133 | 135 |
|
134 | | - total = sum( |
135 | | - transform(d) if i % 2 == 0 else d |
136 | | - for i, d in enumerate(digits) |
137 | | - ) + 24 |
| 136 | + total = sum( |
| 137 | + transform(d) if i % 2 == 0 else d |
| 138 | + for i, d in enumerate(digits) |
| 139 | + ) + 24 |
138 | 140 |
|
139 | | - return total % 10 == 0 |
| 141 | + return total % 10 == 0 |
140 | 142 |
|
141 | | - if not is_valid_npi_format(npi): |
142 | | - raise ValueError(f"NPI '{npi}' (identifier[{position}]) invalid format") |
143 | | - if not npi_check_luhn_algorithm(npi): |
144 | | - raise ValueError(f"NPI '{npi}' (identifier[{position}]) failed Luhn check") |
| 143 | + if not is_valid_npi_format(npi): |
| 144 | + raise ValueError(f"NPI '{npi}' (identifier[{position}]) invalid format") |
| 145 | + if not npi_check_luhn_algorithm(npi): |
| 146 | + raise ValueError(f"NPI '{npi}' (identifier[{position}]) failed Luhn check") |
145 | 147 |
|
146 | | - for identifier_index, identifier in enumerate(self.identifier): |
147 | | - if "hl7.org/fhir/sid/us-npi" in identifier['system']: |
148 | | - verify_npi(identifier['value'],identifier_index) |
149 | | - |
150 | | - return self |
| 148 | + for identifier_index, identifier in enumerate(self.identifier): |
| 149 | + if "hl7.org/fhir/sid/us-npi" in identifier['system']: |
| 150 | + verify_npi(identifier['value'],identifier_index) |
| 151 | + |
| 152 | + return self |
151 | 153 |
|
152 | | -Practitioner.model_rebuild(validators=[verify_identifier_code_from_data]) |
| 154 | +#Practitioner.model_rebuild(validators=[verify_identifier_code_from_data]) |
0 commit comments