Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mixins #329

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
779 changes: 646 additions & 133 deletions backend/dps_training_k/game/migrations/0001_initial.py

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions backend/dps_training_k/game/models/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
from django.db import models

from game.channel_notifications import ExerciseDispatcher
from helpers.eventable import NonEventable
from .lab import Lab
from .scheduled_event import ScheduledEvent
from .log_entry import LogEntry


class Exercise(NonEventable, models.Model):
class Exercise(models.Model):
class StateTypes(models.TextChoices):
CONFIGURATION = "C", "configuration"
RUNNING = "R", "running"
Expand Down
4 changes: 2 additions & 2 deletions backend/dps_training_k/game/models/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from game.models import MaterialInstance
from helpers.moveable_to import MoveableTo
from helpers.completed_actions import CompletedActionsMixin
from helpers.completed_actions import CompletedActionsGetable
from game.models import MaterialInstance
from template.models import Material
from template.constants import MaterialIDs
from template.models import Material


class Lab(MoveableTo, CompletedActionsMixin):
class Lab(MoveableTo, CompletedActionsGetable, models.Model):
exercise = models.OneToOneField(
"Exercise",
on_delete=models.CASCADE,
Expand Down
7 changes: 2 additions & 5 deletions backend/dps_training_k/game/models/patient_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
from configuration import settings
from game.models import Exercise
from game.channel_notifications import PatientInstanceDispatcher
from helpers.eventable import Eventable
from helpers.moveable import Moveable
from helpers.moveable_to import MoveableTo
from helpers.triage import Triage
from helpers.completed_actions import CompletedActionsMixin
from helpers.completed_actions import CompletedActionsGetable
from template.models import PatientState, Action, Subcondition, Material

# from game.models import ActionInstanceStateNames moved into function to avoid circular imports
Expand All @@ -28,9 +27,7 @@ def validate_patient_frontend_id(value):
)


class PatientInstance(
CompletedActionsMixin, Eventable, Moveable, MoveableTo, models.Model
):
class PatientInstance(CompletedActionsGetable, Moveable, MoveableTo, models.Model):
area = models.ForeignKey("Area", on_delete=models.CASCADE, null=True, blank=True)
lab = models.ForeignKey("Lab", on_delete=models.CASCADE, null=True, blank=True)
exercise = models.ForeignKey("Exercise", on_delete=models.CASCADE)
Expand Down
11 changes: 7 additions & 4 deletions backend/dps_training_k/helpers/completed_actions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

class CompletedActionsMixin:
class CompletedActionsGetable:
def get_completed_action_types(self):
from game.models import ActionInstanceState

action_instances = self.actioninstance_set.select_related("template").all()
applied_actions = set()
for action_instance in action_instances:
if action_instance.current_state.name in ActionInstanceState.completion_states():
if (
action_instance.current_state.name
in ActionInstanceState.completion_states()
):
applied_actions.add(action_instance.template)
return applied_actions
return applied_actions
31 changes: 0 additions & 31 deletions backend/dps_training_k/helpers/eventable.py

This file was deleted.

4 changes: 1 addition & 3 deletions backend/dps_training_k/helpers/moveable.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def try_moving_to(self, obj) -> tuple[bool, str]:

def check_moving_to(self, obj) -> tuple[bool, str]:
"""Returns whether the object might be moved successfully and an error message if not."""
from helpers.moveable_to import MoveableTo

if not isinstance(obj, MoveableTo):
if not getattr(obj, "can_be_moved_to", False)():
raise TypeError(
f"Object must inherit from MoveableTo, got {type(obj).__name__} instead."
)
Expand Down
4 changes: 4 additions & 0 deletions backend/dps_training_k/helpers/moveable_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def personnel_assigned(self) -> List["p.Personnel"]:
def personnel_available(self) -> List["p.Personnel"]:
return list(self.personnel_set.filter(action_instance=None))

@staticmethod
def can_be_moved_to():
return True

@staticmethod
@abstractmethod
def frontend_model_name():
Expand Down
Loading