Skip to content

Commit

Permalink
EGCETSII#13-feat: Added GET and POST voting operations
Browse files Browse the repository at this point in the history
Co-authored-by: josaloroc <[email protected]>
Co-authored-by: beabelalv <[email protected]>
  • Loading branch information
pedalopon committed Dec 29, 2021
1 parent ca30977 commit 76089ba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
7 changes: 3 additions & 4 deletions decide/administration/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.urls.conf import re_path

from . import views
from .views import QuestionsAPI, QuestionAPI

urlpatterns = [
# API
Expand All @@ -18,9 +17,9 @@
path('api/census', views.CensussAPI.as_view()),
path('api/census/<int:census_id>', views.CensusAPI.as_view()),
path('api/users/state', views.UpdateUserStateAPI.as_view()),
path('api/votings/question', QuestionsAPI.as_view()),
path('api/votings/question/<int:question_id>/', QuestionAPI.as_view()),

path('api/votings/question', views.QuestionsAPI.as_view()),
path('api/votings/question/<int:question_id>/', views.QuestionAPI.as_view()),
path('api/votings/voting', views.VotingAPI.as_view()),
# react-app
url('', views.index)
]
33 changes: 33 additions & 0 deletions decide/administration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from authentication.serializers import UserSerializer
from administration.serializers import *
from base.serializers import AuthSerializer, KeySerializer
from decide.settings import BASEURL
from voting.serializers import SimpleVotingSerializer
from .serializers import CensusSerializer
from base.perms import IsAdminAPI
from voting.models import Question
Expand All @@ -20,6 +22,37 @@ def index(request):
return render(request, "build/index.html")


class VotingAPI(APIView):
permission_classes = (IsAdminAPI,)

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

def post(self, request):
voting_seria = VotingSerializer(data=request.data)
if not voting_seria.is_valid():
return Response({"result", "Voting object is not valid"}, status=HTTP_400_BAD_REQUEST)
else:
auth_url = request.data.get("auth")
id_users = request.data.get("census")
try:
auth_object = Auth.objects.filter(url=auth_url).get()
except ObjectDoesNotExist:
auth_object = Auth.objects.save(Auth(name="Auth", url=auth_url, me=True))

voting = Voting(name=request.data.get("name"), desc=request.data.get("desc"),
question=request.data.get("question"))
voting.auths.add(auth_object)
voting = voting.save()
voting_id = voting.get("id")
for voter_id in id_users:
census = Census(voting_id=voting_id, voter_id=voter_id)
census.save()
return Response({voting}, status=HTTP_200_OK)


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

Expand Down

0 comments on commit 76089ba

Please sign in to comment.