возврат изображений с голосованиями
This commit is contained in:
1 parent
bebfd39bd2
commit
1c4266ce8f
2 files changed
+22
-8
No files matched your search
@@ -2,6 +2,18 @@ from rest_framework import serializers
|
||||
from .models import *
|
||||
from userauth.models import *
|
||||
|
||||
class VotesSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Vote
|
||||
fields = '__all__'
|
||||
|
||||
def create(self, validated_data):
|
||||
user = self.context['request'].user
|
||||
if user.is_authenticated:
|
||||
validated_data['author'] = user
|
||||
|
||||
return super().create(validated_data)
|
||||
|
||||
class AnswerSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Answer
|
||||
|
||||
+10
-8
@@ -9,6 +9,7 @@ from datetime import datetime
|
||||
from rest_framework import generics
|
||||
from navigation.models import News
|
||||
import json
|
||||
from collections import defaultdict
|
||||
|
||||
class GetVotes(APIView):
|
||||
|
||||
@@ -19,18 +20,19 @@ class GetVotes(APIView):
|
||||
now = datetime.now()
|
||||
status = request.query_params.get('status')
|
||||
votes = self.get_votes_by_status(status, now)
|
||||
serialized_votes = VoteSerializer(votes, many=True).data
|
||||
|
||||
serialized_votes = VotesSerializer(votes, many=True).data
|
||||
questions = Question.objects.filter(vote__in=votes)
|
||||
files = [question.file.url for question in questions if question.file]
|
||||
|
||||
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]
|
||||
|
||||
result = {
|
||||
'votes': serialized_votes,
|
||||
'files': files}
|
||||
|
||||
return Response(result)
|
||||
return Response(serialized_votes)
|
||||
|
||||
def get_votes_by_status(self, status, now):
|
||||
print(status)
|
||||
|
||||
Reference in new issue
Block a user