Skip to content

Commit

Permalink
updates tests and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
cutoffthetop committed Feb 12, 2025
1 parent 5087f24 commit f96271c
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ dmypy.json

# Default exports
*.ndjson
.states/
data/
identity.csv
schema.json
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- add color mode toggle to login screen
- added new/remove buttons for additive rules for string fields
- add event handlers for adding/removing additive value input cards
- added text input field for string fields
- added text input field for string fields (and reference fields, temporarily)
- add event handler for setting string input values

### Changes
Expand Down
12 changes: 7 additions & 5 deletions mex/editor/edit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@

def editor_value_switch(
field_name: str,
primary_source: str | None,
primary_source: EditorPrimarySource,
value: EditorValue,
index: int,
) -> rx.Component:
"""Return a switch for toggling subtractive rules."""
return rx.switch(
checked=value.enabled,
on_change=cast(EditState, EditState).toggle_field_value(field_name, value),
custom_attrs={"data-testid": f"switch-{field_name}-{primary_source}-{index}"},
color_scheme="jade",
custom_attrs={
"data-testid": f"switch-{field_name}-{primary_source.name.text}-{index}"
},
color_scheme=rx.cond(primary_source.enabled, "jade", "gray"),
)


def editor_static_value(
field_name: str,
primary_source: str | None,
primary_source: EditorPrimarySource,
index: int,
value: EditorValue,
) -> rx.Component:
Expand Down Expand Up @@ -112,7 +114,7 @@ def editor_value_card_body(
),
editor_static_value(
field_name,
primary_source.name.text,
primary_source,
index,
value,
),
Expand Down
9 changes: 6 additions & 3 deletions mex/editor/edit/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Annotated

import reflex as rx
from pydantic import Field

from mex.common.types import MergedPrimarySourceIdentifier
from mex.editor.models import EditorValue
Expand All @@ -7,17 +10,17 @@
class InputConfig(rx.Base):
"""Model for configuring input masks."""

data_type: str
data_type: Annotated[str, Field(frozen=True)]


class EditorPrimarySource(rx.Base):
"""Model for describing the editor state for one primary source."""

name: EditorValue
identifier: MergedPrimarySourceIdentifier
input_config: InputConfig | None
editor_values: list[EditorValue] = []
enabled: bool = True
input_config: InputConfig | None = None
enabled: bool


class EditorField(rx.Base):
Expand Down
41 changes: 24 additions & 17 deletions mex/editor/edit/transform.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from functools import cache

from mex.common.fields import (
EMAIL_FIELDS_BY_CLASS_NAME,
LINK_FIELDS_BY_CLASS_NAME,
Expand All @@ -8,6 +10,7 @@
VOCABULARY_FIELDS_BY_CLASS_NAME,
)
from mex.common.models import (
ADDITIVE_MODEL_CLASSES_BY_NAME,
MEX_PRIMARY_SOURCE_STABLE_TARGET_ID,
RULE_SET_REQUEST_CLASSES_BY_NAME,
AnyAdditiveModel,
Expand All @@ -22,10 +25,6 @@
from mex.common.types import (
TEMPORAL_ENTITY_CLASSES_BY_PRECISION,
VOCABULARY_ENUMS_BY_NAME,
AnyNestedModel,
AnyPrimitiveType,
AnyTemporalEntity,
AnyVocabularyEnum,
Link,
MergedPrimarySourceIdentifier,
TemporalEntityPrecision,
Expand All @@ -34,6 +33,7 @@
from mex.editor.edit.models import EditorField, EditorPrimarySource, InputConfig
from mex.editor.models import EditorValue
from mex.editor.transform import ensure_list, transform_value
from mex.editor.types import AnyModelValue


def _get_primary_source_id_from_model(
Expand Down Expand Up @@ -72,14 +72,16 @@ def _transform_model_values_to_editor_values(
return editor_values


@cache
def _transform_model_to_additive_input_config(
field_name: str,
model: AnyExtractedModel | AnyMergedModel | AnyAdditiveModel,
entity_type: str,
) -> InputConfig | None:
"""Determine the input type for a given field of a given model."""
if isinstance(model, AnyAdditiveModel) and (
field_name in STRING_FIELDS_BY_CLASS_NAME[model.entityType]
or field_name in EMAIL_FIELDS_BY_CLASS_NAME[model.entityType]
if (entity_type in ADDITIVE_MODEL_CLASSES_BY_NAME) and field_name in (
STRING_FIELDS_BY_CLASS_NAME[entity_type]
+ EMAIL_FIELDS_BY_CLASS_NAME[entity_type]
+ TEMPORAL_FIELDS_BY_CLASS_NAME[entity_type]
):
return InputConfig(data_type="string")
return None
Expand Down Expand Up @@ -129,7 +131,7 @@ def _transform_model_to_editor_primary_sources(
if not (
input_config := _transform_model_to_additive_input_config(
field_name,
model,
model.entityType,
)
):
continue
Expand Down Expand Up @@ -193,21 +195,26 @@ def transform_models_to_fields(
def _transform_fields_to_additive(
fields: list[EditorField],
stem_type: str,
) -> dict[str, list[str]]:
) -> dict[str, list[AnyModelValue]]:
"""Transform a list of editor fields back to a raw additive rule."""
raw_rule: dict[str, list[str]] = {}
raw_rule: dict[str, list[AnyModelValue]] = {}
additive_class_name = ensure_prefix(stem_type, "Additive")
field_names = MERGEABLE_FIELDS_BY_CLASS_NAME[additive_class_name]
for field in fields:
if field.name not in field_names:
continue
raw_rule[field.name] = field_values = []
for primary_source in field.primary_sources:
if primary_source.input_config:
for value in primary_source.editor_values:
if value.text:
# TODO(ND): transform other types too
field_values.append(value.text) # noqa: PERF401
if not primary_source.input_config:
continue
for editor_value in primary_source.editor_values:
additive_value = _transform_editor_value_to_model_value(
editor_value,
field.name,
additive_class_name,
)
if additive_value not in field_values:
field_values.append(additive_value)
return raw_rule


Expand Down Expand Up @@ -235,7 +242,7 @@ def _transform_editor_value_to_model_value(
value: EditorValue,
field_name: str,
class_name: str,
) -> AnyNestedModel | AnyPrimitiveType | AnyTemporalEntity | AnyVocabularyEnum:
) -> AnyModelValue:
"""Transform an editor value back to a value to be used in mex.common.models."""
if field_name in LINK_FIELDS_BY_CLASS_NAME[class_name]:
return Link(url=value.href, language=value.badge, title=value.text)
Expand Down
10 changes: 10 additions & 0 deletions mex/editor/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
from pydantic import SecretStr

from mex.common.models import BaseModel
from mex.common.types import (
AnyNestedModel,
AnyPrimitiveType,
AnyTemporalEntity,
AnyVocabularyEnum,
)

AnyModelValue = (
AnyNestedModel | AnyPrimitiveType | AnyTemporalEntity | AnyVocabularyEnum
)


class EditorUserPassword(SecretStr):
Expand Down
Loading

0 comments on commit f96271c

Please sign in to comment.