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

Modified bedCreate API View to support bulk creating beds #1275

Merged
merged 34 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c30098a
Merge pull request #1 from coronasafe/master
siddnikh May 28, 2022
cef2530
Merge pull request #2 from coronasafe/master
siddnikh Jun 5, 2022
6c2b01e
Merge pull request #3 from coronasafe/master
siddnikh Jun 23, 2022
5e3cca7
Merge pull request #4 from coronasafe/master
siddnikh Jul 26, 2022
9762009
Merge pull request #5 from coronasafe/master
siddnikh Aug 27, 2022
d95d7c2
Merge pull request #6 from coronasafe/master
siddnikh Sep 27, 2022
bb33895
Merge branch 'master' of https://github.com/siddnikh/care
siddnikh Apr 11, 2023
8a4ffbf
Merge branch 'master' of https://github.com/siddnikh/care
siddnikh Apr 26, 2023
4df90fb
Modified BedCreate View to support bulk creating beds
siddnikh Apr 27, 2023
9676660
Minor fix
siddnikh Apr 27, 2023
642a6fb
Merge branch 'master' into issue#1274
siddnikh Jun 20, 2023
75df976
Merge branch 'master' into issue#1274
siddnikh Jun 20, 2023
461cd6f
Merge branch 'master' into issue#1274
siddnikh Jun 29, 2023
9b320e6
Added test for single and multiple bed creation
siddnikh Jun 29, 2023
daf62e5
Merge branch 'issue#1274' of https://github.com/siddnikh/care into is…
siddnikh Jun 29, 2023
b46935f
Merge branch 'master' into issue#1274
sainak Jul 1, 2023
25c9113
Added missing commits, modified tests
siddnikh Jul 4, 2023
15a5496
Merge branch 'issue#1274' of https://github.com/siddnikh/care into is…
siddnikh Jul 4, 2023
15f1709
Merge branch 'master' into issue#1274
siddnikh Jul 4, 2023
78a442d
Removed hardcoded facility from tests
siddnikh Jul 4, 2023
f561651
Merge branch 'issue#1274' of https://github.com/siddnikh/care into is…
siddnikh Jul 4, 2023
602203e
Fixed tests
siddnikh Jul 7, 2023
067a9b2
Merge branch 'master' into issue#1274
siddnikh Jul 7, 2023
819e634
minor fix
siddnikh Jul 8, 2023
1c1d5cb
Merge branch 'issue#1274' of https://github.com/siddnikh/care into is…
siddnikh Jul 8, 2023
461b946
Modified serializer to allow only 100 beds at once
siddnikh Jul 11, 2023
b286a4c
Merge branch 'master' into issue#1274
siddnikh Jul 11, 2023
c0f42ca
Merge branch 'master' into issue#1274
sainak Jul 20, 2023
c65a1cb
Merge branch 'master' into issue#1274
vigneshhari Jul 20, 2023
bef9a3a
make requested changes
sainak Jul 20, 2023
1cee771
Merge branch 'master' into issue#1274
rithviknishad Jul 24, 2023
f589193
add test case for more than allowed beds
sainak Jul 26, 2023
a066790
Merge branch 'master' into issue#1274
sainak Jul 26, 2023
38d9ccf
remove unnecessary use of transaction test case
sainak Jul 26, 2023
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
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,8 @@ 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)

class Meta:
model = Bed
exclude = ("deleted", "external_id", "assets")
Expand All @@ -57,6 +60,11 @@ def validate(self, attrs):
raise ValidationError(
{"location": "Field is Required", "facility": "Field is Required"}
)

if attrs["number_of_beds"] > 100:
raise ValidationError(
{"number_of_beds": "Cannot create more than 100 beds at once."}
)
return super().validate(attrs)


Expand Down
24 changes: 24 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,28 @@ class BedViewSet(
search_fields = ["name"]
filterset_class = BedFilter

def create(self, request, *args, **kwargs):
sainak marked this conversation as resolved.
Show resolved Hide resolved
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
validated_data = serializer.validated_data
number_of_beds = validated_data.pop("number_of_beds", 1)
# Bulk creating n number of beds
if number_of_beds > 1 and number_of_beds <= 100:
siddnikh marked this conversation as resolved.
Show resolved Hide resolved
objs = []
for i in range(1, number_of_beds + 1):
siddnikh marked this conversation as resolved.
Show resolved Hide resolved
temp_data = dict(validated_data.copy())
temp_data["name"] = temp_data["name"] + f" - {i}"
objs.append(Bed(**temp_data))

res = Bed.objects.bulk_create(objs=objs, batch_size=number_of_beds)
sainak marked this conversation as resolved.
Show resolved Hide resolved
return Response(res, status=status.HTTP_201_CREATED)
sainak marked this conversation as resolved.
Show resolved Hide resolved

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
61 changes: 61 additions & 0 deletions care/facility/tests/test_bed_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from rest_framework import status
from rest_framework.test import APIRequestFactory, APITestCase

from care.facility.api.viewsets.bed import BedViewSet
from care.facility.models import AssetLocation
from care.facility.tests.mixins import TestClassMixin
from care.utils.tests.test_base import TestBase


class SingleBedTest(TestBase, TestClassMixin, APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
state = self.create_state()
district = self.create_district(state=state)
self.user = self.create_user(district=district, username="test user")
self.facility = self.create_facility(district=district, user=self.user)
self.asset_location = AssetLocation.objects.create(
name="asset location", location_type=1, facility=self.facility
)

def test_create(self):
user = self.user
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.new_request(
("/api/v1/bed/", sample_data, "json"), {"post": "create"}, BedViewSet, user
)
self.assertIs(response.status_code, status.HTTP_201_CREATED)
sainak marked this conversation as resolved.
Show resolved Hide resolved


class MultipleBedTest(TestBase, TestClassMixin, APITestCase):
sainak marked this conversation as resolved.
Show resolved Hide resolved
def setUp(self):
self.factory = APIRequestFactory()
state = self.create_state()
district = self.create_district(state=state)
self.user = self.create_user(district=district, username="test user")
self.facility = self.create_facility(district=district, user=self.user)
self.asset_location = AssetLocation.objects.create(
name="asset location", location_type=1, facility=self.facility
)

def test_create(self):
user = self.user
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.new_request(
("/api/v1/bed/", sample_data, "json"), {"post": "create"}, BedViewSet, user
)
self.assertIs(response.status_code, status.HTTP_201_CREATED)
sainak marked this conversation as resolved.
Show resolved Hide resolved
Loading