Skip to content

Commit

Permalink
# backend: add comments to consumers and channel notifications to cla…
Browse files Browse the repository at this point in the history
…rify resposibilties
  • Loading branch information
claasga committed Apr 1, 2024
1 parent 7379ac0 commit 156d5fa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
38 changes: 37 additions & 1 deletion backend/dps_training_k/game/channel_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from helpers.signals import post_update
from game.models import Patient
from game.models import Patient, AppliedAction
from template.serializer.state_serialize import StateSerializer

"""
This package is responsible to decide when to notify which consumers.
Events must be sent as strings, thus objects are passed by ids.
Sending events is done by the celery worker.
"""


class ChannelEventTypes:
ACTION_CONFIRMATION = "action-confirmation"
Expand All @@ -32,3 +38,33 @@ def notify_patient_phase_change(patient):
**serializer.data,
}
_notify_group(channel, event)


# ToDO: Update for actual Actions
@receiver(post_save, action=AppliedAction)
def chose_notification_method(action):
if action.state == AppliedAction.State.DECLINED:
notify_action_declined(action)
if action.state == AppliedAction.State.PLANNED:
notify_action_confirmed(action)


def notify_action_confirmed(action):
if action.state == AppliedAction.State.DECLINED:
return
channel = action.patient.group_name
event = {
"messageType": ChannelEventTypes.ACTION_CONFIRMATION,
"appliedAction": action.id,
}
_notify_group(channel, event)


def notify_action_declined(action):
channel = action.patient.group_name
event = {
"messageType": ChannelEventTypes.ACTION_DECLINATION,
"appliedAction": action.id,
"actionDeclinationReason": action.reason_of_declination,
}
_notify_group(channel, event)
16 changes: 16 additions & 0 deletions backend/dps_training_k/game/consumers/patient_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@


class PatientConsumer(AbstractConsumer):
"""
This class is responsible for DECODING messages from the frontend(user==patient) into method calls and
ENCODING events from the backend into JSONs to send to the frontend. When encoding events this also implies
deciding what part of the event should be sent to the frontend(filtering).
"""

class PatientIncomingMessageTypes:
EXAMPLE = "example"
TEST_PASSTHROUGH = "test-passthrough"
TRIAGE = "triage"
ACTION_ADD = "action-add"

class PatientOutgoingMessageTypes:
RESPONSE = "response"
EXERCISE = "exercise"
TEST_PASSTHROUGH = "test-passthrough"
ACTION_CONFIRMATION = "action-confirmation"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -31,6 +39,7 @@ def __init__(self, *args, **kwargs):
self.handle_triage,
"triage",
),
self.PatientIncomingMessageTypes.ACTION_ADD: (self.handle_action_add,),
}

def connect(self):
Expand Down Expand Up @@ -61,3 +70,10 @@ def handle_test_passthrough(self):
def handle_triage(self, triage):
self.patient.triage = triage
self._send_exercise()

def action_confirmation(self, event):
action = event["action"]
self.send_event(
self.PatientOutgoingMessageTypes.RESPONSE,
{"actionName": action.name, "actionId": action.id},
)
6 changes: 6 additions & 0 deletions backend/dps_training_k/game/consumers/trainer_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@


class TrainerConsumer(AbstractConsumer):
"""
This class is responsible for DECODING messages from the frontend(user==trainer) into method calls and
ENCODING events from the backend into JSONs to send to the frontend. When encoding events this also implies
deciding what part of the event should be sent to the frontend(filtering).
"""

class TrainerIncomingMessageTypes:
EXAMPLE = "example"
EXERCISE_CREATE = "trainer-exercise-create"
Expand Down
4 changes: 2 additions & 2 deletions backend/dps_training_k/game/models/saved_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class SavedExercise(models.Model):
time_speed_up = models.FloatField(default=1.0)

@classmethod
def save_exercise(cls, serialized_exercise, name):
return cls.objects.create(serialized_exercise, name)
def save_exercise(cls, serialized_exercise, name, time_speed_up=1.0):
return cls.objects.create(serialized_exercise, name, time_speed_up)


class SavedExerciseFactory:
Expand Down

0 comments on commit 156d5fa

Please sign in to comment.