Skip to content

Commit

Permalink
EGCETSII#13 - feat: Added new endpoints for Voting API
Browse files Browse the repository at this point in the history
+Bulk delete
+Get by id
+Delete all elements

Co-Authored-By: Beatriz María Beltrán Álvarez <[email protected]>
  • Loading branch information
josaloroc and Beatriz María Beltrán Álvarez committed Jan 3, 2022
1 parent 16531fb commit 7f89b13
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions decide/administration/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
path('api/votings/question', views.QuestionsAPI.as_view()),
path('api/votings/question/<int:question_id>/', views.QuestionAPI.as_view()),
path('api/votings', views.VotingAPI.as_view()),
path('api/votings/<int:voting_id>/', views.VotingsAPI.as_view()),
# react-app
url('', views.index)
]
26 changes: 26 additions & 0 deletions decide/administration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,32 @@ def put(self, request):
st = status.HTTP_400_BAD_REQUEST
return Response(msg, status=st)

def delete(self, request):
if request.data.get("idList") is None:
Voting.objects.all().delete()
return Response({}, status=HTTP_200_OK)
else:
ids = request.data.get("idList")
is_valid(len(ids) > 0, 'The format of the ids list is not correct')
Voting.objects.filter(id__in=ids).delete()
return Response({}, status=HTTP_200_OK)

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

def get(self, request, voting_id):
try:
query = Voting.objects.filter(id=voting_id).get()
except ObjectDoesNotExist:
return Response({}, status=HTTP_404_NOT_FOUND)
rest = VotingSerializer(query).data
return Response(rest, status=HTTP_200_OK)


def delete(self, request, voting_id):
Voting.objects.all().filter(id=voting_id).delete()
return Response({}, status=HTTP_200_OK)


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

0 comments on commit 7f89b13

Please sign in to comment.