Skip to content

Commit

Permalink
Modified bedCreate API View to support bulk creating beds (#1275)
Browse files Browse the repository at this point in the history
* Modified BedCreate View to support bulk creating beds

* Minor fix

* Added test for single and multiple bed creation

* Added missing commits, modified tests

* Removed hardcoded facility from tests

* Fixed tests

* minor fix

* Modified serializer to allow only 100 beds at once

* make requested changes

Signed-off-by: Aakash Singh <[email protected]>

* add test case for more than allowed beds

Signed-off-by: Aakash Singh <[email protected]>

* remove unnecessary use of transaction test case

Signed-off-by: Aakash Singh <[email protected]>

---------

Signed-off-by: Aakash Singh <[email protected]>
Co-authored-by: Aakash Singh <[email protected]>
Co-authored-by: Vignesh Hari <[email protected]>
Co-authored-by: Rithvik Nishad <[email protected]>
  • Loading branch information
4 people authored Aug 2, 2023
1 parent 7dfbef0 commit fdfb798
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
8 changes: 8 additions & 0 deletions care/facility/api/serializers/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework.exceptions import ValidationError
from rest_framework.serializers import (
BooleanField,
IntegerField,
ModelSerializer,
SerializerMethodField,
UUIDField,
Expand Down Expand Up @@ -31,6 +32,13 @@ class BedSerializer(ModelSerializer):
location = UUIDField(write_only=True, required=True)
facility = UUIDField(write_only=True, required=True)

number_of_beds = IntegerField(required=False, default=1, write_only=True)

def validate_number_of_beds(self, value):
if value > 100:
raise ValidationError("Cannot create more than 100 beds at once.")
return value

class Meta:
model = Bed
exclude = ("deleted", "external_id", "assets")
Expand Down
26 changes: 26 additions & 0 deletions care/facility/api/viewsets/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django_filters import rest_framework as filters
from drf_spectacular.utils import extend_schema, extend_schema_view
from rest_framework import filters as drf_filters
from rest_framework import status
from rest_framework.exceptions import PermissionDenied
from rest_framework.exceptions import ValidationError as DRFValidationError
from rest_framework.fields import get_error_detail
Expand All @@ -14,6 +15,7 @@
UpdateModelMixin,
)
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from care.facility.api.serializers.bed import (
Expand Down Expand Up @@ -57,6 +59,30 @@ class BedViewSet(
search_fields = ["name"]
filterset_class = BedFilter

def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
number_of_beds = serializer.validated_data.pop("number_of_beds", 1)
# Bulk creating n number of beds
if number_of_beds > 1:
data = serializer.validated_data.copy()
data.pop("name")
beds = [
Bed(
**data,
name=f"{serializer.validated_data['name']} {i+1}",
)
for i in range(number_of_beds)
]
Bed.objects.bulk_create(beds)
return Response(status=status.HTTP_201_CREATED)

self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(
serializer.data, status=status.HTTP_201_CREATED, headers=headers
)

def get_queryset(self):
user = self.request.user
queryset = self.queryset
Expand Down
73 changes: 73 additions & 0 deletions care/facility/tests/test_bed_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from rest_framework import status

from care.facility.models import AssetLocation, Bed
from care.utils.tests.test_base import TestBase


class SingleBedTest(TestBase):
def setUp(self) -> None:
super().setUp()
self.asset_location: AssetLocation = AssetLocation.objects.create(
name="asset location", location_type=1, facility=self.facility
)

def tearDown(self) -> None:
Bed._default_manager.filter(facility=self.facility).delete()
AssetLocation._default_manager.filter(id=self.asset_location.id).delete()

def test_create(self):
sample_data = {
"bed_type": "REGULAR",
"description": "Testing creation of beds.",
"facility": self.facility.external_id,
"location": self.asset_location.external_id,
"name": "Test Bed",
"number_of_beds": 1,
}
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertIs(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(
Bed.objects.filter(facility=self.facility).count(),
sample_data["number_of_beds"],
)


class MultipleBedTest(TestBase):
def setUp(self) -> None:
super().setUp()
self.asset_location: AssetLocation = AssetLocation.objects.create(
name="asset location", location_type=1, facility=self.facility
)

def tearDown(self) -> None:
Bed._default_manager.filter(facility=self.facility).delete()
AssetLocation._default_manager.filter(id=self.asset_location.id).delete()

def test_create(self):
sample_data = {
"bed_type": "REGULAR",
"description": "Testing creation of beds.",
"facility": self.facility.external_id,
"location": self.asset_location.external_id,
"name": "Test Bed",
"number_of_beds": 5,
}
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertIs(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(
Bed.objects.filter(facility=self.facility).count(),
sample_data["number_of_beds"],
)

def test_create_with_more_than_allowed_value(self):
sample_data = {
"bed_type": "REGULAR",
"description": "Testing creation of beds.",
"facility": self.facility.external_id,
"location": self.asset_location.external_id,
"name": "Test 2 Bed",
"number_of_beds": 101,
}
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertIs(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Bed.objects.filter(facility=self.facility).count(), 0)

0 comments on commit fdfb798

Please sign in to comment.