Coverage for cc_modules/cc_client_api_helpers.py: 100%

1"""

2 camcops_server/cc_modules/cc_client_api_helpers.py

3

4===============================================================================

5

6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

8

9 This file is part of CamCOPS.

10

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.

15

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.

20

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

23

24===============================================================================

25

26**Additional helper functions used by the client (tablet device) API and the

27 webview for special features.**

28

29"""

30

31 from typing import Tuple

32

33 from sqlalchemy.sql.schema import Table

34

35 from camcops_server.cc_modules.cc_all_models import NONTASK_CLIENT_TABLENAMES

36 from camcops_server.cc_modules.cc_patient import Patient, PatientIdNum

37

38

39# =============================================================================

40# Table sort order

41# =============================================================================

42

43

44 def upload_commit_order_sorter(x: Table) -> Tuple[bool, bool, bool, str]:

45 """

46 Function to sort tables for the commit phase of the upload.

47

48 - "patient" must come before "patient_idnum", since ID number indexing

49 looks at the associated Patient.

50

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

58

59 - Task tables come last.

60

61 Args:

62 x: an SQLAlchemy :class:`Table`

63

64 Returns:

65 a tuple suitable for use as the key to a ``sort()`` or ``sorted()``

66 operation

67

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 )