Skip to content

Commit

Permalink
rebase migrations and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed Sep 29, 2024
1 parent a90848f commit aee281a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 21 deletions.
31 changes: 23 additions & 8 deletions care/facility/api/serializers/camera_preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,26 @@ class Meta:
"updated_by",
)

def get_asset_bed_obj(self):
return self.instance.asset_bed if self.instance else self.context["asset_bed"]

Check warning on line 30 in care/facility/api/serializers/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/camera_preset.py#L30

Added line #L30 was not covered by tests

def validate_name(self, value):
presets_of_related_bed = (

Check warning on line 33 in care/facility/api/serializers/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/camera_preset.py#L33

Added line #L33 was not covered by tests
CameraPreset.objects.filter(
asset_bed__bed_id=self.get_asset_bed_obj().bed_id
)
.only("name")
.values_list("name")
)
if value in [x[0] for x in presets_of_related_bed]:
msg = "Name should be unique. Another preset related to this bed already uses the same name."
raise ValidationError(msg)
return value

Check warning on line 43 in care/facility/api/serializers/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/camera_preset.py#L41-L43

Added lines #L41 - L43 were not covered by tests

def validate(self, attrs):
validated_data = super().validate(attrs)

Check warning on line 46 in care/facility/api/serializers/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/camera_preset.py#L46

Added line #L46 was not covered by tests

asset_bed = (
self.instance.asset_bed if self.instance else self.context["asset_bed"]
)
asset_bed = self.get_asset_bed_obj()
position = validated_data.get(

Check warning on line 49 in care/facility/api/serializers/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/camera_preset.py#L48-L49

Added lines #L48 - L49 were not covered by tests
"position", self.instance and self.instance.position
)
Expand All @@ -42,16 +56,17 @@ def validate(self, attrs):
if not self.instance:
# one of position or boundary only must be present
if not (validated_data.get("position") or validated_data.get("boundary")):
raise ValidationError("Either position or boundary must be specified")
msg = "Either position or boundary must be specified"
raise ValidationError(msg)

Check warning on line 60 in care/facility/api/serializers/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/camera_preset.py#L59-L60

Added lines #L59 - L60 were not covered by tests

# single boundary preset for an asset_bed
if boundary and CameraPreset.objects.filter(asset_bed=asset_bed).exists():
raise ValidationError(
"Only one boundary preset can exist for an asset_bed"
)
msg = "Only one boundary preset can exist for an asset_bed"
raise ValidationError(msg)

Check warning on line 65 in care/facility/api/serializers/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/camera_preset.py#L64-L65

Added lines #L64 - L65 were not covered by tests
# one of position or boundary only must be present
if position and boundary:
raise ValidationError("Cannot have both position and a boundary.")
msg = "Cannot have both position and a boundary."
raise ValidationError(msg)

Check warning on line 69 in care/facility/api/serializers/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/camera_preset.py#L68-L69

Added lines #L68 - L69 were not covered by tests

return validated_data

Check warning on line 71 in care/facility/api/serializers/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/camera_preset.py#L71

Added line #L71 was not covered by tests

Expand Down
38 changes: 31 additions & 7 deletions care/facility/api/viewsets/camera_preset.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.shortcuts import get_object_or_404
from django_filters import rest_framework as filters
from dry_rest_permissions.generics import DRYPermissions
from rest_framework.mixins import ListModelMixin
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from rest_framework.viewsets import GenericViewSet, ModelViewSet

from care.facility.api.serializers.camera_preset import CameraPresetSerializer
from care.facility.models import AssetBed, CameraPreset
Expand All @@ -18,16 +18,29 @@ class CameraPresetFilter(filters.FilterSet):

def filter_preset_type(self, queryset, name, value):
if value is not None:
return queryset.filter(**{f"${name}__is_null": not value})
return queryset.filter(**{f"{name}__isnull": not value})
return queryset

Check warning on line 22 in care/facility/api/viewsets/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/camera_preset.py#L21-L22

Added lines #L21 - L22 were not covered by tests


class CameraPresetViewSet(ModelViewSet):
class AssetBedCameraPresetFilter(filters.FilterSet):
position = filters.BooleanFilter(method="filter_preset_type")
boundary = filters.BooleanFilter(method="filter_preset_type")

def filter_preset_type(self, queryset, name, value):
if value is not None:
return queryset.filter(**{f"{name}__isnull": not value})
return queryset

Check warning on line 32 in care/facility/api/viewsets/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/camera_preset.py#L31-L32

Added lines #L31 - L32 were not covered by tests


class AssetBedCameraPresetViewSet(ModelViewSet):
serializer_class = CameraPresetSerializer
queryset = CameraPreset.objects.all()
queryset = CameraPreset.objects.all().select_related(
"asset_bed", "created_by", "updated_by"
)
lookup_field = "external_id"
permission_classes = (IsAuthenticated, DRYPermissions)
permission_classes = (IsAuthenticated,)
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = CameraPresetFilter
filterset_class = AssetBedCameraPresetFilter

