P-106
This commit is contained in:
1 parent
74e2f1ec47
commit
7cfda0b1f2
1 file changed
+14
-11
+14
-11
@@ -167,26 +167,29 @@ class GetDictEmployee(APIView):
|
||||
class GetResults(APIView):
|
||||
|
||||
def get(self, request):
|
||||
vote_id = request.data.get('id')
|
||||
vote_id = request.GET.get('id')
|
||||
try:
|
||||
vote = Vote.objects.get(id=vote_id)
|
||||
except Exception as e:
|
||||
return Response({'status': 'error', 'message': str(e)}, status=400)
|
||||
|
||||
answers_employee = Answer.objects.filter(question__vote=vote).filter(question__settings__choice="employee").values_list('answer', flat=True)
|
||||
results = Result.objects.filter(answer__question__vote=vote).annotate(count=Count('id')).values('answer__answer').annotate(count=Count('id'))
|
||||
answers_employee = set(Answer.objects.filter(question__vote=vote).filter(question__settings__choice="employee").values_list('id', flat=True))
|
||||
results = Result.objects.filter(answer__question__vote=vote).values('answer__id').annotate(count=Count('id'))
|
||||
|
||||
serialized_results = VoteSerializer(vote).data
|
||||
|
||||
if answers_employee:
|
||||
employees = DictEmployee.objects.filter(id__in=answers_employee)
|
||||
employee_dict = {employee.id: employee for employee in employees}
|
||||
employee_dict = {employee['id']: employee for employee in DictEmployeeSerializer(employees, many=True).data}
|
||||
result_dict = {result['answer__id']: {'count': result['count'], 'employee': employee_dict.get(int(result['answer__id']))} for result in results}
|
||||
|
||||
for question in serialized_results['questions']:
|
||||
for answer in question['answers']:
|
||||
answer_data = result_dict.get(answer['id'], {})
|
||||
answer['count'] = answer_data.get('count', 0)
|
||||
if answer['id'] in answers_employee:
|
||||
answer['employee'] = employee_dict.get(answer['id'])
|
||||
|
||||
for result in results:
|
||||
if result['answer__answer'] in answers_employee:
|
||||
employee_data = employee_dict.get(int(result['answer__answer']))
|
||||
result['employee'] = employee_data
|
||||
|
||||
serialized_results = VoteResultSerializer(results, many=True).data
|
||||
|
||||
return Response(serialized_results)
|
||||
|
||||
from rest_framework.views import exception_handler
|
||||
|
||||
Reference in new issue
Block a user