Coverage for cc_modules/cc_client_api_helpers.py: 100%
6 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-15 15:51 +0100
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-15 15:51 +0100
1"""
2camcops_server/cc_modules/cc_client_api_helpers.py
4===============================================================================
6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CamCOPS.
11 CamCOPS is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 CamCOPS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
24===============================================================================
26**Additional helper functions used by the client (tablet device) API and the
27webview for special features.**
29"""
31from typing import Tuple
33from sqlalchemy.sql.schema import Table
35from camcops_server.cc_modules.cc_all_models import NONTASK_CLIENT_TABLENAMES
36from camcops_server.cc_modules.cc_patient import Patient, PatientIdNum
39# =============================================================================
40# Table sort order
41# =============================================================================
44def upload_commit_order_sorter(x: Table) -> Tuple[bool, bool, bool, str]:
45 """
46 Function to sort tables for the commit phase of the upload.
48 - "patient" must come before "patient_idnum", since ID number indexing
49 looks at the associated Patient.
51 - All of "patient", "blobs", and all ancillary tables must come before task
52 tables, because task indexes depend on tasks correctly retrieving their
53 subsidiary information to determine their
54 :meth:`camcops_server.cc_modules.cc_task.Task.is_complete` status.
55 (However, even though ancillary tables can refer to BLOBs, as long as
56 both are complete before the task tables are examined, their relative
57 order doesn't matter.)
59 - Task tables come last.
61 Args:
62 x: an SQLAlchemy :class:`Table`
64 Returns:
65 a tuple suitable for use as the key to a ``sort()`` or ``sorted()``
66 operation
68 Note that ``False`` sorts before ``True``, and see
69 https://stackoverflow.com/questions/23090664/sorting-a-list-of-string-in-python-such-that-a-specific-string-if-present-appea.
70 """
71 return (
72 x.name != Patient.__tablename__,
73 x.name != PatientIdNum.__tablename__,
74 x.name not in NONTASK_CLIENT_TABLENAMES,
75 x.name,
76 )