Skip to content

Commit

Permalink
Fixes issues with and related to PatientAssetBed view (#1325)
Browse files Browse the repository at this point in the history
* fixes #1324, disallow asset type modifications

* fixes #1323, rectify bed_is_occupied filter for patient asset bed view

* fixes #1322, cascade delete `AssetBed`

* fixes #1319, disallow linking asset beds of same asset type

* fix queryset delete of AssetBed

* oops, forgot to run the migration :)

* delete assetbed before asset/bed

* only allow setting asset class on creation (or updation if asset class is not set)
  • Loading branch information
rithviknishad authored Jun 6, 2023
1 parent 0cbff7c commit 831c216
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
10 changes: 10 additions & 0 deletions care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,20 @@ def validate(self, attrs):
if attrs["last_serviced_on"] > datetime.now().date():
raise ValidationError("Last serviced on cannot be in the future")

# only allow setting asset class on creation (or updation if asset class is not set)
if (
attrs.get("asset_class")
and self.instance
and self.instance.asset_class
and self.instance.asset_class != attrs["asset_class"]
):
raise ValidationError({"asset_class": "Cannot change asset class"})

return super().validate(attrs)

def update(self, instance, validated_data):
user = self.context["request"].user

with transaction.atomic():
if (
"current_location" in validated_data
Expand Down
19 changes: 17 additions & 2 deletions care/facility/api/serializers/bed.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from django.shortcuts import get_object_or_404
from rest_framework.exceptions import ValidationError
from rest_framework.serializers import BooleanField, ModelSerializer, UUIDField
from rest_framework.serializers import (
BooleanField,
ModelSerializer,
SerializerMethodField,
UUIDField,
)

from care.facility.api.serializers import TIMESTAMP_FIELDS
from care.facility.api.serializers.asset import AssetLocationSerializer, AssetSerializer
Expand All @@ -14,7 +19,6 @@
from care.utils.queryset.facility import get_facility_queryset
from care.utils.serializer.external_id_field import ExternalIdSerializerField
from config.serializers import ChoiceField
from rest_framework.serializers import SerializerMethodField


class BedSerializer(ModelSerializer):
Expand Down Expand Up @@ -86,6 +90,17 @@ def validate(self, attrs):
raise ValidationError(
{"asset": "Should be in the same facility as the bed"}
)
if (
asset.asset_class in ["HL7MONITOR", "VENTILATOR"]
and AssetBed.objects.filter(
bed=bed, asset__asset_class=asset.asset_class
).exists()
):
raise ValidationError(
{
"asset": "Bed is already in use by another asset of the same class"
}
)
else:
raise ValidationError(
{"asset": "Field is Required", "bed": "Field is Required"}
Expand Down
13 changes: 9 additions & 4 deletions care/facility/api/viewsets/bed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core.exceptions import ValidationError as DjangoValidationError
from django.db.models import OuterRef, Subquery
from django_filters import rest_framework as filters
from rest_framework import filters as drf_filters
from rest_framework.exceptions import PermissionDenied
Expand All @@ -16,9 +17,9 @@

from care.facility.api.serializers.bed import (
AssetBedSerializer,
PatientAssetBedSerializer,
BedSerializer,
ConsultationBedSerializer,
PatientAssetBedSerializer,
)
from care.facility.models.bed import AssetBed, Bed, ConsultationBed
from care.facility.models.patient_base import BedTypeChoices
Expand Down Expand Up @@ -128,13 +129,17 @@ class PatientAssetBedFilter(filters.FilterSet):
bed_is_occupied = filters.BooleanFilter(method="filter_bed_is_occupied")

def filter_bed_is_occupied(self, queryset, name, value):
return queryset.exclude(
bed__consultationbed__consultation__current_bed__isnull=value
return queryset.filter(
bed__id__in=Subquery(
ConsultationBed.objects.filter(
bed__id=OuterRef("bed__id"), end_date__isnull=value
).values("bed__id")
)
)


class PatientAssetBedViewSet(ListModelMixin, GenericViewSet):
queryset = AssetBed.objects.all().select_related("asset", "bed")
queryset = AssetBed.objects.select_related("asset", "bed").order_by("-created_date")
serializer_class = PatientAssetBedSerializer
filter_backends = (
filters.DjangoFilterBackend,
Expand Down
20 changes: 20 additions & 0 deletions care/facility/migrations/0359_auto_20230529_1907.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.2.11 on 2023-05-29 13:37

from django.db import migrations
from django.db.models import Q


class Migration(migrations.Migration):
def delete_asset_beds(apps, schema_editor):
AssetBed = apps.get_model("facility", "AssetBed")
AssetBed.objects.filter(Q(asset__deleted=True) | Q(bed__deleted=True)).update(
deleted=True
)

dependencies = [
("facility", "0358_auto_20230524_1853"),
]

operations = [
migrations.RunPython(delete_asset_beds),
]
6 changes: 6 additions & 0 deletions care/facility/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ class Meta:
),
]

def delete(self, *args, **kwargs):
from care.facility.models.bed import AssetBed

AssetBed.objects.filter(asset=self).update(deleted=True)
super().delete(*args, **kwargs)

def __str__(self):
return self.name

Expand Down
4 changes: 4 additions & 0 deletions care/facility/models/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def save(self, *args, **kwargs) -> None:
self.validate()
return super().save(*args, **kwargs)

def delete(self, *args, **kwargs) -> None:
AssetBed.objects.filter(bed=self).update(deleted=True)
super().delete(*args, **kwargs)


class AssetBed(BaseModel):
asset = models.ForeignKey(Asset, on_delete=models.PROTECT, null=False, blank=False)
Expand Down

0 comments on commit 831c216

Please sign in to comment.