15.2.138. camcops_server.cc_modules.cc_policy¶
camcops_server/cc_modules/cc_policy.py
Copyright (C) 2012, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
This file is part of CamCOPS.
CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
Represents ID number policies.
Note that the upload script should NOT attempt to verify patients against the ID policy, not least because tablets are allowed to upload task data (in a separate transaction) before uploading patients; referential integrity would be very hard to police. So the tablet software deals with ID compliance. (Also, the superuser can change the server’s ID policy retrospectively!)
Both the client and the server do policy tokenizing and can check patient info against policies. The server has additional code to answer questions like “is this policy valid?” (in general and in the context of the server’s configuration).
- class camcops_server.cc_modules.cc_policy.PatientInfoPresence(present: Optional[Dict[int, camcops_server.cc_modules.cc_policy.QuadState]] = None, default: camcops_server.cc_modules.cc_policy.QuadState = <camcops_server.cc_modules.cc_policy.QuadState object>)[source]¶
Represents simply the presence/absence of different kinds of information about a patient.
- __init__(present: Optional[Dict[int, camcops_server.cc_modules.cc_policy.QuadState]] = None, default: camcops_server.cc_modules.cc_policy.QuadState = <camcops_server.cc_modules.cc_policy.QuadState object>) None [source]¶
- Parameters
present – map from token to
QuadState
default – default
QuadState
to return if unspecified
- any_idnum_present() camcops_server.cc_modules.cc_policy.QuadState [source]¶
Is at least one ID number present?
- idnum_present(which_idnum: int) camcops_server.cc_modules.cc_policy.QuadState [source]¶
Is the specified ID number type present?
- is_present(token: int, default: Optional[camcops_server.cc_modules.cc_policy.QuadState] = None) camcops_server.cc_modules.cc_policy.QuadState [source]¶
Is information represented by a particular token present?
- Parameters
token – token to check for; e.g.
TK_FORENAME
default – default
QuadState
to return if unspecified; if this is None,self.default
is used.
- Returns
a
QuadState
- classmethod make_from_ptinfo(ptinfo: camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo, policy_mentioned_idnums: List[int]) camcops_server.cc_modules.cc_policy.PatientInfoPresence [source]¶
Returns a
PatientInfoPresence
representing whether different kinds of information about the patient are present or not.
- classmethod make_uncaring() camcops_server.cc_modules.cc_policy.PatientInfoPresence [source]¶
Makes a
PatientInfoPresence
that doesn’t care about anything.
- classmethod make_uncaring_except(token: int, present: camcops_server.cc_modules.cc_policy.QuadState) camcops_server.cc_modules.cc_policy.PatientInfoPresence [source]¶
Make a
PatientInfoPresence
that is uncaring except for one thing, specified by token.
- class camcops_server.cc_modules.cc_policy.TokenizedPolicy(policy: str)[source]¶
Represents a tokenized ID policy.
A tokenized policy is a policy represented by a sequence of integers; 0 means “bad token”; negative numbers represent fixed things like “forename” or “left parenthesis” or “and”; positive numbers represent ID number types.
- compatible_with_tablet_id_policy(valid_idnums: List[int], verbose: bool = False) bool [source]¶
Is this policy compatible with
TABLET_ID_POLICY
?The “self” policy may be MORE restrictive than the tablet minimum ID policy, but may not be LESS restrictive.
- Parameters
valid_idnums – ID number types that are valid on the server
verbose – report reasons to debug log
- contains_specific_idnum(which_idnum: int) bool [source]¶
Does the policy refer specifically to the given ID number type?
- Parameters
which_idnum – ID number type to test
- find_critical_single_numerical_id(valid_idnums: Optional[List[int]] = None, verbose: bool = False) Optional[int] [source]¶
If the policy involves a single mandatory ID number, return that ID number; otherwise return None.
- Parameters
valid_idnums – ID number types that are valid on the server
verbose – report reasons to debug log
- Returns
the single critical ID number type, or
None
- Return type
int
- classmethod get_tokenized_id_policy(policy: str) List[int] [source]¶
Takes a string policy and returns a tokenized policy, meaning a list of integer tokens, or
[]
.
- is_idnum_mandatory_in_policy(which_idnum: int, valid_idnums: List[int], verbose: bool = False) bool [source]¶
Is the ID number mandatory in the specified policy?
- Parameters
which_idnum – ID number type to test
valid_idnums – ID number types that are valid on the server
verbose – report reasons to debug log
- is_valid(valid_idnums: Optional[List[int]] = None, verbose: bool = False) bool [source]¶
Is the policy valid in the context of the ID types available in our database?
- Parameters
valid_idnums – optional list of valid ID number types
verbose – report reasons to debug log
- is_valid_for_idnums(valid_idnums: List[int], verbose: bool = False) bool [source]¶
Is the policy valid, given a list of valid ID number types?
Checks the following:
valid syntax
refers only to ID number types defined on the server
is compatible with the tablet ID policy
- Parameters
valid_idnums – ID number types that are valid on the server
verbose – report reasons to debug log
- static name_to_token(name: str) int [source]¶
Converts an upper-case string token name (such as
DOB
) to an integer token.
- relevant_idnums(valid_idnums: List[int]) List[int] [source]¶
Which ID numbers are relevant to this policy?
- Parameters
valid_idnums – ID number types that are valid on the server
- Returns
the subset of
valid_idnums
that is mentioned somehow in the policy
- require_valid_idnum_info() None [source]¶
Checks that set_valid_idnums() has been called properly, or raises
AssertionError
.
- satisfies_id_policy(ptinfo: camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo) bool [source]¶
Does the patient information in ptinfo satisfy the specified ID policy?
- Parameters
ptinfo – a camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo