Skip to content

Commit

Permalink
[#1162] Add new field: last_value in ListPendingFormData serializer
Browse files Browse the repository at this point in the history
get the last value from data where UUID equal with pending data and order by created DESC
  • Loading branch information
ifirmawan committed Feb 12, 2024
1 parent 0b5a9f2 commit 6db9658
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 12 additions & 1 deletion backend/api/v1/v1_data/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ def __init__(self, **kwargs):
class ListPendingDataAnswerSerializer(serializers.ModelSerializer):
history = serializers.SerializerMethodField()
value = serializers.SerializerMethodField()
last_value = serializers.SerializerMethodField()

@extend_schema_field(AnswerHistorySerializer(many=True))
def get_history(self, instance):
Expand All @@ -457,9 +458,19 @@ def get_history(self, instance):
def get_value(self, instance: Answers):
return get_answer_value(instance)

@extend_schema_field(OpenApiTypes.ANY)
def get_last_value(self, instance: Answers):
if self.context['last_data']:
answer = self.context['last_data'].data_answer.filter(
question=instance.question
).first()
if answer:
return get_answer_value(answer=answer)
return None

class Meta:
model = PendingAnswers
fields = ["history", "question", "value"]
fields = ["history", "question", "value", "last_value"]


class PendingBatchDataFilterSerializer(serializers.Serializer):
Expand Down
9 changes: 7 additions & 2 deletions backend/api/v1/v1_data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,16 @@ class PendingDataDetailDeleteView(APIView):
summary='To get list of answers for pending data')
def get(self, request, pending_data_id, version):
data = get_object_or_404(PendingFormData, pk=pending_data_id)
last_data = FormData.objects.filter(uuid=data.uuid)\
.order_by('-created').first()
return Response(
ListPendingDataAnswerSerializer(
context={'last_data': last_data},
instance=data.pending_data_answer.all(),
many=True).data,
status=status.HTTP_200_OK)
many=True
).data,
status=status.HTTP_200_OK
)

@extend_schema(
responses={
Expand Down

0 comments on commit 6db9658

Please sign in to comment.