15.2.114. camcops_server.cc_modules.cc_patient


Copyright (C) 2012-2020 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 <https://www.gnu.org/licenses/>.


class camcops_server.cc_modules.cc_patient.DistinctPatientReport[source]

Report to show distinct patients.

get_query(req: CamcopsRequest) → sqlalchemy.sql.selectable.SelectBase[source]

Overriding this function is one way of providing a report. (The other is get_rows_colnames().)

To override this function, return the SQLAlchemy Base Select statement or the SQLAlchemy ORM Query to execute the report.

Parameters are passed in via the request.

classmethod title(req: CamcopsRequest) → str[source]

Descriptive title for display purposes.

class camcops_server.cc_modules.cc_patient.Patient(**kwargs)[source]

Class representing a patient.


A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

add_extra_idnum_info_to_row(row: Dict[str, Any]) → None[source]

For the DB_PATIENT_ID_PER_ROW export option. Adds additional ID number info to a row.


row – future database row, as a dictionary

apply_special_note(req: CamcopsRequest, note: str, audit_msg: str = 'Special note applied manually') → None[source]

Manually applies a special note to a patient. WRITES TO DATABASE.

audit(req: CamcopsRequest, details: str, from_console: bool = False) → None[source]

Audits an action to this patient.

delete_with_dependants(req: CamcopsRequest) → None[source]

Delete the patient with all its dependent objects.

gen_patient_idnums_even_noncurrent() → Generator[[camcops_server.cc_modules.cc_patientidnum.PatientIdNum, None], None][source]

Generates all PatientIdNum objects, including non-current ones.

get_address() → Optional[str][source]

Returns address (NOT necessarily web-safe).

get_age(req: CamcopsRequest, default: str = '') → Union[int, str][source]

Age (in whole years) today, or default.

get_age_at(when: Union[None, datetime.date, str], default: str = '') → Union[int, str][source]

Age (in whole years) at a particular date, or default.


Get basic identifying information, as a camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo object.

get_dob() → Optional[pendulum.date.Date][source]

Date of birth, as a a timezone-naive date.

get_dob_html(req: CamcopsRequest, longform: bool) → str[source]

HTML fragment for date of birth.

get_dob_str() → Optional[str][source]

Date of birth, as a string.

get_email() → Optional[str][source]

Returns email address

get_forename() → str[source]

Get forename (in upper case) or “”.

get_forename_surname() → str[source]

Get “Forename Surname” as a string, using “(UNKNOWN)” for missing details.

get_hl7_pid_segment(req: CamcopsRequest, recipient: ExportRecipient) → hl7.containers.Segment[source]

Get HL7 patient identifier (PID) segment.


a hl7.Segment object

get_iddesc(req: CamcopsRequest, which_idnum: int) → Optional[str][source]

Get value of a specific ID description, if present.

get_idnum_object(which_idnum: int) → Optional[camcops_server.cc_modules.cc_patientidnum.PatientIdNum][source]

Gets the PatientIdNum object for a specified which_idnum, or None.

get_idnum_objects() → List[camcops_server.cc_modules.cc_patientidnum.PatientIdNum][source]

Returns all PatientIdNum objects for the patient.

These are SQLAlchemy ORM objects.

get_idnum_raw_values_only() → List[int][source]

Get all plain ID number values (ignoring which ID number type they represent) for the patient.

get_idnum_references() → List[camcops_server.cc_modules.cc_simpleobjects.IdNumReference][source]

Returns all camcops_server.cc_modules.cc_simpleobjects.IdNumReference objects for the patient.

These are simple which_idnum/idnum_value pairs.

get_idnum_value(which_idnum: int) → Optional[int][source]

Get value of a specific ID number, if present.

get_idshortdesc(req: CamcopsRequest, which_idnum: int) → Optional[str][source]

Get value of a specific ID short description, if present.

get_letter_style_identifiers(req: CamcopsRequest) → str[source]

Our best guess at the kind of text you’d put in a clinical letter to say “it’s about this patient”.


