Skip to content

Commit

Permalink
Add a non-mapped equivalent to camcops_column
Browse files Browse the repository at this point in the history
It looks like we can't use MappedColumn in places where we are setting the type in the
arguments, so we need a non-mapped version as well.
  • Loading branch information
martinburchell committed Feb 26, 2025
1 parent ad93c07 commit 8b5b93c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
65 changes: 62 additions & 3 deletions server/camcops_server/cc_modules/cc_sqla_coltypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1446,8 +1446,67 @@ def camcops_column(
blob_relationship_attr_name: str = "",
permitted_value_checker: PermittedValueChecker = None,
**kwargs,
) -> Column[Any]:
"""
Args:
*args:
Arguments to the :class:`Column` constructor.
include_in_anon_staging_db:
Ensure this is marked for inclusion in data dictionaries for an
anonymisation staging database.
exempt_from_anonymisation:
If true: though this field might be text, it is guaranteed not
to contain identifiers (e.g. it might contain only predefined
disease severity descriptions) and does not require
anonymisation.
identifies_patient:
If true: contains a patient identifier (e.g. name).
is_blob_id_field:
If true: this field contains a reference (client FK) to the
BLOB table.
blob_relationship_attr_name:
For BLOB ID fields: the name of the associated relationship
attribute (which, when accessed, yields the BLOB itself) in
the owning class/object.
permitted_value_checker:
If specified, a :class:`PermittedValueChecker` that allows
soft constraints to be specified on the field's contents. (That
is, no constraints are specified at the database level, but we
can moan if incorrect data are present.)
**kwargs:
Arguments to the :class:`Column` constructor.
"""
if is_blob_id_field:
assert blob_relationship_attr_name, (
"If specifying a BLOB ID field, must give the attribute name "
"of the relationship too"
)
info = dict(
is_camcops_column=True,
include_in_anon_staging_db=include_in_anon_staging_db,
exempt_from_anonymisation=exempt_from_anonymisation,
identifies_patient=identifies_patient,
is_blob_id_field=is_blob_id_field,
blob_relationship_attr_name=blob_relationship_attr_name,
permitted_value_checker=permitted_value_checker,
)
return Column(*args, info=info, **kwargs)


def mapped_camcops_column(
*args,
include_in_anon_staging_db: bool = False,
exempt_from_anonymisation: bool = False,
identifies_patient: bool = False,
is_blob_id_field: bool = False,
blob_relationship_attr_name: str = "",
permitted_value_checker: PermittedValueChecker = None,
**kwargs,
) -> MappedColumn[Any]:
"""
As :func:`camcops_server.cc_modules.cc_sqla_coltypes.camcops_column` but
returns a python typing-compatible MappedColumn.
Args:
*args:
Arguments to the :class:`Column` constructor.
Expand Down Expand Up @@ -1520,7 +1579,7 @@ def gen_columns_matching_attrnames(

def gen_camcops_columns(
obj,
) -> Generator[Tuple[str, MappedColumn], None, None]:
) -> Generator[Tuple[str, Column], None, None]:
"""
Finds all columns of an object that are
:func:`camcops_server.cc_modules.cc_sqla_coltypes.camcops_column` columns.
Expand All @@ -1538,7 +1597,7 @@ def gen_camcops_columns(

def gen_camcops_blob_columns(
obj,
) -> Generator[Tuple[str, MappedColumn], None, None]:
) -> Generator[Tuple[str, Column], None, None]:
"""
Finds all columns of an object that are
:func:`camcops_server.cc_modules.cc_sqla_coltypes.camcops_column` columns
Expand Down Expand Up @@ -1671,7 +1730,7 @@ def gen_blob_relationships(
# =============================================================================


def bool_column(name: str, *args, **kwargs) -> MappedColumn[bool]:
def bool_column(name: str, *args, **kwargs) -> Column[bool]:
constraint_name = kwargs.pop(
"constraint_name", None
) # type: Optional[str]
Expand Down
4 changes: 2 additions & 2 deletions server/camcops_server/tasks/diagnosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
)
from camcops_server.cc_modules.cc_sqlalchemy import Base
from camcops_server.cc_modules.cc_sqla_coltypes import (
camcops_column,
DiagnosticCodeColType,
mapped_camcops_column,
)
from camcops_server.cc_modules.cc_validators import (
validate_restricted_sql_search_literal,
Expand Down Expand Up @@ -134,7 +134,7 @@ class DiagnosisItemBase(GenericTabletRecordMixin, Base):
)

# noinspection PyMethodParameters
description: Mapped[Optional[str]] = camcops_column(
description: Mapped[Optional[str]] = mapped_camcops_column(
"description",
UnicodeText,
exempt_from_anonymisation=True,
Expand Down
4 changes: 2 additions & 2 deletions server/camcops_server/tasks/service_satisfaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from camcops_server.cc_modules.cc_html import tr_qa
from camcops_server.cc_modules.cc_request import CamcopsRequest
from camcops_server.cc_modules.cc_sqla_coltypes import (
camcops_column,
mapped_camcops_column,
ZERO_TO_FOUR_CHECKER,
)
from camcops_server.cc_modules.cc_string import AS
Expand All @@ -57,7 +57,7 @@ class AbstractSatisfaction(object):
)

# noinspection PyMethodParameters
rating: Mapped[Optional[int]] = camcops_column(
rating: Mapped[Optional[int]] = mapped_camcops_column(
"rating",
permitted_value_checker=ZERO_TO_FOUR_CHECKER,
comment="Rating (0 very poor - 4 excellent)",
Expand Down

0 comments on commit 8b5b93c

Please sign in to comment.