Skip to content

Commit

Permalink
#330 backend: add moved_from field to assignable mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolkenfarmer committed Sep 14, 2024
1 parent 860bebf commit 66dbdf5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Generated by Django 5.0.1 on 2024-09-12 12:47

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("contenttypes", "0002_remove_content_type_name"),
("game", "0006_remove_area_unique_area_names_per_exercise"),
]

operations = [
migrations.AddField(
model_name="materialinstance",
name="moved_from_content_type",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="contenttypes.contenttype",
),
),
migrations.AddField(
model_name="materialinstance",
name="moved_from_object_id",
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AddField(
model_name="personnel",
name="moved_from_content_type",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="contenttypes.contenttype",
),
),
migrations.AddField(
model_name="personnel",
name="moved_from_object_id",
field=models.PositiveIntegerField(blank=True, null=True),
),
]
40 changes: 26 additions & 14 deletions backend/dps_training_k/helpers/assignable.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models

from helpers import exactly_one_field_not_null


class Assignable(
models.Model,
):
class Assignable(models.Model):
class Meta:
abstract = True

Expand All @@ -18,10 +18,16 @@ class Meta:
area = models.ForeignKey("Area", on_delete=models.CASCADE, null=True, blank=True)
lab = models.ForeignKey("Lab", on_delete=models.CASCADE, null=True, blank=True)

# GenericForeignKey is just a virtual field for unified access, only _content_type and _object_id are actually stored in DB
moved_from_content_type = models.ForeignKey(
ContentType, on_delete=models.SET_NULL, null=True, blank=True
)
moved_from_object_id = models.PositiveIntegerField(null=True, blank=True)
moved_from = GenericForeignKey("moved_from_content_type", "moved_from_object_id")

def __init_subclass__(cls, **kwargs):
"""Needed to create unique name for each constraint given to the subclasses of Assignable, but I can already define the constraint here."""
"""Create a unique name for each constraint for subclasses of Assignable."""
super().__init_subclass__(**kwargs)
# Add a unique constraint for each subclass of Assignable
constraint_name = (
f"assignable_{cls.__name__.lower()}_exactly_one_field_not_null"
)
Expand All @@ -34,6 +40,8 @@ def __init_subclass__(cls, **kwargs):
def _perform_move(self, obj):
from game.models import PatientInstance, Area, Lab

self.moved_from = self.attached_instance()

if isinstance(obj, PatientInstance):
if self.patient_instance is obj:
return False
Expand All @@ -52,26 +60,30 @@ def _perform_move(self, obj):
self.patient_instance = None
self.area = None
self.lab = obj
self.save(update_fields=["patient_instance", "area", "lab"])

self.save(
update_fields=[
"patient_instance",
"area",
"lab",
"moved_from_content_type",
"moved_from_object_id",
]
)
return True, ""

@staticmethod
def can_move_to_type(obj):
from game.models import PatientInstance, Area, Lab

return (
isinstance(obj, PatientInstance)
or isinstance(obj, Area)
or isinstance(obj, Lab)
)
return isinstance(obj, (PatientInstance, Area, Lab))

def is_blocked(self):
return self.action_instance is not None

def attached_instance(self):
return (
self.patient_instance or self.area or self.lab
) # first not null value determined by short-circuiting
# First non-null value determined by short-circuiting
return self.patient_instance or self.area or self.lab

def block(self, action_instance):
self.action_instance = action_instance
Expand Down

0 comments on commit 66dbdf5

Please sign in to comment.