Merge branch 'сja' into develop
This commit is contained in:
commit
d524f5883b
2 files changed
+94
-44
No files matched your search
@@ -144,7 +144,7 @@ class TestResultCreate(generics.ListCreateAPIView):
|
||||
serializer.save(attempt=test_attempt)
|
||||
self.update_testing(user, test_info)
|
||||
|
||||
return Response(test_info['is_passed'])
|
||||
return Response(data={'score': test_info['score'], 'answered_questions_total': test_info['answered_questions_total']})
|
||||
|
||||
def create_test_attempt(self, user, test_info):
|
||||
test = test_info['test']
|
||||
@@ -163,6 +163,7 @@ class TestResultCreate(generics.ListCreateAPIView):
|
||||
answer_ids = [item['answer'] for item in request.data]
|
||||
test_answers = TestAnswer.objects.filter(id__in=answer_ids)
|
||||
test = test_answers[0].question.test
|
||||
answered_questions_total = test_answers.values('question').distinct().count()
|
||||
|
||||
correct_questions = sum(
|
||||
TestAnswer.objects.filter(
|
||||
@@ -179,7 +180,8 @@ class TestResultCreate(generics.ListCreateAPIView):
|
||||
result = {
|
||||
"is_passed": correct_questions >= pass_perc,
|
||||
"score": correct_questions,
|
||||
"test": test
|
||||
"test": test,
|
||||
"answered_questions_total": answered_questions_total,
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
+90
-42
@@ -25,16 +25,8 @@ class GetVotes(APIView):
|
||||
status = request.query_params.get('status')
|
||||
votes = self.get_votes_by_status(status, now)
|
||||
serialized_votes = VotesSerializer(votes, many=True).data
|
||||
questions = Question.objects.filter(vote__in=votes)
|
||||
|
||||
questions_by_vote_id = defaultdict(list)
|
||||
for question in questions:
|
||||
questions_by_vote_id[question.vote.id].append(question)
|
||||
|
||||
for vote in serialized_votes:
|
||||
vote['is_passed'] = self.has_participated(user, vote['id'])
|
||||
related_questions = questions_by_vote_id[vote['id']]
|
||||
vote['files'] = [question.file.url for question in related_questions if question.file]
|
||||
questions_by_vote_id = self.get_questions_by_vote_id(votes)
|
||||
self.enrich_votes(serialized_votes, questions_by_vote_id, user)
|
||||
|
||||
return Response(serialized_votes)
|
||||
|
||||
@@ -50,6 +42,20 @@ class GetVotes(APIView):
|
||||
|
||||
return status_filters.get(status, Vote.objects.none()).order_by('dateend')
|
||||
|
||||
def get_questions_by_vote_id(self, votes):
|
||||
questions = Question.objects.filter(vote__in=votes)
|
||||
|
||||
questions_by_vote_id = defaultdict(list)
|
||||
for question in questions:
|
||||
questions_by_vote_id[question.vote.id].append(question)
|
||||
return questions_by_vote_id
|
||||
|
||||
def enrich_votes(self, serialized_votes, questions_by_vote_id, user):
|
||||
for vote in serialized_votes:
|
||||
vote['is_passed'] = self.has_participated(user, vote['id'])
|
||||
related_questions = questions_by_vote_id[vote['id']]
|
||||
vote['files'] = [question.file.url for question in related_questions if question.file]
|
||||
|
||||
def has_participated(self, employee, vote):
|
||||
return Result.objects.filter(user=employee, answer__question__vote=vote).exists()
|
||||
|
||||
@@ -60,27 +66,49 @@ class VoteDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
lookup_field = 'id'
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
results = Result.objects.filter(answer__question__vote=kwargs['id'], user=request.user)
|
||||
if results:
|
||||
|
||||
# if self.check_user_vote(kwargs['id'], User.objects.get(username='9966-00-125')):
|
||||
if self.check_user_vote(kwargs['id'], request.user):
|
||||
return Response({'status': 'error', 'message': 'voted'}, status=400)
|
||||
|
||||
vote = self.get_vote(kwargs['id'])
|
||||
if isinstance(vote, Response):
|
||||
return vote
|
||||
|
||||
answers_employee = self.get_employee_answers(vote)
|
||||
answers_names = [item['name'] for item in answers_employee]
|
||||
|
||||
employee_dict = self.serialize_employees(answers_names)
|
||||
|
||||
serialized_data = self.retrieve(request, *args, **kwargs).data
|
||||
self.add_employee_info(serialized_data, answers_employee, answers_names, employee_dict)
|
||||
|
||||
return Response(serialized_data)
|
||||
|
||||
def check_user_vote(self, vote_id, user):
|
||||
results = Result.objects.filter(answer__question__vote=vote_id, user=user)
|
||||
return results.exists()
|
||||
|
||||
def get_vote(self, vote_id):
|
||||
try:
|
||||
vote = Vote.objects.get(id=kwargs['id'])
|
||||
vote = Vote.objects.get(id=vote_id)
|
||||
if vote.dateend < datetime.now(timezone.utc):
|
||||
return Response({'status': 'error', 'message': 'Voting is over'}, status=400)
|
||||
return vote
|
||||
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('name', 'question')
|
||||
|
||||
answers_names = [item['name'] for item in answers_employee]
|
||||
question_ids = [item['question'] for item in answers_employee]
|
||||
def get_employee_answers(self, vote):
|
||||
return Answer.objects.filter(question__vote=vote).filter(question__settings__choice="employee").values('name', 'question')
|
||||
|
||||
def serialize_employees(self, answers_names):
|
||||
|
||||
if answers_employee:
|
||||
employees_serializer = get_employees_serializer(answers_names)
|
||||
employee_dict = {employee['id']: employee for employee in employees_serializer}
|
||||
employees_serializer = get_employees_serializer(answers_names)
|
||||
return {employee['id']: employee for employee in employees_serializer}
|
||||
|
||||
def add_employee_info(self, serialized_data, answers_employee, answers_names, employee_dict):
|
||||
|
||||
serialized_data = self.retrieve(request, *args, **kwargs).data
|
||||
question_ids = [item['question'] for item in answers_employee]
|
||||
|
||||
for question in serialized_data['questions']:
|
||||
for answer in question['answers']:
|
||||
@@ -88,8 +116,6 @@ class VoteDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
if answer['name'] in answers_names:
|
||||
answer['employee'] = employee_dict.get(int(answer['name']))
|
||||
|
||||
return Response(serialized_data)
|
||||
|
||||
class VoteCreate(APIView):
|
||||
def post(self, request):
|
||||
try:
|
||||
@@ -166,45 +192,67 @@ class GetResults(APIView):
|
||||
|
||||
def get(self, request):
|
||||
vote_id = request.GET.get('id')
|
||||
vote = self.get_vote(vote_id)
|
||||
if isinstance(vote, Response):
|
||||
return vote
|
||||
|
||||
serialized_data = VoteSerializer(vote).data
|
||||
|
||||
answers_employee = self.get_employee_answers(vote)
|
||||
answers_names = [item['name'] for item in answers_employee]
|
||||
employee_dict = self.create_employee_dict(answers_names)
|
||||
result_dict = self.get_answer_counts(vote)
|
||||
|
||||
self.add_employee_info(serialized_data, answers_employee, answers_names, employee_dict, result_dict)
|
||||
|
||||
serialized_data['nonvoting'] = self.get_non_voting_employees(vote)
|
||||
|
||||
return Response(serialized_data)
|
||||
|
||||
def get_vote(self, vote_id):
|
||||
try:
|
||||
vote = Vote.objects.get(id=vote_id)
|
||||
return vote
|
||||
except Vote.DoesNotExist:
|
||||
return Response({'status': 'error', 'message': 'Vote not found'}, status=404)
|
||||
|
||||
serialized_data = VoteSerializer(vote).data
|
||||
|
||||
def get_employee_answers(self, vote):
|
||||
answers_employee = Answer.objects.filter(
|
||||
question__vote=vote,
|
||||
question__settings__choice="employee"
|
||||
).values('name', 'question')
|
||||
|
||||
answers_names = [item['name'] for item in answers_employee]
|
||||
question_ids = [item['question'] for item in answers_employee]
|
||||
|
||||
employee_dict = {}
|
||||
if answers_employee:
|
||||
employees_serializer = get_employees_serializer(answers_names)
|
||||
employee_dict = {employee['id']: employee for employee in employees_serializer}
|
||||
|
||||
return answers_employee
|
||||
|
||||
def create_employee_dict(self, answers_names):
|
||||
if not answers_names:
|
||||
return {}
|
||||
|
||||
employees_serializer = get_employees_serializer(answers_names)
|
||||
return {employee['id']: employee for employee in employees_serializer}
|
||||
|
||||
def get_answer_counts(self, vote):
|
||||
results = Result.objects.filter(
|
||||
answer__question__vote=vote
|
||||
).values('answer_id').annotate(count=Count('id'))
|
||||
|
||||
result_dict = {int(result['answer_id']): result['count'] for result in results}
|
||||
|
||||
return {int(result['answer_id']): result['count'] for result in results}
|
||||
|
||||
def add_employee_info(self, serialized_data, answers_employee, answers_names, employee_dict, result_dict):
|
||||
question_ids = [item['question'] for item in answers_employee]
|
||||
for question in serialized_data['questions']:
|
||||
for answer in question['answers']:
|
||||
answer['count'] = result_dict.get(int(answer['id']), 0)
|
||||
if question['id'] in question_ids:
|
||||
if answer['name'] in answers_names:
|
||||
answer['employee'] = employee_dict.get(int(answer['name']))
|
||||
|
||||
users = Result.objects.filter(answer__question__vote=vote).values('user_id')
|
||||
users = User.objects.filter(created_at__lt=vote.dateend).exclude(id__in=users)
|
||||
employee_ids = users.values_list('user_id', flat=True)
|
||||
serialized_data['nonvoting'] = get_employees_serializer(employee_ids)
|
||||
|
||||
return Response(serialized_data)
|
||||
|
||||
def get_non_voting_employees(self, vote):
|
||||
users = Result.objects.filter(answer__question__vote=vote).values('user_id')
|
||||
non_voting_users = User.objects.filter(created_at__lt=vote.dateend).exclude(id__in=users)
|
||||
employee_ids = non_voting_users.values_list('user_id', flat=True)
|
||||
return get_employees_serializer(employee_ids)
|
||||
|
||||
class VoteResultCreate(generics.ListCreateAPIView):
|
||||
queryset = Result.objects.all()
|
||||
serializer_class = ResultSerializer
|
||||
|
||||
Reference in new issue
Block a user