Skip to content

Commit

Permalink
EGCETSII#13-feat: Added action bulkupdate for votings
Browse files Browse the repository at this point in the history
  • Loading branch information
pedalopon committed Dec 31, 2021
1 parent c02fae2 commit ed30f11
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion decide/administration/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Meta:
fields = ('id', 'voting_id', 'voter_id')


class VotingSerializer(serializers.Serializer):
class AdminVotingSerializer(serializers.Serializer):
question = AdminQuestionSerializer(many=False)
auth = serializers.URLField()
name = serializers.CharField(max_length=200)
Expand Down
51 changes: 46 additions & 5 deletions decide/administration/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.shortcuts import render
from django.utils import timezone

from django.shortcuts import render, get_object_or_404
from rest_framework.status import *
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import parsers, renderers
from rest_framework import parsers, renderers, status
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.response import Response
Expand All @@ -11,7 +13,7 @@
from administration.serializers import *
from base.serializers import AuthSerializer, KeySerializer
from decide.settings import BASEURL
from voting.serializers import SimpleVotingSerializer
from voting.serializers import VotingSerializer
from .serializers import CensusSerializer
from base.perms import IsAdminAPI
from voting.models import Question
Expand All @@ -27,11 +29,11 @@ class VotingAPI(APIView):

def get(self, request):
query = Voting.objects.all()
rest = SimpleVotingSerializer(query, many=True).data
rest = VotingSerializer(query, many=True).data
return Response(rest, status=HTTP_200_OK)

def post(self, request):
voting_seria = VotingSerializer(data=request.data)
voting_seria = AdminVotingSerializer(data=request.data)
if not voting_seria.is_valid():
return Response({"result", "Voting object is not valid"}, status=HTTP_400_BAD_REQUEST)
else:
Expand All @@ -58,6 +60,45 @@ def post(self, request):
census.save()
return Response({"id": voting_id, "name": voting.name}, status=HTTP_200_OK)

def put(self, request):

votings_id = request.data.get("idList")
action = request.data.get('action')
if not action:
return Response({}, status=status.HTTP_400_BAD_REQUEST)
msg = ''
st = status.HTTP_200_OK
if action == 'start':
votings = Voting.objects.filter(id__in=votings_id, start_date__isnull=True)
if len(votings)> 0:
votings.update(start_date=timezone.now())
msg = 'Votings started'
else:
msg = 'All votings all already started'
st = status.HTTP_400_BAD_REQUEST

elif action == 'stop':
votings = Voting.objects.filter(id__in=votings_id, start_date__isnull=False, end_date__isnull=True)
if len(votings) > 0:
votings.update(end_date=timezone.now())
msg = 'Votings stopped'
else:
msg = 'All votings all already stopped or not started'
st = status.HTTP_400_BAD_REQUEST
elif action == 'tally':
votings = Voting.objects.filter(id__in=votings_id, start_date__isnull=False, end_date__isnull=False)
if len(votings) > 0:
for voting in votings:
key = request.COOKIES.get('token', "")
voting.tally_votes(key)
else:
msg = 'All votings all already tallied, not stopped or not started'
st = status.HTTP_400_BAD_REQUEST
else:
msg = 'Action not found, try with start, stop or tally'
st = status.HTTP_400_BAD_REQUEST
return Response(msg, status=st)


class QuestionsAPI(APIView):
permission_classes = (IsAdminAPI,)
Expand Down

0 comments on commit ed30f11

Please sign in to comment.