Commit 9633598f authored by Luis García's avatar Luis García
Browse files

Add api for get the results answers on CSV

parent b0687b1f
......@@ -11,6 +11,7 @@ django-ckeditor==5.7.1
django-crontab==0.7.1
django-filter==2.1.0
djangorestframework-jwt==1.11.0
djangorestframework-csv==2.1.0
social-auth-app-django==3.1.0
djangorestframework-gis==0.14
drf-dynamic-fields==0.3.1
......
from django_filters import rest_framework
from .models import Answer, Question, Survey
from project.models import Consultation
class SurveyFilter(rest_framework.FilterSet):
question = rest_framework.ModelChoiceFilter(queryset=Question.objects.all(), field_name='question')
......@@ -7,4 +8,13 @@ class SurveyFilter(rest_framework.FilterSet):
class Meta:
model = Answer
fields = ['question', 'survey']
\ No newline at end of file
fields = ['question', 'survey']
class ResultsAnswersFilter(rest_framework.FilterSet):
question = rest_framework.ModelChoiceFilter(queryset=Question.objects.filter(visible=True), field_name='question')
survey = rest_framework.ModelChoiceFilter(queryset=Survey.objects.filter(visible=True), field_name='response__survey')
consultation = rest_framework.ModelChoiceFilter(queryset=Consultation.objects.filter(visible=True), field_name='response__survey__consultation')
class Meta:
model = Answer
fields = ['question', 'survey', 'consultation']
\ No newline at end of file
from .surveys import CategorySerializer, CategorySurveySerializer, IndicatorSerializer, QuestionSerializer, QuestionOptionSerializer, SpatialQuestionSerializer, SurveyQuestionSerializer, SurveySerializer, AnswerSerializer, ResponseSerializer, SpatialInputSerializer, SpatialInputAnswerSerializer, SocialInputsSerializer
\ No newline at end of file
from .surveys import CategorySerializer, CategorySurveySerializer, IndicatorSerializer, QuestionSerializer, QuestionOptionSerializer, SpatialQuestionSerializer, SurveyQuestionSerializer, SurveySerializer, AnswerSerializer, ResponseSerializer, SpatialInputSerializer, SpatialInputAnswerSerializer, SocialInputsSerializer, ResultsAnswersSerializer
\ No newline at end of file
import json
import json
from django.contrib.gis.geos import GEOSGeometry
from rest_framework.serializers import ModelSerializer, ValidationError, PrimaryKeyRelatedField, SerializerMethodField
from rest_framework import serializers
from surveys.models import Category, Survey, CategorySurvey, Indicator, Question, QuestionOption, SpatialQuestion, SurveyQuestion, Response, Answer, SpatialInput, SocialInput
......@@ -217,3 +218,43 @@ def createQuestionOptionBySerializer(data, owner):
#If option is not valid, send the error message
else:
raise ValidationError(option_serializer.errors)
class ResultsAnswersSerializer(ModelSerializer):
answer = serializers.SerializerMethodField()
question = serializers.SerializerMethodField()
type = serializers.SerializerMethodField()
consultation = serializers.SerializerMethodField()
survey = serializers.SerializerMethodField()
class Meta:
model = Answer
fields = ('answer', 'question', 'type', 'consultation', 'survey', 'created')
def get_answer(self, obj):
response = obj.content['value']
if obj.question.type=='spatial':
try:
response = GEOSGeometry(str(response)).wkt
except ValueError:
response=str(response)
elif obj.question.type=='option':
try:
temp = QuestionOption.objects.get(pk=response)
response = temp.option
except ValueError:
response=str(response)
else:
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
\ 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
from .views import CategoryViewSet, SurveyViewSet, CategorySurveyViewSet, IndicatorViewSet, QuestionViewSet, QuestionOptionViewSet, SpatialQuestionViewSet, SurveyQuestionViewSet, ResponseViewSet, AnswerViewSet, SpatialInputViewSet, SpatialInputAnswerViewSet, SocialInputViewSet, ResultsAnswersViewSet
#views for api core
router = DefaultRouter()
......@@ -16,4 +16,5 @@ router.register(r'answers', AnswerViewSet, basename='answers')
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')
surveys_api_patterns = router.urls
from .api import CategoryViewSet, SurveyViewSet, CategorySurveyViewSet, IndicatorViewSet, QuestionViewSet, SpatialQuestionViewSet, QuestionOptionViewSet, SurveyQuestionViewSet, ResponseViewSet, AnswerViewSet, SpatialInputViewSet, SpatialInputAnswerViewSet, SocialInputViewSet
\ No newline at end of file
from .api import CategoryViewSet, SurveyViewSet, CategorySurveyViewSet, IndicatorViewSet, QuestionViewSet, SpatialQuestionViewSet, QuestionOptionViewSet, SurveyQuestionViewSet, ResponseViewSet, AnswerViewSet, SpatialInputViewSet, SpatialInputAnswerViewSet, SocialInputViewSet, ResultsAnswersViewSet
\ No newline at end of file
......@@ -5,12 +5,16 @@ from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response as Http_Response
from rest_framework.viewsets import ModelViewSet, GenericViewSet
from rest_framework import mixins
#csv
from rest_framework.settings import api_settings
from rest_framework_csv import renderers as r
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
from surveys.filters import SurveyFilter
from surveys.serializers import CategorySerializer, SurveySerializer, CategorySurveySerializer, IndicatorSerializer, QuestionSerializer, QuestionOptionSerializer, SpatialQuestionSerializer, SurveyQuestionSerializer, ResponseSerializer, AnswerSerializer, SpatialInputSerializer, SpatialInputAnswerSerializer, SocialInputsSerializer, ResultsAnswersSerializer
from surveys.filters import SurveyFilter, ResultsAnswersFilter
from surveys.permissions import AnswerPermissions, SpatialInputPermissions
############################################################
......@@ -205,4 +209,16 @@ class SocialInputViewSet(mixins.ListModelMixin, GenericViewSet):
serializer_class = SocialInputsSerializer
#Enable filter by code
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('question', 'survey')
\ No newline at end of file
filterset_fields = ('question', 'survey')
############################################################
#REST API for answers
############################################################
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
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