Merge branch 'сja' into Vit

This commit is contained in:
Nukeap committed 2025-01-20 08:06:19 +03:00
commit 051b1d6521
1 file changed
+24 -13
+24 -13
View File
@@ -12,6 +12,8 @@ import json
from collections import defaultdict
import os
from django.conf import settings
from django.urls import reverse
import html
class GetVotes(APIView):
@@ -138,7 +140,8 @@ class VoteCreate(APIView):
end_date_str = request.data["endDate"]
formatted_date = datetime.strptime(end_date_str[:-1], "%Y-%m-%dT%H:%M:%S.%f").strftime("%d.%m.%Y %H:%M")
# formatted_date = datetime.fromisoformat(request.data["endDate"]).strftime("%d.%m.%Y %H:%M")
text = f'Пройдите голосование "{vote_name}" до {formatted_date}'
vote_url = make_clickable(vote_name, f"/vote/voting/{vote.id}")
text = f'Пройдите голосование "{vote_url}" до {formatted_date}'
news = News(
title=title,
@@ -149,7 +152,12 @@ class VoteCreate(APIView):
vote = vote
)
news.save()
def make_clickable(text, url):
text = html.escape(text)
anchor = f'<a href="{html.escape(url)}">{text}</a>'
return anchor
class GetDictEmployee(APIView):
def get(self, request):
employees = DictEmployee.objects.filter(deleted=0)
@@ -159,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