Skip to content

Commit

Permalink
Implement release check events and abort action (#13)
Browse files Browse the repository at this point in the history
* fix base action

* typo

* new release events

* common dialog data

* adapt schema

* fix dependency

* remove general release events

* add release check events for documents, parts and ecs

* update schema

* remove unneeded constructors

* cdb_obsolete can be None

* Functions can return Actions or list of Actions directly

* make doc

* add documentation for new actions and events

* make schema

* unused import

* remove mentioning workloadresponse

* clarify check events

* change order

* remove some unused code

* make object linking independent of field names

* rename fields to attached_<object>

* adjust documentation to include the attached_<object> fields

---------

Co-authored-by: Jens Kürten <[email protected]>
  • Loading branch information
jens-kuerten and Jens Kürten authored Aug 7, 2024
1 parent c1aac00 commit 49c7955
Show file tree
Hide file tree
Showing 27 changed files with 553 additions and 116 deletions.
4 changes: 3 additions & 1 deletion csfunctions/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from pydantic import Field

from .abort_and_show_error import AbortAndShowErrorAction
from .base import ActionNames
from .dummy import DummyAction

Action = Annotated[Union[AbortAndShowErrorAction, DummyAction], Field(discriminator="name")]
ActionUnion = Union[AbortAndShowErrorAction, DummyAction]
Action = Annotated[ActionUnion, Field(discriminator="name")]
19 changes: 3 additions & 16 deletions csfunctions/actions/abort_and_show_error.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
from typing import Literal

from pydantic import BaseModel, Field
from pydantic import Field

from .base import ActionNames, BaseAction


class AbortAndShowErrorData(BaseModel):
def __init__(self, message: str, **kwargs):
super().__init__(message=message, **kwargs)

message: str = Field("unknown error", description="error message to be shown to the user")


class AbortAndShowErrorAction(BaseAction):
def __init__(self, id: str, message: str | None = None, **kwargs): # pylint: disable=redefined-builtin
if message:
super().__init__(name=ActionNames.ABORT_AND_SHOW_ERROR, id=id, data=AbortAndShowErrorData(message=message))
else:
super().__init__(name=ActionNames.ABORT_AND_SHOW_ERROR, id=id, data=kwargs["data"])

name: Literal[ActionNames.ABORT_AND_SHOW_ERROR]
data: AbortAndShowErrorData
name: Literal[ActionNames.ABORT_AND_SHOW_ERROR] = ActionNames.ABORT_AND_SHOW_ERROR
message: str = Field("unknown error", description="error message to be shown to the user")
5 changes: 2 additions & 3 deletions csfunctions/actions/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum

from pydantic import BaseModel, Field
from pydantic import BaseModel


class ActionNames(str, Enum):
Expand All @@ -10,5 +10,4 @@ class ActionNames(str, Enum):

class BaseAction(BaseModel):
name: str
id: str = Field(..., description="unique identifier")
data: dict | None = None
id: str | None = None
14 changes: 12 additions & 2 deletions csfunctions/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,39 @@

from pydantic import Field

from .document_release import DocumentReleaseData, DocumentReleaseDialogData, DocumentReleaseEvent
from .dialog_data import DocumentReleaseDialogData, PartReleaseDialogData
from .document_release import DocumentReleaseData, DocumentReleaseEvent
from .document_release_check import DocumentReleaseCheckData, DocumentReleaseCheckEvent
from .dummy import DummyEvent, DummyEventData
from .engineering_change_release import EngineeringChangeRelease, EngineeringChangeReleaseData
from .engineering_change_release_check import EngineeringChangeReleaseCheck, EngineeringChangeReleaseCheckData
from .field_value_calculation import FieldValueCalculationData, FieldValueCalculationEvent
from .part_release import PartReleaseData, PartReleaseDialogData, PartReleaseEvent
from .part_release import PartReleaseData, PartReleaseEvent
from .part_release_check import PartReleaseCheckData, PartReleaseCheckEvent
from .workflow_task_trigger import WorkflowTaskTriggerEvent, WorkflowTaskTriggerEventData

Event = Annotated[
Union[
DocumentReleaseEvent,
DocumentReleaseCheckEvent,
PartReleaseEvent,
PartReleaseCheckEvent,
FieldValueCalculationEvent,
DummyEvent,
EngineeringChangeRelease,
EngineeringChangeReleaseCheck,
WorkflowTaskTriggerEvent,
],
Field(discriminator="name"),
]
EventData = Union[
DocumentReleaseData,
DocumentReleaseCheckData,
PartReleaseData,
PartReleaseCheckData,
FieldValueCalculationData,
DummyEventData,
EngineeringChangeReleaseData,
EngineeringChangeReleaseCheckData,
WorkflowTaskTriggerEventData,
]
4 changes: 3 additions & 1 deletion csfunctions/events/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
class EventNames(str, Enum):
DUMMY = "dummy"
DOCUMENT_RELEASE = "document_release"
DOCUMENT_RELEASE_CHECK = "document_release_check"
PART_RELEASE = "part_release"
PART_RELEASE_CHECK = "part_release_check"
ENGINEERING_CHANGE_RELEASE = "engineering_change_release"
ENGINEERING_CHANGE_RELEASE_CHECK = "engineering_change_release_check"
FIELD_VALUE_CALCULATION = "field_value_calculation"
WORKFLOW_TASK_TRIGGER = "workflow_task_trigger"


class BaseEvent(BaseModel):
name: str
event_id: str = Field(..., description="unique identifier")
data: dict | None = None
15 changes: 15 additions & 0 deletions csfunctions/events/dialog_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Literal

from pydantic import BaseModel, Field


class DocumentReleaseDialogData(BaseModel):
dialog_type: Literal["document_release"] = "document_release"
cdbprot_remark: str | None = Field(None, description="remark")
cdb_ec_id: str | None = Field(None, description="Engineering Change ID")


class PartReleaseDialogData(BaseModel):
dialog_type: Literal["part_release"] = "part_release"
cdbprot_remark: str | None = Field("", description="remark")
cdb_ec_id: str | None = Field("", description="Engineering Change ID")
16 changes: 3 additions & 13 deletions csfunctions/events/document_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,15 @@
from csfunctions.objects import Document, Part

from .base import BaseEvent, EventNames


class DocumentReleaseDialogData(BaseModel):
cdbprot_remark: str | None = Field(None, description="remark")
cdb_ec_id: str | None = Field(None, description="Engineering Change ID")
from .dialog_data import DocumentReleaseDialogData


class DocumentReleaseData(BaseModel):
def __init__(self, documents: list[Document], parts: list[Part], dialog_data: dict, **kwargs):
super().__init__(documents=documents, parts=parts, dialog_data=dialog_data, **kwargs)

documents: list[Document] = Field(..., description="List if documents that were released.")
documents: list[Document] = Field(..., description="List of documents that were released.")
parts: list[Part] = Field(..., description="List of parts that belong to the released documents")
dialog_data: DocumentReleaseDialogData


class DocumentReleaseEvent(BaseEvent):
def __init__(self, event_id: str, data: DocumentReleaseData, **_):
super().__init__(name=EventNames.DOCUMENT_RELEASE, event_id=event_id, data=data)

name: Literal[EventNames.DOCUMENT_RELEASE]
name: Literal[EventNames.DOCUMENT_RELEASE] = EventNames.DOCUMENT_RELEASE
data: DocumentReleaseData
19 changes: 19 additions & 0 deletions csfunctions/events/document_release_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Literal

from pydantic import BaseModel, Field

from csfunctions.objects import Document, Part

from .base import BaseEvent, EventNames
from .dialog_data import DocumentReleaseDialogData


class DocumentReleaseCheckData(BaseModel):
documents: list[Document] = Field(..., description="List of documents that will be released.")
attached_parts: list[Part] = Field(..., description="List of parts that belong to the documents")
dialog_data: DocumentReleaseDialogData


class DocumentReleaseCheckEvent(BaseEvent):
name: Literal[EventNames.DOCUMENT_RELEASE_CHECK] = EventNames.DOCUMENT_RELEASE_CHECK
data: DocumentReleaseCheckData
5 changes: 1 addition & 4 deletions csfunctions/events/engineering_change_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ class EngineeringChangeReleaseData(BaseModel):


class EngineeringChangeRelease(BaseEvent):
def __init__(self, event_id: str, data: EngineeringChangeReleaseData, **_):
super().__init__(name=EventNames.ENGINEERING_CHANGE_RELEASE, event_id=event_id, data=data)

name: Literal[EventNames.ENGINEERING_CHANGE_RELEASE]
name: Literal[EventNames.ENGINEERING_CHANGE_RELEASE] = EventNames.ENGINEERING_CHANGE_RELEASE
data: EngineeringChangeReleaseData
20 changes: 20 additions & 0 deletions csfunctions/events/engineering_change_release_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import Literal

from pydantic import BaseModel, Field

from csfunctions.objects import Document, EngineeringChange, Part

from .base import BaseEvent, EventNames


class EngineeringChangeReleaseCheckData(BaseModel):
attached_documents: list[Document] = Field(..., description="List of included documents.")
attached_parts: list[Part] = Field(..., description="List of included parts.")
engineering_changes: list[EngineeringChange] = Field(
..., description="List of engineering changes that will be released."
)


class EngineeringChangeReleaseCheck(BaseEvent):
name: Literal[EventNames.ENGINEERING_CHANGE_RELEASE_CHECK] = EventNames.ENGINEERING_CHANGE_RELEASE_CHECK
data: EngineeringChangeReleaseCheckData
14 changes: 2 additions & 12 deletions csfunctions/events/part_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,15 @@
from csfunctions.objects import Document, Part

from .base import BaseEvent, EventNames


class PartReleaseDialogData(BaseModel):
cdbprot_remark: str | None = Field("", description="remark")
cdb_ec_id: str | None = Field("", description="Engineering Change ID")
from .dialog_data import PartReleaseDialogData


class PartReleaseData(BaseModel):
def __init__(self, parts: list[Part], dialog_data: dict, **kwargs):
super().__init__(parts=parts, dialog_data=dialog_data, **kwargs)

parts: list[Part] = Field(..., description="List if parts that were released.")
documents: list[Document] = Field(..., description="List if documents that are referenced by the released part.")
dialog_data: PartReleaseDialogData


class PartReleaseEvent(BaseEvent):
def __init__(self, event_id: str, data: PartReleaseData, **_):
super().__init__(name=EventNames.PART_RELEASE, event_id=event_id, data=data)

name: Literal[EventNames.PART_RELEASE]
name: Literal[EventNames.PART_RELEASE] = EventNames.PART_RELEASE
data: PartReleaseData
19 changes: 19 additions & 0 deletions csfunctions/events/part_release_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Literal

from pydantic import BaseModel, Field

from csfunctions.objects import Document, Part

from .base import BaseEvent, EventNames
from .dialog_data import PartReleaseDialogData


class PartReleaseCheckData(BaseModel):
parts: list[Part] = Field(..., description="List of parts that will be released.")
attached_documents: list[Document] = Field(..., description="List of documents that are referenced by the parts.")
dialog_data: PartReleaseDialogData


class PartReleaseCheckEvent(BaseEvent):
name: Literal[EventNames.PART_RELEASE_CHECK]
data: PartReleaseCheckData
32 changes: 10 additions & 22 deletions csfunctions/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import yaml
from pydantic import BaseModel

from csfunctions import ErrorResponse, Event, Request
from csfunctions import ErrorResponse, Event, Request, WorkloadResponse
from csfunctions.actions import ActionUnion
from csfunctions.config import ConfigModel, FunctionModel
from csfunctions.objects.base import BaseObject
from csfunctions.objects import BaseObject
from csfunctions.response import ResponseUnion
from csfunctions.service import Service

Expand Down Expand Up @@ -46,26 +47,6 @@ def get_function_callable(function_name: str, function_dir: str) -> Callable:
return getattr(mod, function_name)


def link_documents2parts(event: Event) -> None:
"""
Links the relationship document -> part
"""
if hasattr(event.data, "documents") and hasattr(event.data, "parts"):
# build parts dicts, indexed by teilenummer@index
parts = {}
for part in event.data.parts:
key = part.teilenummer + "@" + part.t_index
parts[key] = part

# link the parts to the documents
for document in event.data.documents:
if document.teilenummer is None or document.t_index is None:
continue
key = document.teilenummer + "@" + document.t_index
if key in parts:
document.part = parts[key]


def link_objects(event: Event):
"""
Link the relationships between objects, to allow accessing relationships with dot notation,
Expand Down Expand Up @@ -108,6 +89,13 @@ def execute(function_name: str, request_body: str, function_dir: str = "src") ->
if response is None:
return ""

if isinstance(response, ActionUnion):
# wrap returned Actions into a WorkloadResponse
response = WorkloadResponse(actions=[response])
elif isinstance(response, list) and all(isinstance(o, ActionUnion) for o in response):
# wrap list of Actions into a WorkloadResponse
response = WorkloadResponse(actions=response)

if not isinstance(
response, ResponseUnion
): # need to check for ResponseUnion instead of Response, because isinstance doesn't work with annotated unions
Expand Down
1 change: 1 addition & 0 deletions csfunctions/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pydantic import Field

from .base import BaseObject
from .briefcase import Briefcase
from .classification import ObjectPropertyValue
from .document import CADDocument, Document
Expand Down
8 changes: 5 additions & 3 deletions csfunctions/objects/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from pydantic import Field

from csfunctions.util import get_items_of_type

from .base import BaseObject, ObjectType
from .file import File

Expand Down Expand Up @@ -31,7 +33,7 @@ class Document(BaseObject):
category2_name_de: str | None = Field(..., description="Category")
z_categ1: str | None = Field(..., description="Main Category")
z_categ2: str | None = Field(..., description="Category")
cdb_obsolete: int = Field(..., description="Obsolete")
cdb_obsolete: int | None = Field(..., description="Obsolete")
z_status: int = Field(..., description="Status Number")
z_status_txt: str = Field(..., description="Status Text")
autoren: str | None = Field(..., description="Authors, comma separated")
Expand Down Expand Up @@ -79,8 +81,8 @@ class Document(BaseObject):
part: Optional["Part"] = Field(None, exclude=True)

def link_objects(self, data: "EventData"):
parts = getattr(data, "parts", None)
if parts and self.teilenummer:
if self.teilenummer:
parts = get_items_of_type(data, Part)
self._link_part(parts)

def _link_part(self, parts: list["Part"]):
Expand Down
9 changes: 7 additions & 2 deletions csfunctions/objects/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@

from pydantic import Field

from csfunctions.util import get_items_of_type

from .base import BaseObject, ObjectType

if TYPE_CHECKING:
from csfunctions.events import EventData

try:
from .document import Document
except ImportError:
pass


class Part(BaseObject):
Expand Down Expand Up @@ -77,8 +82,8 @@ class Part(BaseObject):
documents: list["Document"] = Field([], exclude=True)

def link_objects(self, data: "EventData"):
documents = getattr(data, "documents", None)
if documents and self.document_ids:
if self.document_ids:
documents = get_items_of_type(data, Document)
self._link_documents(documents)

def _link_documents(self, documents: list["Document"]):
Expand Down
Loading

0 comments on commit 49c7955

Please sign in to comment.