Coverage for cc_modules/cc_all_models.py: 98%
48 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-15 14:23 +0100
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-15 14:23 +0100
1"""
2camcops_server/cc_modules/cc_all_models.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**The point of this is to import everything that's an SQLAlchemy model, so
27they're registered (and also Task knows about all its subclasses).**
29"""
31# =============================================================================
32# Imports
33# =============================================================================
35# -----------------------------------------------------------------------------
36# Simple
37# -----------------------------------------------------------------------------
39import logging
40from typing import Dict, List, Type
42from cardinal_pythonlib.logs import BraceStyleAdapter
43from cardinal_pythonlib.sqlalchemy.orm_inspect import gen_orm_classes_from_base
44from sqlalchemy.orm import configure_mappers
45from sqlalchemy.sql.schema import Table
47from camcops_server.cc_modules.cc_baseconstants import ALEMBIC_VERSION_TABLE
48from camcops_server.cc_modules.cc_db import GenericTabletRecordMixin
49from camcops_server.cc_modules.cc_sqlalchemy import Base
51# -----------------------------------------------------------------------------
52# Non-task model imports representing client-side tables
53# -----------------------------------------------------------------------------
54# How to suppress "Unused import statement"?
55# https://stackoverflow.com/questions/21139329/false-unused-import-statement-in-pycharm # noqa
56# http://codeoptimism.com/blog/pycharm-suppress-inspections-list/
58# noinspection PyUnresolvedReferences
59from camcops_server.cc_modules.cc_blob import Blob # noqa: F401
61# noinspection PyUnresolvedReferences
62from camcops_server.cc_modules.cc_patientidnum import ( # noqa: F401
63 PatientIdNum,
64)
66# noinspection PyUnresolvedReferences
67from camcops_server.cc_modules.cc_patient import Patient # noqa: F401
69# -----------------------------------------------------------------------------
70# Other non-task model imports
71# -----------------------------------------------------------------------------
73from camcops_server.cc_modules.cc_audit import AuditEntry
74from camcops_server.cc_modules.cc_device import Device
75from camcops_server.cc_modules.cc_dirtytables import DirtyTable
76from camcops_server.cc_modules.cc_email import Email
77from camcops_server.cc_modules.cc_group import Group, group_group_table
78from camcops_server.cc_modules.cc_exportmodels import (
79 ExportedTaskEmail,
80 ExportedTask,
81 ExportedTaskFileGroup,
82 ExportedTaskHL7Message,
83)
84from camcops_server.cc_modules.cc_exportrecipient import ExportRecipient
85from camcops_server.cc_modules.cc_idnumdef import IdNumDefinition
86from camcops_server.cc_modules.cc_membership import UserGroupMembership
87from camcops_server.cc_modules.cc_session import CamcopsSession
88from camcops_server.cc_modules.cc_specialnote import SpecialNote
89from camcops_server.cc_modules.cc_serversettings import ServerSettings
91# noinspection PyUnresolvedReferences
92from camcops_server.cc_modules.cc_task import Task
93from camcops_server.cc_modules.cc_taskfilter import TaskFilter
94from camcops_server.cc_modules.cc_taskschedule import (
95 TaskSchedule,
96 TaskScheduleItem,
97)
99# noinspection PyUnresolvedReferences
100from camcops_server.cc_modules.cc_taskindex import (
101 PatientIdNumIndexEntry,
102 TaskIndexEntry,
103)
104from camcops_server.cc_modules.cc_user import (
105 SecurityAccountLockout,
106 SecurityLoginFailure,
107 User,
108)
110# -----------------------------------------------------------------------------
111# Task imports
112# -----------------------------------------------------------------------------
114# import_submodules("..tasks", __package__)
115#
116# ... NO LONGER SUFFICIENT as we are using SQLAlchemy relationship clauses that
117# are EVALUATED and so require the class names to be in the relevant namespace
118# at the time. So doing something equivalent to "import tasks.phq9" -- which is
119# what we get from 'import_submodules("..tasks", __package__)' -- isn't enough.
120# We need something equivalent to "from tasks.phq9 import Phq9".
122# noinspection PyUnresolvedReferences
123from camcops_server.tasks import * # see tasks/__init__.py # noqa: F401, F403
125# -----------------------------------------------------------------------------
126# Other report imports
127# -----------------------------------------------------------------------------
129# noinspection PyUnresolvedReferences
130from camcops_server.cc_modules.cc_taskreports import ( # noqa: F401
131 TaskCountReport,
132)
134# noinspection PyUnresolvedReferences
135from camcops_server.cc_modules.cc_taskschedulereports import ( # noqa: F401
136 TaskAssignmentReport,
137)
140# =============================================================================
141# Logging
142# =============================================================================
144log = BraceStyleAdapter(logging.getLogger(__name__))
146# log.debug("Loading cc_all_models")
149# =============================================================================
150# Ensure that anything with an AbstractConcreteBase gets its mappers
151# registered (i.e. Task).
152# =============================================================================
154configure_mappers()
157# =============================================================================
158# Tables (and fields) that clients can't touch
159# =============================================================================
161RESERVED_TABLE_NAMES = [
162 ALEMBIC_VERSION_TABLE,
163 AuditEntry.__tablename__,
164 CamcopsSession.__tablename__,
165 Device.__tablename__,
166 DirtyTable.__tablename__,
167 Email.__tablename__,
168 ExportedTask.__tablename__,
169 ExportedTaskEmail.__tablename__,
170 ExportedTaskFileGroup.__tablename__,
171 ExportedTaskHL7Message.__tablename__,
172 ExportRecipient.__tablename__,
173 Group.__tablename__,
174 group_group_table.name,
175 IdNumDefinition.__tablename__,
176 PatientIdNumIndexEntry.__tablename__,
177 SecurityAccountLockout.__tablename__,
178 SecurityLoginFailure.__tablename__,
179 ServerSettings.__tablename__,
180 SpecialNote.__tablename__,
181 TaskFilter.__tablename__,
182 TaskIndexEntry.__tablename__,
183 TaskSchedule.__tablename__,
184 TaskScheduleItem.__tablename__,
185 User.__tablename__,
186 UserGroupMembership.__tablename__,
187]
190# =============================================================================
191# Catalogue tables that clients use
192# =============================================================================
194CLIENT_TABLE_MAP = {} # type: Dict[str, Table]
195NONTASK_CLIENT_TABLENAMES = [] # type: List[str]
197# Add all tables that clients may upload to (including ancillary tables).
198for __orm_class in gen_orm_classes_from_base(Base): # type: Type[Base]
199 # noinspection PyUnresolvedReferences
200 if issubclass(__orm_class, GenericTabletRecordMixin):
201 __tablename = __orm_class.__tablename__
202 if __tablename not in RESERVED_TABLE_NAMES:
203 # Additional safety check: no client tables start with "_" and all
204 # server tables do:
205 if __tablename.startswith("_"):
206 pass
207 # noinspection PyUnresolvedReferences
208 __table = __orm_class.__table__ # type: ignore[assignment]
209 CLIENT_TABLE_MAP[__tablename] = __table # type: ignore[assignment]
210 if not issubclass(__orm_class, Task):
211 NONTASK_CLIENT_TABLENAMES.append(__tablename)
212NONTASK_CLIENT_TABLENAMES.sort()
213# log.debug("NONTASK_CLIENT_TABLENAMES: {}", NONTASK_CLIENT_TABLENAMES)