P-106
This commit is contained in:
1 parent
694e00c2f5
commit
5f79c72946
4 files changed
+29
-14
No files matched your search
@@ -42,4 +42,4 @@ class Result(models.Model):
|
|||||||
comment = models.TextField(null=True, blank=True)
|
comment = models.TextField(null=True, blank=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'Result by {self.user.username} for {self.answer.answer}'
|
return self.answer.answer
|
||||||
@@ -44,11 +44,12 @@ class ResultSerializer(serializers.ModelSerializer):
|
|||||||
model = Result
|
model = Result
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
class VoteResultSerializer(serializers.Serializer):
|
|
||||||
answer = serializers.CharField(source='answer__answer')
|
|
||||||
count = serializers.IntegerField()
|
|
||||||
|
|
||||||
class DictEmployeeSerializer(serializers.ModelSerializer):
|
class DictEmployeeSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = DictEmployee
|
model = DictEmployee
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
class VoteResultSerializer(serializers.Serializer):
|
||||||
|
answer = serializers.CharField(source='answer__answer')
|
||||||
|
count = serializers.IntegerField()
|
||||||
|
employee = DictEmployeeSerializer(required=False)
|
||||||
@@ -8,4 +8,5 @@ urlpatterns = [
|
|||||||
path('votedetail/<int:id>/', VoteDetail.as_view(),name='VoteDetail'),
|
path('votedetail/<int:id>/', VoteDetail.as_view(),name='VoteDetail'),
|
||||||
path('votecreate/', VoteCreate.as_view(),name='VoteCreate'),
|
path('votecreate/', VoteCreate.as_view(),name='VoteCreate'),
|
||||||
path('getdictemployee/', GetDictEmployee.as_view(),name='GetDictEmployee'),
|
path('getdictemployee/', GetDictEmployee.as_view(),name='GetDictEmployee'),
|
||||||
|
path('getresults', GetResults.as_view(),name='GetResults'),
|
||||||
]
|
]
|
||||||
+21
-8
@@ -48,13 +48,6 @@ class GetVotes(APIView):
|
|||||||
def has_participated(self, employee, vote):
|
def has_participated(self, employee, vote):
|
||||||
return Result.objects.filter(user=employee, answer__question__vote=vote).exists()
|
return Result.objects.filter(user=employee, answer__question__vote=vote).exists()
|
||||||
|
|
||||||
def get_vote_results(self, request):
|
|
||||||
vote_id = request.data.get('id')
|
|
||||||
vote = Vote.objects.get(id=vote_id)
|
|
||||||
results = Result.objects.filter(answer__question__vote=vote).values('answer__answer').annotate(count=Count('id'))
|
|
||||||
serialized_results = VoteResultSerializer(results, many=True).data
|
|
||||||
return Response(serialized_results)
|
|
||||||
|
|
||||||
class VoteDetail(generics.RetrieveUpdateDestroyAPIView):
|
class VoteDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||||
queryset = Vote.objects.all()
|
queryset = Vote.objects.all()
|
||||||
serializer_class = VoteSerializer
|
serializer_class = VoteSerializer
|
||||||
@@ -134,4 +127,24 @@ class GetDictEmployee(APIView):
|
|||||||
def get(self, request):
|
def get(self, request):
|
||||||
employees = DictEmployee.objects.filter(deleted=0)
|
employees = DictEmployee.objects.filter(deleted=0)
|
||||||
employees_serializer = DictEmployeeSerializer(employees, many=True).data
|
employees_serializer = DictEmployeeSerializer(employees, many=True).data
|
||||||
return Response(employees_serializer)
|
return Response(employees_serializer)
|
||||||
|
|
||||||
|
class GetResults(APIView):
|
||||||
|
|
||||||
|
def get(self, request):
|
||||||
|
vote_id = request.data.get('id')
|
||||||
|
vote = Vote.objects.get(id=vote_id)
|
||||||
|
|
||||||
|
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'))
|
||||||
|
employees = DictEmployee.objects.filter(id__in=answers_employee)
|
||||||
|
employee_dict = {employee.id: employee for employee in employees}
|
||||||
|
|
||||||
|
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)
|
||||||
Reference in new issue
Block a user