Доработаны роуты для голосования и получения информации о голосованиях

This commit is contained in:
Andrei Filippov committed 2024-09-27 14:26:48 +03:00
1 parent dfc412dfc8
commit 5c9368f666
4 files changed
+35 -13

No files matched your search

-2
View File
@@ -4,7 +4,6 @@ from django.db import models
class ThanksVote(models.Model):
cand_name_id = models.IntegerField(blank=False)
thanks = models.BooleanField(default=False)
vote_thanks_date = models.DateTimeField(auto_now_add=True)
class Meta:
@@ -17,7 +16,6 @@ class ThanksVote(models.Model):
class BestVote(models.Model):
cand_name_id = models.IntegerField(blank=False)
likes = models.BooleanField(default=False)
vote_best_date = models.DateTimeField(auto_now_add=True)
class Meta:
+4 -4
View File
@@ -3,11 +3,11 @@ from .models import *
class ThanksVoteSerializer(serializers.ModelSerializer):
class Meta:
model= ThanksVote,
fields = '__all_'
model= ThanksVote
fields = ('id', 'cand_name_id','vote_thanks_date')
class BestVoteSerializer(serializers.ModelSerializer):
class Meta:
model = BestVote,
fields = '__all__'
model = BestVote
fields = ('id', 'cand_name_id','vote_best_date')
+4 -2
View File
@@ -3,6 +3,8 @@ from .views import *
urlpatterns = [
# path('setthanks/', SetThank.as_view(), name = 'Отправить Спасибо'),
path('getthanks/', GetThanks.as_view(), name = 'Получение информации'),
path('setthanks/', SetThanks.as_view(), name=''),
path('getthanks/', GetThanks.as_view(), name=''),
path('setbest/', SetBest.as_view(), name=''),
path('getbest/', GetBest.as_view(), name=''),
]
+27 -5
View File
@@ -1,13 +1,35 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serializers import *
# Create your views here.
class SetThanks(APIView):
def post(self, request):
cand_thanks_id = request.GET.get('id')
ThanksVote.objects.update_or_create(
cand_name_id = cand_thanks_id,
defaults = None
)
return Response("Ваш голос добавлен!")
class GetThanks(APIView):
def get(self, request):
cand_id = request.GET.get('id')
req_cand = ThanksVote.objects.filter(cand_name_id = cand_id, thanks = True)
print(f'candidate_id: {req_cand}')
return Response('test')
all_thanks = ThanksVote.objects.all()
return Response(ThanksVoteSerializer(all_thanks, many=True).data)
class SetBest(APIView):
def post(self, request):
cand_best_id = request.GET.get('id')
BestVote.objects.update_or_create(
cand_name_id = cand_best_id,
defaults = None
)
return Response("Ваш голос добавлен!")
class GetBest(APIView):
def get(self, request):
all_thanks = BestVote.objects.all()
return Response(BestVoteSerializer(all_thanks, many=True).data)