Commit 664b0c8b authored by Luis García's avatar Luis García
Browse files

More fixes on the surveys api

parent 747d1d5f
......@@ -30,7 +30,7 @@ class SpatialQuestionSerializer(ModelSerializer):
fields = ('id', 'slug', 'center', 'zoom', 'study_area', 'map', 'question')
class QuestionSerializer(ModelSerializer):
options = QuestionOptionSerializer_temp(many=True)
options = SerializerMethodField('get_options_filter')
indicator = AsymetricRelatedField.from_serializer(IndicatorSerializer)()
spatial_data = SpatialQuestionSerializer(many=True, read_only=True)
......@@ -38,6 +38,11 @@ class QuestionSerializer(ModelSerializer):
model = Question
fields = ('id', 'content', 'indicator', 'type', 'options', 'spatial_data')
def get_options_filter(self, object):
qs = QuestionOption.objects.filter(visible=True, question=object)
serializer = QuestionOptionSerializer_temp(instance=qs, many=True)
return serializer.data
def create(self, validated_data):
question_options = None
#If options was send, pop options
......@@ -174,7 +179,7 @@ class SpatialInputSerializer(ModelSerializer):
fields = ('id', 'name', 'description', 'geometry', 'photo', 'category', 'answer')
class ResponseSerializer(ModelSerializer):
answers = AnswerSerializer_temp(many=True, read_only=True)
answers = SerializerMethodField('get_answers_filter')
status = AsymetricRelatedField.from_serializer(StatusSerializer)()
owner = OwnerSerializer(read_only=True)
......@@ -182,6 +187,10 @@ class ResponseSerializer(ModelSerializer):
model = Response
fields = ('id', 'survey', 'status', 'created', 'answers', 'owner')
def get_answers_filter(self, object):
qs = Answer.objects.filter(visible=True, response=object)
serializer = AnswerSerializer_temp(instance=qs, many=True, read_only=True)
return serializer.data
class SpatialInputAnswerSerializer(ModelSerializer):
spatial_inputs = SpatialInputSerializer_temp(many=True, read_only=True)
......
from rest_framework.serializers import ModelSerializer
from rest_framework.serializers import ModelSerializer, SerializerMethodField
from surveys.models import Survey, Indicator, Question, QuestionOption, SurveyQuestion, Response, Answer, SpatialInput, SocialInput
from project.models import Consultation
......@@ -15,12 +15,17 @@ class QuestionOptionSerializer_temp(ModelSerializer):
class QuestionSerializer_temp(ModelSerializer):
indicator = IndicatorSerializer_temp(read_only=True)
options = QuestionOptionSerializer_temp(many=True, read_only=True)
options = SerializerMethodField('get_options_filter')
class Meta:
model = Question
fields = ('id', 'content', 'indicator', 'type', 'options')
def get_options_filter(self, object):
qs = QuestionOption.objects.filter(visible=True, question=object)
serializer = QuestionOptionSerializer_temp(instance=qs, many=True, read_only=True)
return serializer.data
class AnswerSerializer_temp(ModelSerializer):
question = QuestionSerializer_temp(read_only=True)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment