diff --git a/backend/dps_training_k/game/channel_notifications.py b/backend/dps_training_k/game/channel_notifications.py index c2340a4a..4c8eb2e5 100644 --- a/backend/dps_training_k/game/channel_notifications.py +++ b/backend/dps_training_k/game/channel_notifications.py @@ -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" @@ -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) diff --git a/backend/dps_training_k/game/consumers/patient_consumer.py b/backend/dps_training_k/game/consumers/patient_consumer.py index 7cc07f0a..9470e820 100644 --- a/backend/dps_training_k/game/consumers/patient_consumer.py +++ b/backend/dps_training_k/game/consumers/patient_consumer.py @@ -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) @@ -31,6 +39,7 @@ def __init__(self, *args, **kwargs): self.handle_triage, "triage", ), + self.PatientIncomingMessageTypes.ACTION_ADD: (self.handle_action_add,), } def connect(self): @@ -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}, + ) diff --git a/backend/dps_training_k/game/consumers/trainer_consumer.py b/backend/dps_training_k/game/consumers/trainer_consumer.py index 5adc76e1..8735194c 100644 --- a/backend/dps_training_k/game/consumers/trainer_consumer.py +++ b/backend/dps_training_k/game/consumers/trainer_consumer.py @@ -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" diff --git a/backend/dps_training_k/game/models/saved_exercise.py b/backend/dps_training_k/game/models/saved_exercise.py index 82cb1816..c125c40a 100644 --- a/backend/dps_training_k/game/models/saved_exercise.py +++ b/backend/dps_training_k/game/models/saved_exercise.py @@ -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: