Skip to content

Commit

Permalink
UNFINISHED: change state and add to history
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Sep 4, 2023
1 parent 29657fa commit b91c8d7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
5 changes: 0 additions & 5 deletions src/zinolib/event_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,3 @@ def set_log_for_event(self, event_or_id: EventOrId, log_list: List[LogEntry]):
event.log = log_list
self._set_event(event)
return event

# not finished
def add_history_for_event(self, event: EventType, history_entry):
event.history.append(history_entry)
return event
43 changes: 37 additions & 6 deletions src/zinolib/zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Now you can use the session when initializing Zino1EventEngine::
> from zinolib.zino1 import Zino1EventEngine
> event_engine = Zino1EventEngine(session)
To get a list of currently available events::
Expand Down Expand Up @@ -48,9 +49,9 @@
"""

from datetime import datetime, timezone
from typing import List, Dict, Union, TypedDict
from typing import List, Dict, Union, TypedDict, Optional

from .event_types import EventType, Event, EventEngine, HistoryEntry, LogEntry
from .event_types import EventType, Event, EventEngine, HistoryEntry, LogEntry, AdmState


HistoryDict = TypedDict(
Expand Down Expand Up @@ -117,7 +118,7 @@ def convert_values(cls, attrdict):
return attrdict

@staticmethod
def set_admin_state(session, event: EventType, state):
def set_admin_state(session, event: EventType, state: AdmState) -> bool:
return session.set_state(event.id, state.value)

@staticmethod
Expand Down Expand Up @@ -185,7 +186,9 @@ def add(cls, session, message: str, event: EventType):
# fetch history
raw_history = cls.get_history(session, event.id)
parsed_history = cls.parse_response(raw_history)
event.history = HistoryEntry.create_list(parsed_history)
new_history = HistoryEntry.create_list(parsed_history)
if new_history != event.history:
event.history = new_history
return event


Expand Down Expand Up @@ -250,13 +253,41 @@ def create_event_from_id(self, event_id: int):
attrdict = self._event_adapter.convert_values(attrdict)
return Event.create(attrdict)

def get_history_for_id(self, event_id: int):
def get_updated_event_for_id(self, event_id):
event = self.create_event_from_id(event_id)
history_list = self.get_history_for_id(event.id)
self.set_history_for_event(event, history_list)
log_list = self.get_log_for_id(event.id)
self.set_log_for_event(event, log_list)
return event

def change_admin_state_for_id(self, event_id, admin_state: AdmState) -> Optional[Event]:
self.check_session()
event = self._get_event(event_id)
success = self._event_adapter.set_admin_state(self.session, event, admin_state)
if success:
event = self.get_updated_event_for_id(event_id)
self._set_event(event)
return event
return None

def get_history_for_id(self, event_id: int) -> list[HistoryEntry]:
self.check_session()
raw_history = self._history_adapter.get_history(self.session, event_id)
parsed_history = self._history_adapter.parse_response(raw_history)
return HistoryEntry.create_list(parsed_history)

def get_log_for_id(self, event_id: int):
def add_history_entry_for_id(self, event_id: int, message) -> Optional[EventType]:
self.check_session()
event = self._get_event(event_id)
success = self._history_adapter.add(self.session, message, event)
if success:
event = self.get_updated_event_for_id(event_id)
self._set_event(event)
return event
return None

def get_log_for_id(self, event_id: int) -> list[LogEntry]:
self.check_session()
raw_log = self._log_adapter.get_log(self.session, event_id)
parsed_log = self._log_adapter.parse_response(raw_log)
Expand Down

0 comments on commit b91c8d7

Please sign in to comment.