Bob Smith (1 Jan 1950, RiO number 123, NHS number 456)
classmethod get_patient_by_id_device_era(dbsession: sqlalchemy.orm.session.Session, client_id: int, device_id: int, era: str) → Optional[camcops_server.cc_modules.cc_patient.Patient][source]

Fetch a patient by the client ID, device ID, and era.

classmethod get_patient_by_pk(dbsession: sqlalchemy.orm.session.Session, server_pk: int) → Optional[camcops_server.cc_modules.cc_patient.Patient][source]

Fetch a patient by the server PK.

classmethod get_patients_by_idnum(dbsession: sqlalchemy.orm.session.Session, which_idnum: int, idnum_value: int, group_id: int = None, current_only: bool = True) → List[camcops_server.cc_modules.cc_patient.Patient][source]

Get all patients matching the specified ID number.

  • dbsession – a sqlalchemy.orm.session.Session

  • which_idnum – which ID number type?

  • idnum_value – actual value of the ID number

  • group_id – optional group ID to restrict to

  • current_only – restrict to _current patients?


list of all matching patients

get_sex() → str[source]

Return sex or “”.

get_sex_verbose(default: str = 'sex unknown') → str[source]

Returns HTML-safe version of sex, or default.

get_surname() → str[source]

Get surname (in upper case) or “”.

get_surname_forename_upper() → str[source]

Get “SURNAME, FORENAME”, using “(UNKNOWN)” for missing details.

get_tsv_page(req: CamcopsRequest)camcops_server.cc_modules.cc_tsv.TsvPage[source]

Get a camcops_server.cc_modules.cc_tsv.TsvPage for the patient.

get_xml_root(req: CamcopsRequest, options: camcops_server.cc_modules.cc_simpleobjects.TaskExportOptions = None)camcops_server.cc_modules.cc_xml.XmlElement[source]

Get root of XML tree, as an camcops_server.cc_modules.cc_xml.XmlElement.

property group

Returns the camcops_server.cc_modules.cc_group.Group to which this patient’s record belongs.

has_idnum_type(which_idnum: int) → bool[source]

Does the patient have an ID number of the specified type?

is_female() → bool[source]

Is sex ‘F’?

is_finalized() → bool[source]

Is the patient finalized (no longer available to be edited on the client device), and therefore editable on the server?

is_male() → bool[source]

Is sex ‘M’?

is_preserved() → bool[source]

Is the patient record preserved and erased from the tablet?

prettystr(req: CamcopsRequest) → str[source]

A prettified string version.


req – a camcops_server.cc_modules.cc_request.CamcopsRequest


SMITH, BOB (M, 1 Jan 1950, RiO# 123, NHS# 456)
satisfies_finalize_id_policy() → bool[source]

Does the patient satisfy the finalizing ID policy?

satisfies_id_policy(policy: TokenizedPolicy) → bool[source]

Does the patient satisfy a particular ID policy?

satisfies_upload_id_policy() → bool[source]

Does the patient satisfy the uploading ID policy?

set_idnum_value(req: CamcopsRequest, which_idnum: int, idnum_value: int) → None[source]

Sets an ID number value.

user_may_edit(req: CamcopsRequest) → bool[source]

Does the current user have permission to edit this patient?

camcops_server.cc_modules.cc_patient.is_candidate_patient_valid_for_group(ptinfo: camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo, group: Group, finalizing: bool) → Tuple[bool, str][source]

Is the specified patient acceptable to upload into this group?


  • group upload or finalize policy


is_candidate_patient_valid: check against predefined patients, if the group wants


valid, reason

Return type


camcops_server.cc_modules.cc_patient.is_candidate_patient_valid_for_restricted_user(req: CamcopsRequest, ptinfo: camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo) → Tuple[bool, str][source]

Is the specified patient OK to be uploaded by this user? Performs a check for restricted (single-patient) users; if true, ensures that the identifiers all match the expected patient.


valid, reason

Return type