Merge branch 'develop' into Vit

This commit is contained in:
Nukeap committed 2025-01-20 10:15:35 +03:00
commit ee4603a7c1
2 files changed
+31 -11

No files matched your search

+2 -2
View File
@@ -19,7 +19,7 @@ class Vote(models.Model):
verbose_name_plural = "Голосования"
class Question(models.Model):
question = models.CharField(max_length=255)
name = models.CharField(max_length=255)
vote = models.ForeignKey(Vote, related_name='questions', on_delete=models.CASCADE)
file = models.ImageField(upload_to='question_images/', null=True, blank=True)
numb = models.PositiveIntegerField()
@@ -33,7 +33,7 @@ class Question(models.Model):
verbose_name_plural = "Вопросы"
class Answer(models.Model):
answer = models.CharField(max_length=255)
name = models.CharField(max_length=255)
file = models.FileField(upload_to='answer_files/', null=True, blank=True)
question = models.ForeignKey(Question, related_name='answers', on_delete=models.CASCADE)
+29 -9
View File
@@ -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):