14.3.107. crate_anon.crateweb.consent.utils
crate_anon/crateweb/consent/utils.py
Copyright (C) 2015, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
This file is part of CRATE.
CRATE 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.
CRATE 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 CRATE. If not, see <https://www.gnu.org/licenses/>.
Utility functions for the consent-to-contact system.
- crate_anon.crateweb.consent.utils.days_to_years(days: int, dp: int = 1) str [source]
Converts days to years, in string form.
- Parameters:
days – number of days
dp – number of decimal places
- Returns:
number of years
- Return type:
str
For “consent after discharge”, primarily.
Assumes 365 days/year, not 365.24.
- crate_anon.crateweb.consent.utils.email_css() str [source]
Returns CSS for use in e-mails to clinicians.
- crate_anon.crateweb.consent.utils.get_domain_from_email(email: str) str [source]
Extracts the domain part from an e-mail address.
- Parameters:
email – the e-mail address, e.g. “someone@cam.ac.uk”
- Returns:
the domain part, e.g. “cam.ac.uk”
Very simple algorithm…
- crate_anon.crateweb.consent.utils.latest_date(*args) date | None [source]
Returns the latest of a bunch of dates, or
None
if there are no dates specified at all.
- crate_anon.crateweb.consent.utils.make_cpft_email_address(forename: str, surname: str, default: str = '') str [source]
Make a CPFT e-mail address. Not guaranteed to work.
- Parameters:
forename – forename
surname – surname
default – value to return if something looks wrong
- Returns:
forename.surname@cpft.nhs.uk
, ordefault
- Return type:
e-mail address
- crate_anon.crateweb.consent.utils.make_forename_surname_email_address(forename: str, surname: str, domain: str, default: str = '') str [source]
Converts a forename and surname into an e-mail address of the form
forename.surname@domain
. Not guaranteed to work.- Parameters:
forename – forename
surname – surname
domain – domain, e.g. “cpft.nhs.uk”
default – value to return if something looks wrong
- Returns:
e-mail address (or
default
)
- crate_anon.crateweb.consent.utils.pdf_css(patient: bool = True) str [source]
Returns CSS for use in PDF letters etc.
- Parameters:
patient – patient settings (e.g. “large print”), rather than researcher settings (“cram it in”)?
- crate_anon.crateweb.consent.utils.read_static_file_contents(filename: str) str [source]
Returns the text contents of a static file.
- Parameters:
filename – filename (within the local static directory as determined by
settings.LOCAL_STATIC_DIR
- crate_anon.crateweb.consent.utils.render_email_html_to_string(template: str, context: Dict[str, Any] | None = None) str [source]
Renders a template into HTML that can be used for making PDFs.
- Parameters:
template – filename of the Django template
context – template context dictionary (which will be augmented with email-specific content)
- Returns:
HTML
- crate_anon.crateweb.consent.utils.render_pdf_html_to_string(template: str, context: Dict[str, Any] | None = None, patient: bool = True) str [source]
Renders a template into HTML that can be used for making PDFs.
- Parameters:
template – filename of the Django template
context – template context dictionary (which will be augmented with PDF-specific content)
patient – patient CSS settings (e.g. “large print”), rather than researcher CSS settings (“cram it in”)?
- Returns:
HTML
- crate_anon.crateweb.consent.utils.to_date(d: date | datetime | None) date | None [source]
Converts any of various date-like things to
datetime.date
objects.
- crate_anon.crateweb.consent.utils.validate_researcher_email_domain(email: str) None [source]
Ensures that an e-mail address is acceptable as a researcher e-mail address. We may be sending patient-identifiable information (with consent) via this method, so we want to be sure that nobody’s put dodgy researcher e-mails in our system.
We validate the e-mail domain against
settings.VALID_RESEARCHER_EMAIL_DOMAINS
, if set.- Parameters:
email – an e-mail address
- Raises:
django.core.exceptions.ValidationError –