Commit 0beda7c4 authored by Luis García's avatar Luis García
Browse files

Add restrictions to poll answers

parent bc702fa8
......@@ -3,6 +3,7 @@ from api.utils import AsymetricRelatedField
from .models import Poll, Question, Response, Answer
from project.serializers import ConsultationSerializer_temp
from datetime import date
class QuestionSerializer(ModelSerializer):
......@@ -27,6 +28,21 @@ class AnswerSerializer(ModelSerializer):
fields = ('id', 'question', 'response', 'content', 'created')
ref_name = 'Poll answer'
def validate(self, data):
in_valid = False
today = date.today()
user = self.context['request'].user
answers = Answer.objects.filter(
created__startswith=today,
question=data['question'],
response__owner=user
)
if answers.count() > 0:
in_valid = True
if in_valid:
raise ValidationError("You can't answer this question more than one time per day")
return data
class ResponseSerializer(ModelSerializer):
answers = AnswerSerializer(many=True, read_only=True)
......
......@@ -4,7 +4,7 @@ from rest_framework import viewsets
from rest_framework.viewsets import ModelViewSet
from rest_framework_extensions.mixins import NestedViewSetMixin
from polls.serializers import PollSerializer, QuestionSerializer, ResponseSerializer, AnswerSerializer
from api.permissions import IsStaffOrReadOnly
from api.permissions import IsStaffOrReadOnly, IsAuthOrReadOnly
from polls.models import Poll, Question, Response, Answer
from api.mixins import OwnerFieldMixin, DestroyToInvisibleMixin
......@@ -36,8 +36,8 @@ class QuestionViewSet(DestroyToInvisibleMixin, OwnerFieldMixin, ModelViewSet):
#REST API for response
############################################################
class ResponseViewSet(DestroyToInvisibleMixin, OwnerFieldMixin, ModelViewSet):
#Everyone can read, just staff user can modificate
permission_classes = (IsStaffOrReadOnly,)
#Everyone can read, just auth user can modificate
permission_classes = (IsAuthOrReadOnly,)
queryset = Response.objects.filter(visible=True)
serializer_class = ResponseSerializer
#Enable filter by code
......@@ -49,8 +49,8 @@ class ResponseViewSet(DestroyToInvisibleMixin, OwnerFieldMixin, ModelViewSet):
#REST API for answers
############################################################
class AnswerViewSet(DestroyToInvisibleMixin, ModelViewSet):
#Everyone can read, just staff user can modificate
permission_classes = (IsStaffOrReadOnly,)
#Everyone can read, just auth user can modificate
permission_classes = (IsAuthOrReadOnly,)
queryset = Answer.objects.filter(visible=True)
serializer_class = AnswerSerializer
#Enable filter by code
......
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