Coverage for cc_modules/cc_all_models.py : 100%

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
3"""
4camcops_server/cc_modules/cc_all_models.py
6===============================================================================
8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com).
10 This file is part of CamCOPS.
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.
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.
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/>.
25===============================================================================
27**The point of this is to import everything that's an SQLAlchemy model, so
28they're registered (and also Task knows about all its subclasses).**
30"""
32import logging
33# from pprint import pformat
34from typing import Dict, List, Type
36from cardinal_pythonlib.logs import BraceStyleAdapter
37from cardinal_pythonlib.sqlalchemy.orm_inspect import gen_orm_classes_from_base
38from sqlalchemy.orm import configure_mappers
39from sqlalchemy.sql.schema import Table
41from camcops_server.cc_modules.cc_baseconstants import ALEMBIC_VERSION_TABLE
42from camcops_server.cc_modules.cc_db import GenericTabletRecordMixin
43from camcops_server.cc_modules.cc_sqlalchemy import Base
45# =============================================================================
46# Non-task model imports representing client-side tables
47# =============================================================================
48# How to suppress "Unused import statement"?
49# https://stackoverflow.com/questions/21139329/false-unused-import-statement-in-pycharm # noqa
50# http://codeoptimism.com/blog/pycharm-suppress-inspections-list/
52# noinspection PyUnresolvedReferences
53from camcops_server.cc_modules.cc_blob import Blob # noqa: F401
54# noinspection PyUnresolvedReferences
55from camcops_server.cc_modules.cc_patientidnum import PatientIdNum # noqa: F401
56# noinspection PyUnresolvedReferences
57from camcops_server.cc_modules.cc_patient import Patient # noqa: F401
59# =============================================================================
60# Other non-task model imports
61# =============================================================================
63from camcops_server.cc_modules.cc_audit import AuditEntry
64from camcops_server.cc_modules.cc_device import Device
65from camcops_server.cc_modules.cc_dirtytables import DirtyTable
66from camcops_server.cc_modules.cc_email import Email
67from camcops_server.cc_modules.cc_group import Group, group_group_table
68from camcops_server.cc_modules.cc_exportmodels import (
69 ExportedTaskEmail,
70 ExportedTask,
71 ExportedTaskFileGroup,
72 ExportedTaskHL7Message,
73)
74from camcops_server.cc_modules.cc_exportrecipient import ExportRecipient
75from camcops_server.cc_modules.cc_idnumdef import IdNumDefinition
76from camcops_server.cc_modules.cc_membership import UserGroupMembership
77from camcops_server.cc_modules.cc_session import CamcopsSession
78from camcops_server.cc_modules.cc_specialnote import SpecialNote
79from camcops_server.cc_modules.cc_serversettings import ServerSettings
80# noinspection PyUnresolvedReferences
81from camcops_server.cc_modules.cc_task import Task
82from camcops_server.cc_modules.cc_taskfilter import TaskFilter
83from camcops_server.cc_modules.cc_taskschedule import (
84 TaskSchedule,
85 TaskScheduleItem,
86)
87# noinspection PyUnresolvedReferences
88from camcops_server.cc_modules.cc_taskindex import (
89 PatientIdNumIndexEntry,
90 TaskIndexEntry,
91)
92from camcops_server.cc_modules.cc_user import (
93 SecurityAccountLockout,
94 SecurityLoginFailure,
95 User,
96)
98# =============================================================================
99# Task imports
100# =============================================================================
102# import_submodules("..tasks", __package__)
103#
104# ... NO LONGER SUFFICIENT as we are using SQLAlchemy relationship clauses that
105# are EVALUATED and so require the class names to be in the relevant namespace
106# at the time. So doing something equivalent to "import tasks.phq9" -- which is
107# what we get from 'import_submodules("..tasks", __package__)' -- isn't enough.
108# We need something equivalent to "from tasks.phq9 import Phq9".
110# noinspection PyUnresolvedReferences
111from camcops_server.tasks import * # see tasks/__init__.py # noqa: F401,F403
113# =============================================================================
114# Other report imports
115# =============================================================================
117# noinspection PyUnresolvedReferences
118from camcops_server.cc_modules.cc_taskreports import TaskCountReport # noqa: E501,F401
120# =============================================================================
121# Logging
122# =============================================================================
124log = BraceStyleAdapter(logging.getLogger(__name__))
126# log.critical("Loading cc_all_models")
128# =============================================================================
129# Ensure that anything with an AbstractConcreteBase gets its mappers
130# registered (i.e. Task).
131# =============================================================================
133configure_mappers()
135# =============================================================================
136# Tables (and fields) that clients can't touch
137# =============================================================================
139RESERVED_TABLE_NAMES = [
140 ALEMBIC_VERSION_TABLE,
141 AuditEntry.__tablename__,
142 CamcopsSession.__tablename__,
143 Device.__tablename__,
144 DirtyTable.__tablename__,
145 Email.__tablename__,
146 ExportedTask.__tablename__,
147 ExportedTaskEmail.__tablename__,
148 ExportedTaskFileGroup.__tablename__,
149 ExportedTaskHL7Message.__tablename__,
150 ExportRecipient.__tablename__,
151 Group.__tablename__,
152 group_group_table.name,
153 IdNumDefinition.__tablename__,
154 PatientIdNumIndexEntry.__tablename__,
155 SecurityAccountLockout.__tablename__,
156 SecurityLoginFailure.__tablename__,
157 ServerSettings.__tablename__,
158 SpecialNote.__tablename__,
159 TaskFilter.__tablename__,
160 TaskIndexEntry.__tablename__,
161 TaskSchedule.__tablename__,
162 TaskScheduleItem.__tablename__,
163 User.__tablename__,
164 UserGroupMembership.__tablename__,
165]
166RESERVED_FIELDS = GenericTabletRecordMixin.RESERVED_FIELDS
168# =============================================================================
169# Catalogue tables that clients use
170# =============================================================================
172CLIENT_TABLE_MAP = {} # type: Dict[str, Table]
173NONTASK_CLIENT_TABLENAMES = [] # type: List[str]
175# Add all tables that clients may upload to (including ancillary tables).
176for __orm_class in gen_orm_classes_from_base(Base): # type: Type[Base]
177 # noinspection PyUnresolvedReferences
178 if issubclass(__orm_class, GenericTabletRecordMixin):
179 __tablename = __orm_class.__tablename__
180 if __tablename not in RESERVED_TABLE_NAMES:
181 # Additional safety check: no client tables start with "_" and all
182 # server tables do:
183 if __tablename.startswith("_"):
184 pass
185 # noinspection PyUnresolvedReferences
186 __table = __orm_class.__table__ # type: Table
187 CLIENT_TABLE_MAP[__tablename] = __table
188 if not issubclass(__orm_class, Task):
189 NONTASK_CLIENT_TABLENAMES.append(__tablename)
190NONTASK_CLIENT_TABLENAMES.sort()
191# log.debug("NONTASK_CLIENT_TABLENAMES: {}", NONTASK_CLIENT_TABLENAMES)