Commit 50a7382f authored by Luis García's avatar Luis García
Browse files

Add filter and improve the queryset

parent c6ed110b
......@@ -221,10 +221,10 @@ def createQuestionOptionBySerializer(data, owner):
class ResultsAnswersSerializer(ModelSerializer):
answer = serializers.SerializerMethodField()
question = serializers.SerializerMethodField()
type = serializers.SerializerMethodField()
consultation = serializers.SerializerMethodField()
survey = serializers.SerializerMethodField()
question = serializers.CharField(max_length=300)
type = serializers.CharField(max_length=300)
consultation = serializers.CharField(max_length=300)
survey = serializers.CharField(max_length=300)
location = serializers.SerializerMethodField()
location_wkt = serializers.SerializerMethodField()
......@@ -233,13 +233,13 @@ class ResultsAnswersSerializer(ModelSerializer):
fields = ('answer', 'question', 'type', 'consultation', 'survey', 'created', 'location', 'location_wkt')
def get_answer(self, obj):
response = obj.content['value']
if obj.question.type=='spatial':
response = obj['content']['value']
if obj['type'] == 'spatial':
try:
response = GEOSGeometry(str(response)).wkt
except ValueError:
response=str(response)
elif obj.question.type=='option':
elif obj['type'] == 'option':
try:
temp = QuestionOption.objects.get(pk=response)
response = temp.option
......@@ -249,30 +249,18 @@ class ResultsAnswersSerializer(ModelSerializer):
response = str(response)
return response
def get_question(self, obj):
return obj.question.content
def get_type(self, obj):
return obj.question.type
def get_consultation(self, obj):
return obj.response.survey.consultation.name
def get_survey(self, obj):
return obj.response.survey.name
def get_location(self, obj):
location = obj.response.owner.profile.location
location = obj['location_b']
response = None
if location:
response = json.loads(obj.response.owner.profile.location.geojson)
response = obj['location_b'].geojson
return response
def get_location_wkt(self, obj):
location = obj.response.owner.profile.location
location = obj['location_b']
response = None
if location:
response = obj.response.owner.profile.location.wkt
response = obj['location_b'].wkt
return response
class ResultsInputsSerializer(Serializer):
......
......@@ -229,20 +229,49 @@ class ResultsAnswersViewSet(mixins.ListModelMixin, GenericViewSet):
queryset = Answer.objects.filter(visible=True).exclude(question__type = 'cm')
serializer_class = ResultsAnswersSerializer
#Enable filter by code
filterset_class = ResultsAnswersFilter
#filterset_class = ResultsAnswersFilter
#Render
renderer_classes = (r.CSVRenderer, ) + tuple(api_settings.DEFAULT_RENDERER_CLASSES)
def get_queryset(self):
consultation = self.request.query_params.get('consultation')
survey = self.request.query_params.get('survey')
question = self.request.query_params.get('question')
#Get the socialinputs and get only the visibles
answers = Answer.objects.filter(visible=True)
if consultation:
answers = answers.filter(response__survey__consultation=consultation)
if survey:
answers = answers.filter(response__survey=survey)
if question:
answers = answers.filter(question=question)
answers = answers.values('content').annotate(
question=F('question__content'),
type=F('question__type'),
consultation=F('response__survey__consultation__name'),
survey=F('response__survey__name'),
location_b=F('response__owner__profile__location')
).values(
'content', 'question', 'type', 'consultation', 'survey', 'location_b'
)
return list( answers )
class ResultsInputsViewSet(mixins.ListModelMixin, GenericViewSet):
serializer_class = ResultsInputsSerializer
#Render
renderer_classes = (r.CSVRenderer, ) + tuple(api_settings.DEFAULT_RENDERER_CLASSES)
def get_queryset(self):
consultation = self.request.query_params.get('consultation')
survey = self.request.query_params.get('survey')
#Get the socialinputs and get only the visibles
spatial_inputs = SpatialInput.objects.filter(
visible=True
).values(
spatial_inputs = SpatialInput.objects.filter(visible=True)
if consultation:
spatial_inputs = spatial_inputs.filter(answer__response__survey__consultation=consultation)
if survey:
spatial_inputs = spatial_inputs.filter(answer__response__survey=survey)
#Get the socialinputs and get only the visibles
spatial_inputs = spatial_inputs.values(
'name', 'description', 'geometry', 'photo'
#Add the extra fields
).annotate(
......@@ -254,9 +283,13 @@ class ResultsInputsViewSet(mixins.ListModelMixin, GenericViewSet):
'name', 'description', 'category', 'geometry', 'origin', 'photo', 'survey', 'consultation'
)
#Get the social inputs
social_inputs = SocialInput.objects.filter(
visible=True
).values(
social_inputs = SocialInput.objects.filter(visible=True)
if consultation:
social_inputs = social_inputs.filter(survey__consultation=consultation)
if survey:
social_inputs = social_inputs.filter(survey=survey)
#Get the socialinputs and get only the visibles
social_inputs = social_inputs.values(
'description', 'geometry', 'photo'
).annotate(
name=F('description'),
......
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