From 7a4a613e98e058c398d1f6be412e9ebf675b550a Mon Sep 17 00:00:00 2001 From: Paulina Kujawa Date: Wed, 3 Apr 2024 14:02:03 +0200 Subject: [PATCH] return user of the approval process as defined str representation instead of username --- backend/hct_mis_api/apps/payment/api/serializers.py | 6 ++++++ backend/hct_mis_api/apps/payment/models.py | 6 +++--- backend/tests/apps/payments/test_payment_plan_views.py | 6 +++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/hct_mis_api/apps/payment/api/serializers.py b/backend/hct_mis_api/apps/payment/api/serializers.py index c85c96a67c..a792a925b0 100644 --- a/backend/hct_mis_api/apps/payment/api/serializers.py +++ b/backend/hct_mis_api/apps/payment/api/serializers.py @@ -1,3 +1,5 @@ +from typing import Optional + from rest_framework import serializers from hct_mis_api.apps.account.api.fields import Base64ModelField @@ -21,6 +23,7 @@ class PaymentPlanSerializer(serializers.ModelSerializer): follow_ups = FollowUpPaymentPlanSerializer(many=True, read_only=True) program = serializers.CharField(source="program.name") program_id = Base64ModelField(model_name="Program") + last_approval_process_by = serializers.SerializerMethodField() class Meta: model = PaymentPlan @@ -45,6 +48,9 @@ class Meta: "last_approval_process_by", ) + def get_last_approval_process_by(self, obj: PaymentPlan) -> Optional[str]: + return str(obj.last_approval_process_by) if obj.last_approval_process_by else None + class PaymentPlanBulkActionSerializer(serializers.Serializer): ids = serializers.ListField(child=serializers.CharField()) diff --git a/backend/hct_mis_api/apps/payment/models.py b/backend/hct_mis_api/apps/payment/models.py index 05ece769b1..cd987e0ee4 100644 --- a/backend/hct_mis_api/apps/payment/models.py +++ b/backend/hct_mis_api/apps/payment/models.py @@ -1022,13 +1022,13 @@ def _get_last_approval_process_data(self) -> dict: if self.status == PaymentPlan.Status.IN_APPROVAL: return { "modified_date": approval_process.sent_for_approval_date, - "modified_by": approval_process.sent_for_approval_by.username, + "modified_by": approval_process.sent_for_approval_by, } if self.status == PaymentPlan.Status.IN_AUTHORIZATION: if approval := approval_process.approvals.filter(type=Approval.APPROVAL).order_by("created_at").last(): return { "modified_date": approval.created_at, - "modified_by": approval.created_by.username, + "modified_by": approval.created_by, } if self.status == PaymentPlan.Status.IN_REVIEW: if ( @@ -1038,7 +1038,7 @@ def _get_last_approval_process_data(self) -> dict: ): return { "modified_date": approval.created_at, - "modified_by": approval.created_by.username, + "modified_by": approval.created_by, } return { "modified_date": self.updated_at, diff --git a/backend/tests/apps/payments/test_payment_plan_views.py b/backend/tests/apps/payments/test_payment_plan_views.py index 04c816c590..38951b13c6 100644 --- a/backend/tests/apps/payments/test_payment_plan_views.py +++ b/backend/tests/apps/payments/test_payment_plan_views.py @@ -143,7 +143,7 @@ def test_list_payment_plans_approval_process_data( assert response_json[0]["last_approval_process_date"] == approval_process.sent_for_approval_date.strftime( "%Y-%m-%dT%H:%M:%SZ" ) - assert response_json[0]["last_approval_process_by"] == approval_process.sent_for_approval_by.username + assert response_json[0]["last_approval_process_by"] == str(approval_process.sent_for_approval_by) self.payment_plan1.status = PaymentPlan.Status.IN_AUTHORIZATION self.payment_plan1.save() @@ -154,7 +154,7 @@ def test_list_payment_plans_approval_process_data( assert response_json[0]["last_approval_process_date"] == approval_approval.created_at.strftime( "%Y-%m-%dT%H:%M:%S.%fZ" ) - assert response_json[0]["last_approval_process_by"] == approval_approval.created_by.username + assert response_json[0]["last_approval_process_by"] == str(approval_approval.created_by) self.payment_plan1.status = PaymentPlan.Status.IN_REVIEW self.payment_plan1.save() @@ -165,7 +165,7 @@ def test_list_payment_plans_approval_process_data( assert response_json[0]["last_approval_process_date"] == approval_authorization.created_at.strftime( "%Y-%m-%dT%H:%M:%S.%fZ" ) - assert response_json[0]["last_approval_process_by"] == approval_authorization.created_by.username + assert response_json[0]["last_approval_process_by"] == str(approval_authorization.created_by) def _bulk_approve_action_response(self) -> Any: ApprovalProcessFactory(payment_plan=self.payment_plan1)