Skip to content

Commit

Permalink
new core
Browse files Browse the repository at this point in the history
  • Loading branch information
mochic committed Jul 16, 2024
1 parent 680ff25 commit ac0cd43
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
23 changes: 12 additions & 11 deletions src/aind_slims_api/behavior_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from pydantic import Field

from aind_slims_api.core import SlimsBaseModel, SlimsClient, SLIMSTABLES
from aind_slims_api.mouse import SlimsMouseContent
from aind_slims_api.instrument import SlimsInstrument
from aind_slims_api.user import SlimsUser

logger = logging.getLogger()

Expand All @@ -31,7 +34,8 @@ class SlimsBehaviorSessionContentEvent(SlimsBaseModel):
task_schema_version: str | None = Field(
default=None, alias="cnvn_cf_taskSchemaVersion"
)
software_version: str | None = Field(default=None, alias="cnvn_cf_softwareVersion")
software_version: str | None = Field(
default=None, alias="cnvn_cf_softwareVersion")
date: datetime | None = Field(..., alias="cnvn_cf_scheduledDate")
cnvn_fk_contentEventType: int = 10 # pk of Behavior Session ContentEvent
_slims_table: SLIMSTABLES = "ContentEvent"
Expand Down Expand Up @@ -66,17 +70,14 @@ def _resolve_pk(

def fetch_behavior_session_content_events(
client: SlimsClient,
mouse: SlimsSingletonFetchReturn,
) -> tuple[list[SlimsBehaviorSessionContentEvent], list[dict[str, Any]]]:
mouse: SlimsMouseContent,
) -> list[SlimsBehaviorSessionContentEvent]:
"""Fetches behavior sessions for a mouse with labtracks id {mouse_name}
Returns
-------
tuple:
list:
Validated SlimsBehaviorSessionContentEvent objects
list:
Dictionaries representations of objects that failed validation
list:
Validated SlimsBehaviorSessionContentEvent objects
"""
return client.fetch_models(
SlimsBehaviorSessionContentEvent,
Expand All @@ -88,9 +89,9 @@ def fetch_behavior_session_content_events(

def write_behavior_session_content_events(
client: SlimsClient,
mouse: SlimsSingletonFetchReturn,
instrument: SlimsSingletonFetchReturn,
trainers: list[SlimsSingletonFetchReturn],
mouse: SlimsMouseContent,
instrument: SlimsInstrument,
trainers: list[SlimsUser],
*behavior_sessions: SlimsBehaviorSessionContentEvent,
) -> list[SlimsBehaviorSessionContentEvent]:
"""Writes behavior sessions for a mouse with labtracks id {mouse_name}
Expand Down
19 changes: 14 additions & 5 deletions src/aind_slims_api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
field_serializer,
field_validator,
)
from copy import deepcopy
from pydantic.fields import FieldInfo
import logging
from typing import (
Any, Generator, Callable, Literal, Optional, Sequence, Type, TypeVar
Any, Generator, Callable, Literal, Optional, Sequence, Type, TypeVar,
)
from requests import Response
from slims.slims import Slims, _SlimsApiException
Expand Down Expand Up @@ -97,6 +98,7 @@ class MyModel(SlimsBaseModel):
json_entity: dict = None
attachments: Optional[Callable[[], Sequence[SlimsAttachment]]] = None
_slims_table: SLIMSTABLES
_base_fetch_filters: dict[str, str | int] # use for fetch_models, fetch_model

@field_validator("*", mode="before")
def _validate(cls, value, info: ValidationInfo):
Expand Down Expand Up @@ -310,10 +312,17 @@ def fetch_models(
-----
- kwargs are mapped to field alias values
"""
resolved_kwargs = {
self.resolve_model_alias(model, name): value
for name, value in kwargs.items()
}
resolved_kwargs = deepcopy(model._base_fetch_filters)
for name, value in kwargs.items():
resolved_kwargs[self.resolve_model_alias(model, name)] = value
# resolved_kwargs = {
# self.resolve_model_alias(model, name): value
# for name, value in kwargs.items()
# }
# for filter_name, filter_value in model._base_fetch_filters:
# if filter_name in resolved_kwargs:

# resolved_kwargs[filter_name] = filter_value
logger.debug("Resolved kwargs: %s", resolved_kwargs)
resolved_sort: Optional[str | list[str]] = None
if sort is not None:
Expand Down
2 changes: 1 addition & 1 deletion src/aind_slims_api/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class SlimsInstrument(SlimsBaseModel):
"""Model for an instance of the Behavior Session ContentEvent
"""Model for a SLIMS instrument record.
Examples
--------
Expand Down

0 comments on commit ac0cd43

Please sign in to comment.