Coverage for cc_modules/cc_client_api_helpers.py: 86%

7 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-08 23:14 +0000

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/cc_modules/cc_client_api_helpers.py 

5 

6=============================================================================== 

7 

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

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

10 

11 This file is part of CamCOPS. 

12 

13 CamCOPS is free software: you can redistribute it and/or modify 

14 it under the terms of the GNU General Public License as published by 

15 the Free Software Foundation, either version 3 of the License, or 

16 (at your option) any later version. 

17 

18 CamCOPS is distributed in the hope that it will be useful, 

19 but WITHOUT ANY WARRANTY; without even the implied warranty of 

20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

21 GNU General Public License for more details. 

22 

23 You should have received a copy of the GNU General Public License 

24 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>. 

25 

26=============================================================================== 

27 

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

29webview for special features.** 

30 

31""" 

32 

33from typing import Tuple 

34 

35from sqlalchemy.sql.schema import Table 

36 

37from camcops_server.cc_modules.cc_all_models import NONTASK_CLIENT_TABLENAMES 

38from camcops_server.cc_modules.cc_patient import Patient, PatientIdNum 

39 

40 

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

42# Table sort order 

43# ============================================================================= 

44 

45 

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

47 """ 

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

49 

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

51 looks at the associated Patient. 

52 

53 - All of "patient", "blobs", and all ancillary tables must come before task 

54 tables, because task indexes depend on tasks correctly retrieving their 

55 subsidiary information to determine their 

56 :meth:`camcops_server.cc_modules.cc_task.Task.is_complete` status. 

57 (However, even though ancillary tables can refer to BLOBs, as long as 

58 both are complete before the task tables are examined, their relative 

59 order doesn't matter.) 

60 

61 - Task tables come last. 

62 

63 Args: 

64 x: an SQLAlchemy :class:`Table` 

65 

66 Returns: 

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

68 operation 

69 

70 Note that ``False`` sorts before ``True``, and see 

71 https://stackoverflow.com/questions/23090664/sorting-a-list-of-string-in-python-such-that-a-specific-string-if-present-appea. 

72 """ # noqa 

73 return ( 

74 x.name != Patient.__tablename__, 

75 x.name != PatientIdNum.__tablename__, 

76 x.name not in NONTASK_CLIENT_TABLENAMES, 

77 x.name, 

78 )