-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add many command origination effects. #63
Changes from all commits
fe4991e
6db8146
5546b48
237bfe5
e32a590
9f3de1b
14cb669
4455eb1
ad9dc2c
278780c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import json | ||
import re | ||
from enum import EnumType | ||
from typing import get_args | ||
|
||
from pydantic import BaseModel, ConfigDict, model_validator | ||
from typing_extensions import Self | ||
|
||
from canvas_sdk.effects import Effect | ||
from canvas_sdk.effects import Effect, EffectType | ||
|
||
|
||
class _BaseCommand(BaseModel): | ||
|
@@ -14,15 +15,18 @@ class _BaseCommand(BaseModel): | |
class Meta: | ||
key = "" | ||
|
||
def constantized_key(self) -> str: | ||
return re.sub(r"(?<!^)(?=[A-Z])", "_", self.Meta.key).upper() | ||
|
||
# todo: update int to str as we should use external identifiers | ||
note_id: int | None = None | ||
note_uuid: str | None = None | ||
command_uuid: str | None = None | ||
user_id: int | ||
|
||
@model_validator(mode="after") | ||
def _verify_has_note_id_or_command_id(self) -> Self: | ||
if not self.note_id and not self.command_uuid: | ||
raise ValueError("Command should have either a note_id or a command_uuid.") | ||
def _verify_has_note_uuid_or_command_id(self) -> Self: | ||
if not self.note_uuid and not self.command_uuid: | ||
raise ValueError("Command should have either a note_uuid or a command_uuid.") | ||
return self | ||
|
||
@property | ||
|
@@ -52,7 +56,7 @@ def _get_property_type(cls, name: str) -> type: | |
@classmethod | ||
def command_schema(cls) -> dict: | ||
"""The schema of the command.""" | ||
base_properties = {"note_id", "command_uuid", "user_id"} | ||
base_properties = {"note_uuid", "command_uuid", "user_id"} | ||
schema = cls.model_json_schema() | ||
return { | ||
definition.get("commands_api_name", name): { | ||
|
@@ -66,23 +70,25 @@ def command_schema(cls) -> dict: | |
|
||
def originate(self) -> Effect: | ||
"""Originate a new command in the note body.""" | ||
if not self.note_id: | ||
if not self.note_uuid: | ||
raise AttributeError("Note id is required to originate a command") | ||
return { | ||
"type": f"ADD_{self.Meta.key.upper()}_COMMAND", | ||
"payload": { | ||
"user": self.user_id, | ||
"note": self.note_id, | ||
"data": self.values, | ||
}, | ||
} | ||
return Effect( | ||
type=EffectType.Value(f"ORIGINATE_{self.constantized_key()}_COMMAND"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed |
||
payload=json.dumps( | ||
{ | ||
"user": self.user_id, | ||
"note": self.note_uuid, | ||
"data": self.values, | ||
} | ||
), | ||
) | ||
|
||
def edit(self) -> Effect: | ||
"""Edit the command.""" | ||
if not self.command_uuid: | ||
raise AttributeError("Command uuid is required to edit a command") | ||
return { | ||
"type": f"EDIT_{self.Meta.key.upper()}_COMMAND", | ||
"type": f"EDIT_{self.constantized_key()}_COMMAND", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed these from |
||
"payload": { | ||
"user": self.user_id, | ||
"command": self.command_uuid, | ||
|
@@ -95,7 +101,7 @@ def delete(self) -> Effect: | |
if not self.command_uuid: | ||
raise AttributeError("Command uuid is required to delete a command") | ||
return { | ||
"type": f"DELETE_{self.Meta.key.upper()}_COMMAND", | ||
"type": f"DELETE_{self.constantized_key()}_COMMAND", | ||
"payload": {"command": self.command_uuid, "user": self.user_id}, | ||
} | ||
|
||
|
@@ -104,7 +110,7 @@ def commit(self) -> Effect: | |
if not self.command_uuid: | ||
raise AttributeError("Command uuid is required to commit a command") | ||
return { | ||
"type": f"COMMIT_{self.Meta.key.upper()}_COMMAND", | ||
"type": f"COMMIT_{self.constantized_key()}_COMMAND", | ||
"payload": {"command": self.command_uuid, "user": self.user_id}, | ||
} | ||
|
||
|
@@ -113,6 +119,6 @@ def enter_in_error(self) -> Effect: | |
if not self.command_uuid: | ||
raise AttributeError("Command uuid is required to enter in error a command") | ||
return { | ||
"type": f"ENTER_IN_ERROR_{self.Meta.key.upper()}_COMMAND", | ||
"type": f"ENTER_IN_ERROR_{self.constantized_key()}_COMMAND", | ||
"payload": {"command": self.command_uuid, "user": self.user_id}, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,11 @@ class Substitutions(Enum): | |
|
||
fdb_code: str = Field(json_schema_extra={"commands_api_name": "prescribe"}) | ||
icd10_codes: list[str] | None = Field( | ||
None, json_schema_extra={"commandsd_api_name": "indications"} | ||
None, json_schema_extra={"commands_api_name": "indications"} | ||
aduane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
sig: str | ||
days_supply: int | None = None | ||
quantity_to_dispense: Decimal | ||
quantity_to_dispense: Decimal | float | int | None = None | ||
type_to_dispense: str | ||
refills: int | ||
substitutions: Substitutions = Substitutions.ALLOWED # type: ignore | ||
|
@@ -38,10 +38,12 @@ def values(self) -> dict: | |
"icd10_codes": self.icd10_codes, | ||
"sig": self.sig, | ||
"days_supply": self.days_supply, | ||
"quantity_to_dispense": self.quantity_to_dispense, | ||
"type_to_dispense": self.type_to_dispense, | ||
"quantity_to_dispense": str(Decimal(self.quantity_to_dispense)) | ||
if self.quantity_to_dispense | ||
else None, | ||
# "type_to_dispense": self.type_to_dispense, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This breaks the interpreter if sent. Suppressing until the interpreter works. |
||
"refills": self.refills, | ||
"substitutions": self.substitutions, | ||
"substitutions": self.substitutions.value, | ||
"pharmacy": self.pharmacy, | ||
"prescriber_id": self.prescriber_id, | ||
"note_to_pharmacist": self.note_to_pharmacist, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should run the pydantic validators when these effect methods are called rather than on assignment. This should allow us to set the attributes individually instead of only in the constructor.
Example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea. want me to tackle that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, once this gets merged. We should chat about other priorities soon too.