Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/cc_modules/cc_client_api_helpers.py 

5 

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

7 

8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com). 

9 

10 This file is part of CamCOPS. 

11 

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

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

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

15 (at your option) any later version. 

16 

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

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

19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

20 GNU General Public License for more details. 

21 

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

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

24 

25=============================================================================== 

26 

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

28webview for special features.** 

29 

30""" 

31 

32from typing import Tuple 

33 

34from sqlalchemy.sql.schema import Table 

35 

36from camcops_server.cc_modules.cc_all_models import NONTASK_CLIENT_TABLENAMES 

37from camcops_server.cc_modules.cc_patient import Patient, PatientIdNum 

38 

39 

40# ============================================================================= 

41# Table sort order 

42# ============================================================================= 

43 

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. 

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 """ # noqa 

71 return (x.name != Patient.__tablename__, 

72 x.name != PatientIdNum.__tablename__, 

73 x.name not in NONTASK_CLIENT_TABLENAMES, 

74 x.name)