P-106
This commit is contained in:
1 parent
7cfda0b1f2
commit
c1c3615da0
2 files changed
+31
-11
No files matched your search
+29
-9
@@ -62,7 +62,27 @@ class VoteDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
results = Result.objects.filter(answer__question__vote=kwargs['id'])
|
||||
if results:
|
||||
return Response({'status': 'error', 'message': 'voted'}, status=400)
|
||||
return self.retrieve(request, *args, **kwargs)
|
||||
|
||||
try:
|
||||
vote = Vote.objects.get(id=kwargs['id'])
|
||||
except Exception as e:
|
||||
return Response({'status': 'error', 'message': str(e)}, status=400)
|
||||
|
||||
answers_employee = set(Answer.objects.filter(question__vote=vote).filter(question__settings__choice="employee").values_list('name', flat=True))
|
||||
|
||||
if answers_employee:
|
||||
employees = DictEmployee.objects.filter(id__in=answers_employee)
|
||||
employee_dict = {employee['id']: employee for employee in DictEmployeeSerializer(employees, many=True).data}
|
||||
result_dict = {result['answer__id']: {'employee': employee_dict.get(int(result['answer__name']))} for result in results}
|
||||
|
||||
serialized_data = self.retrieve(request, *args, **kwargs).data
|
||||
|
||||
for question in serialized_data['questions']:
|
||||
for answer in question['answers']:
|
||||
if answer['name'] in answers_employee:
|
||||
answer['employee'] = employee_dict.get(int(answer['name']))
|
||||
|
||||
return Response(serialized_data)
|
||||
|
||||
class VoteCreate(APIView):
|
||||
def post(self, request):
|
||||
@@ -173,24 +193,24 @@ class GetResults(APIView):
|
||||
except Exception as e:
|
||||
return Response({'status': 'error', 'message': str(e)}, status=400)
|
||||
|
||||
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'))
|
||||
answers_employee = set(Answer.objects.filter(question__vote=vote).filter(question__settings__choice="employee").values_list('name', flat=True))
|
||||
results = Result.objects.filter(answer__question__vote=vote).values('answer__id', 'answer__name').annotate(count=Count('id'))
|
||||
|
||||
serialized_results = VoteSerializer(vote).data
|
||||
serialized_data = VoteSerializer(vote).data
|
||||
|
||||
if answers_employee:
|
||||
employees = DictEmployee.objects.filter(id__in=answers_employee)
|
||||
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}
|
||||
result_dict = {result['answer__id']: {'count': result['count'], 'employee': employee_dict.get(int(result['answer__name']))} for result in results}
|
||||
|
||||
for question in serialized_results['questions']:
|
||||
for question in serialized_data['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'])
|
||||
if answer['name'] in answers_employee:
|
||||
answer['employee'] = employee_dict.get(int(answer['name']))
|
||||
|
||||
return Response(serialized_results)
|
||||
return Response(serialized_data)
|
||||
|
||||
from rest_framework.views import exception_handler
|
||||
class VoteResultCreate(APIView):
|
||||
|
||||
Reference in new issue
Block a user