Merge remote-tracking branch 'origin/develop' into dev-denzel

This commit is contained in:
Denzel committed 2025-01-20 08:23:56 +03:00
commit fa06b82232
1 file changed
+44 -15
+44 -15
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):
@@ -88,20 +90,38 @@ class VoteCreate(APIView):
)
vote.save()
questions_files = request.FILES.getlist('questionsfiles')
answers_files = request.FILES.getlist('answersfiles')
questions = json.loads(request.data['questions'])
for count, question_data in enumerate(questions, start=1):
question_image = None
if 'image' in question_data:
for file in questions_files:
if file.name == question_data['image']['name']:
question_image = file
break
question = Question(
question=question_data['text'],
vote=vote,
file=question_data['image'],
file=question_image,
numb=count,
settings=question_data.get('settings', {}),
)
question.save()
for answer in question_data['answers']:
answer_image = None
if 'image' in answer:
for file in answers_files:
if file.name == answer['image']['name']:
answer_image = file
break
Answer(
answer=answer['text'],
file=answer['image'],
file=answer_image,
question=question
).save()
@@ -120,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,
@@ -131,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)
@@ -141,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