13.2.55. camcops_server.cc_modules.cc_policy

camcops_server/cc_modules/cc_policy.py


Copyright (C) 2012-2019 Rudolf Cardinal (rudolf@pobox.com).

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 <http://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.PolicyTests(methodName='runTest')[source]

Unit tests.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

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]) → 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
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: List[int]) → Optional[int][source]

If the policy involves a single mandatory ID number, return that ID number; otherwise return None.

find_critical_single_numerical_id_from_req(req: CamcopsRequest) → Optional[int][source]

Return the single critical required ID number type, if one exists.

Parameters:req – a camcops_server.cc_modules.cc_request.CamcopsRequest
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 [].

id_policy_chunk(policy: List[int], ptinfo: camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo) → Optional[bool][source]

Applies the policy to the patient info in ptinfo. Can be used recursively.

Parameters:
  • policy – a tokenized policy
  • ptinfo – a camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo
Returns:

True if the ptinfo satisfies the policy; False if it doesn’t; None if there was an error.

Return type:

bool

id_policy_content(policy: List[int], ptinfo: camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo, start: int) → Tuple[Optional[bool], int][source]

Applies part of a policy to ptinfo. The part of policy pointed to by start represents something – “content” – that should return a value (not an operator, for example). Called by id_policy_chunk() (q.v.).

Parameters:
  • policy – a tokenized policy (list of integers)
  • ptinfo – a camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo
  • start – zero-based index of the first token to check
Returns:

chunk_value, next_index. chunk_value is True if the specified chunk is satisfied by the ptinfo, False if it isn’t, and None if there was an error. next_index is the index of the next token after this chunk.

Return type:

tuple

id_policy_element(ptinfo: camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo, token: int) → Optional[bool][source]

Returns the “value” of a content token as judged against the patient information. For example, if the patient information contains a date of birth, a TK_DOB token will evaluate to True.

Parameters:
  • ptinfo – a camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo
  • token – an integer token from the policy
Returns:

whether the token’s information is present in the ptinfo (or None if it was not a content token)

Return type:

bool

classmethod id_policy_op(policy: List[int], start: int) → Tuple[Optional[int], int][source]

Returns an operator from the policy, beginning at index start, or None if there wasn’t an operator there.

policy:
a tokenized policy (list of integers)
start:
zero-based index of the first token to check
Returns:operator, next_index. operator is the operator’s integer token or None. next_index gives the next index of the policy to check at.
Return type:tuple
is_idnum_mandatory_in_policy(which_idnum: int, valid_idnums: List[int]) → 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
is_syntactically_valid() → bool[source]

Is the policy syntactically valid? This is a basic check.

is_valid(valid_idnums: List[int]) → 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
is_valid_from_req(req: CamcopsRequest) → bool[source]

Is the policy valid in the context of the ID types available in our database?

Parameters:req – a camcops_server.cc_modules.cc_request.CamcopsRequest
less_restrictive_than(other: camcops_server.cc_modules.cc_policy.TokenizedPolicy, valid_idnums: List[int]) → bool[source]

Is this (“self”) policy at least as restrictive as the “other” policy?

Parameters:
  • other – the other policy
  • valid_idnums – ID number types that are valid on the server
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
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
camcops_server.cc_modules.cc_policy.gen_all_combinations(x: Sequence[int]) → Generator[[Iterable[int], None], None][source]

Yields all combinations of x, of all lengths, including zero.

You can apply the reversed() iterator to get them in reverse order.

camcops_server.cc_modules.cc_policy.gen_test_bpis(which_idnums_present_options: Iterable[Iterable[int]], forename_present_options: Iterable[bool] = None, surname_present_options: Iterable[bool] = None, sex_present_options: Iterable[bool] = None, dob_present_options: Iterable[bool] = None, address_present_options: Iterable[bool] = None, gp_present_options: Iterable[bool] = None, otherdetails_present_options: Iterable[bool] = None) → Generator[[camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo, None], None][source]

Generates test camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo objects.

Parameters:
  • which_idnums_present_options – list of options, where each option is a list of ID number types that should be present
  • forename_present_options – options for “forename” present, e.g. [True], [True, False]; if None is passed, the default of [True, False] is used
  • surname_present_options – as for forename_present_options
  • sex_present_options – as for forename_present_options
  • dob_present_options – as for forename_present_options
  • address_present_options – as for forename_present_options
  • gp_present_options – as for forename_present_options
  • otherdetails_present_options – as for forename_present_options
Yields:

camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo objects, for all combinations of the options requested