Skip to content

Commit

Permalink
feat: 투표 API 수정 및 후보리스트 필터링, 득표순 정렬
Browse files Browse the repository at this point in the history
  • Loading branch information
kynzun committed Dec 3, 2021
1 parent 8626f6a commit 2a301cd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
10 changes: 8 additions & 2 deletions account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
class User(AbstractUser):
part = models.CharField(max_length=2, choices=PART_CHOICES)


class Candidate(models.Model):
name = models.CharField(max_length=30)
part = models.CharField(max_length=2, choices=PART_CHOICES)

def __str__(self):
return self.name


class Vote(models.Model):
vote_user = models.ForeignKey(User, on_delete=models.CASCADE)
vote_candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE)
vote_user = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="user_votes"
)
vote_candidate = models.ForeignKey(
Candidate, on_delete=models.CASCADE, related_name="cand_votes"
)

def __str__(self):
return f"{self.vote_user.username}'s vote on {self.vote_candidate.name}"
18 changes: 12 additions & 6 deletions account/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ def create(self, validated_data):
return user


class CandidateSerializer(serializers.ModelSerializer):
class Meta:
model = Candidate
fields = ["id", "name", "part"]


class VoteSerializer(serializers.ModelSerializer):
vote_user = serializers.SerializerMethodField()
vote_candidate = serializers.SerializerMethodField()
Expand All @@ -40,3 +34,15 @@ def get_vote_candidate(self, obj):
class Meta:
model = Vote
fields = ["id", "vote_user", "vote_candidate"]


class CandidateSerializer(serializers.ModelSerializer):
cand_votes = VoteSerializer(many=True)
vote_count = serializers.SerializerMethodField()

class Meta:
model = Candidate
fields = ["id", "name", "part", "vote_count", "cand_votes"]

def get_vote_count(self, obj):
return obj.cand_votes.count()
2 changes: 1 addition & 1 deletion account/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
path("signup", RegisterAPIView.as_view()),
path("candidate", CandidateListAPIView.as_view()),
path("candidate/<int:pk>", CandidateDetailAPIView.as_view()),
path("vote", VoteAPIView.as_view()),
path("test", TestAPIView.as_view()),
path("testauth", TestAuthAPIView.as_view()),
]
40 changes: 25 additions & 15 deletions account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from .models import Candidate, Vote
from .serializers import RegisterSerializer, CandidateSerializer, VoteSerializer
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework.permissions import IsAuthenticated

from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
from django.db.models import Count

User = get_user_model()

Expand Down Expand Up @@ -78,11 +78,21 @@ def post(self, request):
class CandidateListAPIView(APIView):
def get(self, request, format=None):
candidates = Candidate.objects.all()
part = request.GET.get("part", None)
if part is not None:
candidates = candidates.filter(part=part)

# 득표순으로 정렬
candidates = candidates.annotate(vote_count=Count("cand_votes")).order_by(
"-vote_count"
)
serializer = CandidateSerializer(candidates, many=True)
return Response(serializer.data, status.HTTP_200_OK)


class CandidateDetailAPIView(APIView):
permission_classes = (IsAuthenticatedOrReadOnly,)

def get_object(self, pk):
try:
return Candidate.objects.get(pk=pk)
Expand All @@ -94,22 +104,22 @@ def get(self, request, pk, format=None):
serializer = CandidateSerializer(candidate)
return Response(serializer.data, status.HTTP_200_OK)


class VoteAPIView(APIView):
permission_classes = (IsAuthenticated,)

def post(self, request, format=None):
def post(self, request, pk, format=None):
try:
user_id = request.data.get("user_id")
candidate_id = request.data.get("candidate_id")
vote = Vote.objects.create(
vote_user=User.objects.get(pk=user_id),
vote_candidate=Candidate.objects.get(pk=candidate_id),
)
user = request.user
candidate = self.get_object(pk)
vote = Vote(vote_user=user, vote_candidate=candidate)
vote.save()
serializer = VoteSerializer(vote)
except Exception:
return Response(
{
"vote": serializer.data,
"message": "Successfully voted",
},
status=status.HTTP_200_OK,
)
except:
return Response(status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response(serializer.data, status.HTTP_201_CREATED)


class TestAPIView(APIView):
Expand Down

0 comments on commit 2a301cd

Please sign in to comment.