From 80cf4892932cc4e16566b9a5c2554a9d0de776c0 Mon Sep 17 00:00:00 2001 From: Ngure Nyaga Date: Wed, 24 Nov 2021 12:24:16 +0300 Subject: [PATCH] feat: usability improvements for client creation form --- config/settings/base.py | 2 ++ mycarehub/clients/forms.py | 2 +- mycarehub/clients/tests/test_views.py | 2 +- mycarehub/clients/views.py | 13 +++++-------- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index dfa67ac..d901cfb 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -307,6 +307,8 @@ "rest_framework_datatables.renderers.DatatablesRenderer", "rest_framework.renderers.BrowsableAPIRenderer", "rest_framework.renderers.AdminRenderer", + "rest_framework.renderers.HTMLFormRenderer", + "rest_framework.renderers.StaticHTMLRenderer", ), "DEFAULT_PARSER_CLASSES": ( "rest_framework.parsers.JSONParser", diff --git a/mycarehub/clients/forms.py b/mycarehub/clients/forms.py index 95a579c..946d70b 100644 --- a/mycarehub/clients/forms.py +++ b/mycarehub/clients/forms.py @@ -28,7 +28,7 @@ def validate_date_past(value): def get_facility_choices(): choices = [] for facility in Facility.objects.all(): - choices.append((facility.pk, facility.name)) + choices.append((facility.name, facility.name)) return choices diff --git a/mycarehub/clients/tests/test_views.py b/mycarehub/clients/tests/test_views.py index cba17e7..ed86294 100644 --- a/mycarehub/clients/tests/test_views.py +++ b/mycarehub/clients/tests/test_views.py @@ -255,7 +255,7 @@ def test_client_registration_view_valid(user_with_all_permissions, client): response = client.post( url, data={ - "facility": facility.pk, + "facility": facility.name, "client_type": "PMTCT", "name": fake.name(), "gender": "MALE", diff --git a/mycarehub/clients/views.py b/mycarehub/clients/views.py index f7edbee..6d48f10 100644 --- a/mycarehub/clients/views.py +++ b/mycarehub/clients/views.py @@ -57,14 +57,11 @@ class ClientFacilityViewSet(ModelViewSet): class ClientRegistrationView(APIView): queryset = Client.objects.all() # to enable model permissions + serializer_class = ClientRegistrationSerializer @transaction.atomic def post(self, request, format=None): - serializer_data = request.data - serializer_data["organisation"] = request.user.organisation - serializer_data["user"] = request.user - - serializer = ClientRegistrationSerializer(data=serializer_data) + serializer = self.serializer_class(data=request.data) if serializer.is_valid(): data = serializer.validated_data request_user = request.user @@ -113,9 +110,9 @@ def post(self, request, format=None): }, ) - # retrieve the facility - facility_id = data["facility"] - facility = Facility.objects.get(pk=facility_id) + # retrieve the facility by the unique name + facility_name = data["facility"] + facility = Facility.objects.get(name=facility_name) # create a client client, _ = Client.objects.get_or_create(