def get_asset_bed_obj(self):
return get_object_or_404(

Check warning on line 46 in care/facility/api/viewsets/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/camera_preset.py#L46

Added line #L46 was not covered by tests
Expand All @@ -42,3 +55,14 @@ def get_serializer_context(self):
context = super().get_serializer_context()
context["asset_bed"] = self.get_asset_bed_obj()
return context

Check warning on line 57 in care/facility/api/viewsets/camera_preset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/camera_preset.py#L55-L57

Added lines #L55 - L57 were not covered by tests


class CameraPresetViewSet(GenericViewSet, ListModelMixin):
serializer_class = CameraPresetSerializer
queryset = CameraPreset.objects.all().select_related(
"asset_bed", "created_by", "updated_by"
)
lookup_field = "external_id"
permission_classes = (IsAuthenticated,)
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = CameraPresetFilter
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("facility", "0459_remove_bed_unique_bed_name_per_location_and_more"),
("facility", "0465_merge_20240923_1045"),
]

def delete_asset_beds_without_asset_class(apps, schema_editor):
Expand Down Expand Up @@ -56,7 +56,7 @@ def backfill_camera_presets(apps, schema_editor):
position={
"x": position["x"],
"y": position["y"],
"z": position["zoom"],
"zoom": position["zoom"],
},
is_migrated=True,
)
Expand Down Expand Up @@ -113,8 +113,9 @@ def backfill_camera_presets(apps, schema_editor):
"properties": {
"x": {"type": "number"},
"y": {"type": "number"},
"z": {"type": "number"},
"zoom": {"type": "number"},
},
"required": ["x", "y", "zoom"],
"type": "object",
}
)
Expand All @@ -136,6 +137,7 @@ def backfill_camera_presets(apps, schema_editor):
"y0": {"type": "number"},
"y1": {"type": "number"},
},
"required": ["x0", "y0", "x1", "y1"],
"type": "object",
}
)
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 @@ -80,6 +80,10 @@ class Meta:
def __str__(self):
return f"{self.asset.name} - {self.bed.name}"

def delete(self, *args):
self.camera_presets.update(deleted=True)
return super().delete(*args)

Check warning on line 85 in care/facility/models/bed.py

View check run for this annotation

Codecov / codecov/patch

care/facility/models/bed.py#L84-L85

Added lines #L84 - L85 were not covered by tests


class ConsultationBed(BaseModel):
consultation = models.ForeignKey(
Expand Down
4 changes: 3 additions & 1 deletion care/facility/models/camera_preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"properties": {
"x": {"type": "number"},
"y": {"type": "number"},
"z": {"type": "number"},
"zoom": {"type": "number"},
},
"required": ["x", "y", "zoom"],
"additionalProperties": False,
}

Expand All @@ -23,6 +24,7 @@
"x1": {"type": "number"},
"y1": {"type": "number"},
},
"required": ["x0", "y0", "x1", "y1"],
"additionalProperties": False,
}

Expand Down
12 changes: 10 additions & 2 deletions config/api_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
ConsultationBedViewSet,
PatientAssetBedViewSet,
)
from care.facility.api.viewsets.camera_preset import CameraPresetViewSet
from care.facility.api.viewsets.camera_preset import (
AssetBedCameraPresetViewSet,
CameraPresetViewSet,
)
from care.facility.api.viewsets.consultation_diagnosis import (
ConsultationDiagnosisViewSet,
)
Expand Down Expand Up @@ -235,7 +238,11 @@
router.register("bed", BedViewSet, basename="bed")
router.register("assetbed", AssetBedViewSet, basename="asset-bed")
router.register("consultationbed", ConsultationBedViewSet, basename="consultation-bed")
router.register("camera_preset", CameraPresetViewSet, basename="camera-preset")
router.register("camera_presets", CameraPresetViewSet, basename="camera-preset")
assetbed_nested_router = NestedSimpleRouter(router, r"assetbed", lookup="assetbed")
assetbed_nested_router.register(
r"camera_presets", AssetBedCameraPresetViewSet, basename="assetbed-camera-presets"
)

router.register("patient/search", PatientSearchViewSet, basename="patient-search")
router.register("patient", PatientViewSet, basename="patient")
Expand Down Expand Up @@ -329,6 +336,7 @@
path("", include(facility_nested_router.urls)),
path("", include(facility_location_nested_router.urls)),
path("", include(asset_nested_router.urls)),
path("", include(assetbed_nested_router.urls)),
path("", include(patient_nested_router.urls)),
path("", include(patient_notes_nested_router.urls)),
path("", include(consultation_nested_router.urls)),
Expand Down

0 comments on commit aee281a

Please sign in to comment.