From c02fae2aabb69be6923b63f4d46e71b248669295 Mon Sep 17 00:00:00 2001 From: pedalopon Date: Fri, 31 Dec 2021 17:30:51 +0100 Subject: [PATCH] #13-feat: Finished create voting endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Beatriz María Beltrán Álvarez Co-Authored-By: José Luis Alonso Rocha --- decide/administration/serializers.py | 2 +- decide/administration/urls.py | 2 +- decide/administration/views.py | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/decide/administration/serializers.py b/decide/administration/serializers.py index a50f31d6ee..c98b888c84 100644 --- a/decide/administration/serializers.py +++ b/decide/administration/serializers.py @@ -67,7 +67,7 @@ class Meta: class VotingSerializer(serializers.Serializer): question = AdminQuestionSerializer(many=False) - auth = AuthSerializer(many=False) + auth = serializers.URLField() name = serializers.CharField(max_length=200) desc = serializers.CharField(max_length=1000, allow_blank=True, allow_null=True) census = serializers.ListField() diff --git a/decide/administration/urls.py b/decide/administration/urls.py index e7cbd75954..abcd475c6a 100644 --- a/decide/administration/urls.py +++ b/decide/administration/urls.py @@ -19,7 +19,7 @@ path('api/users/state', views.UpdateUserStateAPI.as_view()), path('api/votings/question', views.QuestionsAPI.as_view()), path('api/votings/question//', views.QuestionAPI.as_view()), - path('api/votings/voting', views.VotingAPI.as_view()), + path('api/votings', views.VotingAPI.as_view()), # react-app url('', views.index) ] diff --git a/decide/administration/views.py b/decide/administration/views.py index da7f808a27..5036c88a9d 100644 --- a/decide/administration/views.py +++ b/decide/administration/views.py @@ -40,17 +40,23 @@ def post(self, request): 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)) - + auth_object = Auth(name="Auth", url=auth_url, me=True) + auth_object.save() + question = Question(desc=request.data.get('question').get("desc")) + question.save() + options = request.data.get('question').get("options") + for opt in options: + option = QuestionOption(question=question, option=opt.get("option"), number=opt.get("number")) + option.save() voting = Voting(name=request.data.get("name"), desc=request.data.get("desc"), - question=request.data.get("question")) + question=question) + voting.save() voting.auths.add(auth_object) - voting = voting.save() - voting_id = voting.get("id") + voting_id = voting.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) + return Response({"id": voting_id, "name": voting.name}, status=HTTP_200_OK) class QuestionsAPI(APIView):