15.2.118. camcops_server.cc_modules.cc_patient


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/>.


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.

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.datetime, datetime.date, pendulum.datetime.DateTime, str], default: str = '') Union[int, str][source]

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

get_bare_ptinfo() camcops_server.cc_modules.cc_simpleobjects.BarePatientInfo[source]

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_fhir_bundle_entry(req: CamcopsRequest, recipient: ExportRecipient) Dict[str, Any][source]

Returns a dictionary, suitable for serializing to JSON, that encapsulates patient identity information in a FHIR bundle.

See https://www.hl7.org/fhir/patient.html.

get_fhir_identifier(req: CamcopsRequest, recipient: ExportRecipient) fhirclient.models.identifier.Identifier[source]

Returns a FHIR identifier for this patient, as a fhirclient.models.identifier.Identifier object.

This pairs a URL to our CamCOPS server indicating the ID number type (as the “system”) with the actual ID number (as the “value”).

For debugging situations, it falls back to a default identifier (using the PK on our CamCOPS server).

get_fhir_subject_ref(req: CamcopsRequest, recipient: ExportRecipient) Dict[source]

Returns a FHIRReference (in JSON dict format) used to refer to this patient as a “subject” of some other entry (like a questionnaire).

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: Optional[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: Optional[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_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?

user_may_view(user: User) bool[source]

May this user inspect patient details directly?

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