Commit 31e0b25b authored by Luis García's avatar Luis García
Browse files

Results api for inputs

parent 9633598f
from .surveys import CategorySerializer, CategorySurveySerializer, IndicatorSerializer, QuestionSerializer, QuestionOptionSerializer, SpatialQuestionSerializer, SurveyQuestionSerializer, SurveySerializer, AnswerSerializer, ResponseSerializer, SpatialInputSerializer, SpatialInputAnswerSerializer, SocialInputsSerializer, ResultsAnswersSerializer
\ No newline at end of file
from .surveys import CategorySerializer, CategorySurveySerializer, IndicatorSerializer, QuestionSerializer, QuestionOptionSerializer, SpatialQuestionSerializer, SurveyQuestionSerializer, SurveySerializer, AnswerSerializer, ResponseSerializer, SpatialInputSerializer, SpatialInputAnswerSerializer, SocialInputsSerializer, ResultsAnswersSerializer, ResultsInputsSerializer
\ No newline at end of file
import json
from django.contrib.gis.geos import GEOSGeometry
from rest_framework.serializers import ModelSerializer, ValidationError, PrimaryKeyRelatedField, SerializerMethodField
from rest_framework.serializers import ModelSerializer, Serializer, ValidationError, PrimaryKeyRelatedField, SerializerMethodField
from rest_framework import serializers
from surveys.models import Category, Survey, CategorySurvey, Indicator, Question, QuestionOption, SpatialQuestion, SurveyQuestion, Response, Answer, SpatialInput, SocialInput
from api.mixins import AllowUniqueMixin
......@@ -257,4 +257,19 @@ class ResultsAnswersSerializer(ModelSerializer):
return obj.response.survey.consultation.name
def get_survey(self, obj):
return obj.response.survey.name
\ No newline at end of file
return obj.response.survey.name
class ResultsInputsSerializer(Serializer):
name = serializers.CharField(max_length=100)
description = serializers.CharField(max_length=300)
category = serializers.CharField(max_length=100)
geometry = serializers.SerializerMethodField()
origin = serializers.CharField(max_length=100)
photo = serializers.ImageField()
survey = serializers.CharField(max_length=100)
consultation = serializers.CharField(max_length=200)
def get_geometry(self, obj):
response = obj['geometry']
response = GEOSGeometry(str(response)).wkt
return response
\ No newline at end of file
from rest_framework.routers import DefaultRouter
from .views import CategoryViewSet, SurveyViewSet, CategorySurveyViewSet, IndicatorViewSet, QuestionViewSet, QuestionOptionViewSet, SpatialQuestionViewSet, SurveyQuestionViewSet, ResponseViewSet, AnswerViewSet, SpatialInputViewSet, SpatialInputAnswerViewSet, SocialInputViewSet, ResultsAnswersViewSet
from .views import CategoryViewSet, SurveyViewSet, CategorySurveyViewSet, IndicatorViewSet, QuestionViewSet, QuestionOptionViewSet, SpatialQuestionViewSet, SurveyQuestionViewSet, ResponseViewSet, AnswerViewSet, SpatialInputViewSet, SpatialInputAnswerViewSet, SocialInputViewSet, ResultsAnswersViewSet, ResultsInputsViewSet
#views for api core
router = DefaultRouter()
......@@ -17,4 +17,5 @@ router.register(r'spatial-inputs', SpatialInputViewSet, basename='spatial-inputs
router.register(r'spatial-inputs-answers', SpatialInputAnswerViewSet, basename='spatial-answers')
router.register(r'social-inputs', SocialInputViewSet, basename='social-inputs')
router.register(r'results-answers', ResultsAnswersViewSet, basename='results-answers')
router.register(r'results-inputs', ResultsInputsViewSet, basename='results-inputs')
surveys_api_patterns = router.urls
from .api import CategoryViewSet, SurveyViewSet, CategorySurveyViewSet, IndicatorViewSet, QuestionViewSet, SpatialQuestionViewSet, QuestionOptionViewSet, SurveyQuestionViewSet, ResponseViewSet, AnswerViewSet, SpatialInputViewSet, SpatialInputAnswerViewSet, SocialInputViewSet, ResultsAnswersViewSet
\ No newline at end of file
from .api import CategoryViewSet, SurveyViewSet, CategorySurveyViewSet, IndicatorViewSet, QuestionViewSet, SpatialQuestionViewSet, QuestionOptionViewSet, SurveyQuestionViewSet, ResponseViewSet, AnswerViewSet, SpatialInputViewSet, SpatialInputAnswerViewSet, SocialInputViewSet, ResultsAnswersViewSet, ResultsInputsViewSet
\ No newline at end of file
from django.db.models import Value, CharField, F
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status
from rest_framework.decorators import action
......@@ -13,7 +14,7 @@ from api.mixins import OwnerFieldMixin, DestroyToInvisibleMixin
from api.permissions import IsStaffOrReadOnly, IsOwner
from status.methods import StatusMethod
from surveys.models import Category, Survey, CategorySurvey, Indicator, Question, QuestionOption, SpatialQuestion, SurveyQuestion, Response, Answer, SpatialInput, SocialInput
from surveys.serializers import CategorySerializer, SurveySerializer, CategorySurveySerializer, IndicatorSerializer, QuestionSerializer, QuestionOptionSerializer, SpatialQuestionSerializer, SurveyQuestionSerializer, ResponseSerializer, AnswerSerializer, SpatialInputSerializer, SpatialInputAnswerSerializer, SocialInputsSerializer, ResultsAnswersSerializer
from surveys.serializers import CategorySerializer, SurveySerializer, CategorySurveySerializer, IndicatorSerializer, QuestionSerializer, QuestionOptionSerializer, SpatialQuestionSerializer, SurveyQuestionSerializer, ResponseSerializer, AnswerSerializer, SpatialInputSerializer, SpatialInputAnswerSerializer, SocialInputsSerializer, ResultsAnswersSerializer, ResultsInputsSerializer
from surveys.filters import SurveyFilter, ResultsAnswersFilter
from surveys.permissions import AnswerPermissions, SpatialInputPermissions
......@@ -212,13 +213,31 @@ class SocialInputViewSet(mixins.ListModelMixin, GenericViewSet):
filterset_fields = ('question', 'survey')
############################################################
#REST API for answers
#REST API for results
############################################################
class ResultsAnswersViewSet(mixins.ListModelMixin, GenericViewSet):
#Everyone can read, just staff user can modificate
queryset = Answer.objects.filter(visible=True).exclude(question__type = 'cm')
serializer_class = ResultsAnswersSerializer
#Enable filter by code
filterset_class = ResultsAnswersFilter
#Render
renderer_classes = (r.CSVRenderer, ) + tuple(api_settings.DEFAULT_RENDERER_CLASSES)
\ No newline at end of file
renderer_classes = (r.CSVRenderer, ) + tuple(api_settings.DEFAULT_RENDERER_CLASSES)
class ResultsInputsViewSet(mixins.ListModelMixin, GenericViewSet):
serializer_class = ResultsInputsSerializer
#Render
renderer_classes = (r.CSVRenderer, ) + tuple(api_settings.DEFAULT_RENDERER_CLASSES)
def get_queryset(self):
spatial_inputs = SpatialInput.objects.filter(visible=True).annotate(
origin=Value('platform', CharField()),
survey=F('answer__response__survey__name'),
consultation=F('answer__response__survey__consultation__name')
).values('name', 'description', 'category', 'geometry', 'origin', 'photo', 'survey', 'consultation')
social_inputs = SocialInput.objects.filter(visible=True).annotate(
name=F('description'),
category=Value('', CharField()),
origin=F('network'),
consultation=F('survey__consultation__name')
).values('name', 'description', 'category', 'geometry', 'origin', 'photo', 'survey', 'consultation')
return spatial_inputs#.union(social_inputs)
\ No newline at end of file
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