14.3.127. crate_anon.crateweb.research.archive_backend
crate_anon/crateweb/research/archive_backend.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/>.
Support functions for the archive system.
- class crate_anon.crateweb.research.archive_backend.ArchiveContextKeys[source]
Names of objects that become part of the context in which archive templates operate. Some are also used as URL parameter keys.
The case here is to avoid confusion not to indicate “constness” within this class.
- crate_anon.crateweb.research.archive_backend.add_file_timestamp_to_url_query(filepath: str, qparams: Dict[str, Any]) None [source]
Adds a file’s timestamp to the query parameters that will make up a URL.
Why? So that if the file is edited, a new URL is generated, and caching browsers will automatically refresh.
See
https://stackoverflow.com/questions/9692665/cache-busting-via-params
https://docs.python.org/3/library/os.path.html#os.path.getmtime
- Parameters:
filepath – full path to file
qparams – parameter dictionary, which will be modified
- crate_anon.crateweb.research.archive_backend.archive_attachment_url(filename: str, patient_id: str = '', content_type: str = '', offered_filename: str = '', guess_content_type: bool | None = None) str [source]
Returns a URL to download an archive attachment (e.g. a PDF).
- Parameters:
filename – filename on disk, within the archive’s attachment directory
patient_id – patient ID (used for auditing)
content_type – HTTP content type; see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
offered_filename – filename offered to user
guess_content_type – if no content_type is specified, should we guess? Pass
None
for the default,DEFAULT_GUESS_CONTENT_TYPE
.
- crate_anon.crateweb.research.archive_backend.archive_misconfigured_response() HttpResponse [source]
Returns an error
HttpResponse
describing how the archive is misconfigured.
- crate_anon.crateweb.research.archive_backend.archive_root_url() str [source]
Returns a URL to the root of the archive, typically including the “launch for patient” view.
- crate_anon.crateweb.research.archive_backend.archive_static_url(filename: str) str [source]
Returns a URL to download a static file from the archive.
- Parameters:
filename – filename on disk, within the archive’s static directory
- crate_anon.crateweb.research.archive_backend.archive_template_url(template_name: str = '', patient_id: str = '', **kwargs) str [source]
Creates a URL to inspect part of the archive.
- Parameters:
template_name – short name of the (configurable) template
patient_id – patient ID
**kwargs – other optional arguments, passed as URL parameters
- Returns:
A URL.
- crate_anon.crateweb.research.archive_backend.audit_archive_attachment(request: HttpRequest, patient_id: str, filename: str) None [source]
Audits access to an attachment via a patient’s archive view.
- Parameters:
request – Django request
patient_id – patient ID
filename – filename of attachment within archive
- crate_anon.crateweb.research.archive_backend.audit_archive_template(request: HttpRequest, patient_id: str, query_string: str) None [source]
Audits access to a template for a patient.
- Parameters:
request – Django request
patient_id – patient ID
query_string – URL query string, which will include details of the template and any other arguments.
- crate_anon.crateweb.research.archive_backend.get_archive_attachment_filepath(filename: str) str [source]
Returns the full path of an archive attachment.
- Parameters:
filename – name of the attachment
- crate_anon.crateweb.research.archive_backend.get_archive_static_filepath(filename: str) str [source]
Returns the full path of an archive static file.
- Parameters:
filename – name of the static file
- crate_anon.crateweb.research.archive_backend.get_archive_template_filepath(template_name: str) str [source]
Returns the full path of a template, or “” if none is found.
- Parameters:
template_name – name of the template
- crate_anon.crateweb.research.archive_backend.safe_path(directory: str, filename: str) str [source]
Ensures that a filename is safe and within a directory – for example, that nobody passes a filename like
../../../etc/passwd
to break out of our directory.- Parameters:
directory – directory, within which filename must be
filename – filename
- Returns:
the filename if it’s safe and exists
- Return type:
str