Skip to content

Commit

Permalink
EGCETSII#13 - feat: Added voting serializer
Browse files Browse the repository at this point in the history
Co-Authored-By: Beatriz María Beltrán Álvarez <[email protected]>
  • Loading branch information
josaloroc and Beatriz María Beltrán Álvarez committed Dec 29, 2021
1 parent 6223532 commit ca30977
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions decide/administration/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from census.models import Census
from voting.models import Question, QuestionOption, Voting
from rest_framework import serializers
from base.serializers import AuthSerializer


class UserAdminSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
Expand All @@ -20,6 +22,7 @@ class Meta:
model = QuestionOption
fields = ('id', 'number', 'option')


class UserUpdateSerializer(serializers.Serializer):
username = serializers.CharField(max_length=150, min_length=1)
first_name = serializers.CharField(max_length=30, allow_blank=True)
Expand All @@ -29,10 +32,11 @@ class UserUpdateSerializer(serializers.Serializer):

class AdminQuestionSerializer(serializers.ModelSerializer):
options = AdminQuestionOptionSerializer(many=True)

class Meta:
model = Question
fields = ('id','desc', 'options')
depht=1
fields = ('id', 'desc', 'options')
depht = 1

def create(self, validated_data):
options_data = validated_data.pop("options")
Expand All @@ -50,14 +54,20 @@ def update(self, instance, validated_data):
for option_data in options_data:
option = options.pop(0)
option.number = option_data.get("number", option.number)
option.option = option_data.get("option",option.option)
option.option = option_data.get("option", option.option)
option.save()
return instance




class CensusSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Census
fields = ('id', 'voting_id', 'voter_id')


class VotingSerializer(serializers.Serializer):
question = AdminQuestionSerializer(many=False)
auth = AuthSerializer(many=False)
name = serializers.CharField(max_length=200)
desc = serializers.CharField(max_length=1000, allow_blank=True, allow_null=True)
census = serializers.ListField()

0 comments on commit ca30977

Please sign in to comment.