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

Feature/1162 modify pending data point detail to have the last response #1165

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
12 changes: 8 additions & 4 deletions backend/api/v1/v1_data/tests/test_update_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,13 @@ def test_update_datapoint_by_data_entry_role(self):
results = [{
'history': None,
'question': 101,
'value': 'User Wayan'
'value': 'User Wayan',
'last_value': None,
}, {
'history': None,
'question': 102,
'value': ['Male']
'value': ['Male'],
'last_value': None,
}]
self.assertEqual(data, results)
# test get form data with pending data object inside
Expand Down Expand Up @@ -517,11 +519,13 @@ def test_update_datapoint_by_county_admin_with_national_form(self):
results = [{
'history': None,
'question': 201,
'value': 'Made County Admin'
'value': 'Made County Admin',
'last_value': None,
}, {
'history': None,
'question': 202,
'value': ['Male']
'value': ['Male'],
'last_value': None,
}]
self.assertEqual(data, results)
# test get form data with pending data object inside
Expand Down
29 changes: 26 additions & 3 deletions backend/api/v1/v1_data/tests/tests_pending_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

from api.v1.v1_data.constants import DataApprovalStatus
from api.v1.v1_data.models import PendingFormData, PendingDataApproval, \
PendingDataBatch
PendingDataBatch, FormData
from api.v1.v1_forms.constants import FormTypes, QuestionTypes
from api.v1.v1_forms.models import Forms, Questions
from api.v1.v1_profile.constants import UserRoleTypes
from api.v1.v1_profile.management.commands.administration_seeder import (
MAX_LEVEL_IN_SOURCE_FILE)
from api.v1.v1_profile.models import Administration
from api.v1.v1_users.models import SystemUser
from utils.functions import get_answer_value


@override_settings(USE_TZ=False)
Expand Down Expand Up @@ -56,8 +57,10 @@ def tests_pending_data(self):
content_type='application/json',
**header)
self.assertEqual(200, response.status_code)
self.assertEqual(['history', 'question', 'value'],
list(response.json()[0]))
self.assertEqual(
['history', 'question', 'value', 'last_value'],
list(response.json()[0])
)
response = self.client.get(
'/api/v1/form-pending-data-batch/{}'.format(data[0]['id']),
content_type='application/json',
Expand Down Expand Up @@ -114,6 +117,26 @@ def tests_pending_data(self):
'created', 'updated', 'status', 'approvers'
])

def tests_pending_data_with_last_reponse(self):
call_command("fake_data_seeder", "-r", 1, '-t', True)
call_command("fake_data_monitoring_seeder", "-r", 1, '-t', True)
pending_data = PendingFormData.objects.first()
t = RefreshToken.for_user(pending_data.created_by)
header = {'HTTP_AUTHORIZATION': f'Bearer {t.access_token}'}
response = self.client.get(
'/api/v1/pending-data/{0}'.format(pending_data.id),
content_type='application/json',
**header
)
data = FormData.objects.filter(
uuid=pending_data.uuid
).order_by('-created').first()
first_res = response.json()[0]
question_id = first_res['question']
answer = data.data_answer.filter(question_id=question_id).first()
self.assertEqual(response.status_code, 200)
self.assertEqual(first_res['last_value'], get_answer_value(answer))

def test_pending_batch_list(self):
call_command('fake_pending_data_seeder', '-r', 5, '-t', True, '-b', 5)

Expand Down
12 changes: 8 additions & 4 deletions backend/api/v1/v1_data/tests/tests_update_pending_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,20 @@ def test_update_pending_data(self):
# update pending data
payload = [{
"question": 101,
"value": "Jane Doe"
"value": "Jane Doe",
'last_value': None,
}, {
"question": 102,
"value": ["Female"]
"value": ["Female"],
'last_value': None,
}, {
"question": 104,
"value": 4
"value": 4,
'last_value': None,
}, {
"question": 109,
"value": 5.5
"value": 5.5,
'last_value': None,
}]
data = self.client.put(
'/api/v1/form-pending-data/{0}?pending_data_id={1}'
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