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