Skip to content

Commit

Permalink
#82: add personnel model
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolkenfarmer committed Mar 27, 2024
1 parent aa2519e commit 91302f3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
40 changes: 39 additions & 1 deletion backend/dps_training_k/game/consumers/trainer_consumer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .abstract_consumer import AbstractConsumer
from game.models import Exercise
from game.models import Exercise, Personnel
from game.models import Area


Expand All @@ -14,6 +14,9 @@ class TrainerIncomingMessageTypes:
EXERCISE_RESUME = "exercise-resume"
AREA_ADD = "area-add"
AREA_DELETE = "area-delete"
PERSONNEL_ADD = "personnel-add"
PERSONNEL_DELETE = "personnel-delete"
PERSONNEL_UPDATE = "personnel-update"

class TrainerOutgoingMessageTypes:
RESPONSE = "response"
Expand Down Expand Up @@ -58,6 +61,19 @@ def __init__(self, *args, **kwargs):
self.handle_delete_area,
"areaName",
),
self.TrainerIncomingMessageTypes.PERSONNEL_ADD: (
self.handle_add_personnel,
"areaName",
),
self.TrainerIncomingMessageTypes.PERSONNEL_DELETE: (
self.handle_delete_personnel,
"personnelId",
),
self.TrainerIncomingMessageTypes.PERSONNEL_UPDATE: (
self.handle_update_personnel,
"personnelId",
"personnelName",
),
}

def connect(self):
Expand Down Expand Up @@ -109,3 +125,25 @@ def handle_add_area(self):
def handle_delete_area(self, areaName):
Area.objects.filter(name=areaName).delete()
# TODO: send update to all subscribers

def handle_add_personnel(self, areaName):
try:
area = Area.objects.get(name=areaName)
Personnel(area=area)
self._send_exercise(self.exercise)
except Area.DoesNotExist:
print(f"No area found with the name {areaName}.")
except Area.MultipleObjectsReturned:
print(
f"Multiple areas found with the name {areaName}. Please provide a unique area name."
)

def handle_delete_personnel(self, personnelId):
Personnel.objects.filter(id=personnelId).delete()
self._send_exercise(self.exercise)

def handle_update_personnel(self, personnelId, personnelName):
personnel = Personnel.objects.get(id=personnelId)
personnel.name = personnelName
personnel.save()
self._send_exercise(self.exercise)
1 change: 1 addition & 0 deletions backend/dps_training_k/game/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .saved_exercise import SavedExercise
from .patient import Patient
from .patient_state import PatientState
from .personnel import Personnel
from .user import User
from .scheduled_event import ScheduledEvent
from .area import Area
16 changes: 16 additions & 0 deletions backend/dps_training_k/game/models/personnel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.db import models


class Personnel(models.Model):
name = models.CharField(max_length=100, blank=True)
area = models.ForeignKey("Area", on_delete=models.CASCADE)

def __str__(self):
return f"Personnel #{self.id} called {self.name} in area {self.area.name}"

def save(self, *args, **kwargs):
creating = not self.pk
super().save(*args, **kwargs)
if creating:
self.name = f"Personnel {self.id}"
super().save(update_fields=["name"])

0 comments on commit 91302f3

Please sign in to comment.