рефакторинг vote/views
This commit is contained in:
1 parent
33582a04e7
commit
7a2862a825
1 file changed
+84
-36
+84
-36
@@ -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,36 +66,56 @@ 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')
|
||||||
|
|
||||||
|
def serialize_employees(self, answers_names):
|
||||||
|
|
||||||
|
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):
|
||||||
|
|
||||||
answers_names = [item['name'] for item in answers_employee]
|
|
||||||
question_ids = [item['question'] for item in answers_employee]
|
question_ids = [item['question'] for item in answers_employee]
|
||||||
|
|
||||||
if answers_employee:
|
|
||||||
employees_serializer = get_employees_serializer(answers_names)
|
|
||||||
employee_dict = {employee['id']: employee for employee in employees_serializer}
|
|
||||||
|
|
||||||
serialized_data = self.retrieve(request, *args, **kwargs).data
|
|
||||||
|
|
||||||
for question in serialized_data['questions']:
|
for question in serialized_data['questions']:
|
||||||
for answer in question['answers']:
|
for answer in question['answers']:
|
||||||
if question['id'] in question_ids:
|
if question['id'] in question_ids:
|
||||||
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]
|
|
||||||
|
|
||||||
employee_dict = {}
|
def create_employee_dict(self, answers_names):
|
||||||
if answers_employee:
|
if not answers_names:
|
||||||
employees_serializer = get_employees_serializer(answers_names)
|
return {}
|
||||||
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 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