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

Update polls api

parent 8a2befdc
......@@ -4,22 +4,6 @@ from django.contrib.auth.models import User
from project.models import Consultation
# Model for questions
class Question(models.Model):
content = models.CharField(verbose_name='Content', max_length=200)
owner = models.ForeignKey(User, verbose_name='Owner', on_delete=models.PROTECT, related_name='polls_questions')
created = models.DateTimeField(auto_now_add=True, verbose_name='Create date')
updated = models.DateTimeField(auto_now=True, verbose_name='Update date')
visible = models.BooleanField(verbose_name='visible', default=True)
class Meta():
verbose_name = 'Question'
verbose_name_plural = 'Questions'
ordering = ['-created']
def __str__(self):
return self.content
# Model for Polls
class Poll(models.Model):
name = models.CharField(verbose_name='Name', max_length=100)
......@@ -37,20 +21,25 @@ class Poll(models.Model):
def __str__(self):
return self.name
# Model for relate poll questions
class PollQuestion(models.Model):
# Model for questions
class Question(models.Model):
TYPE_CHOICES=[('quick', 'Quick'),('healt', 'Healt'),]
content = models.CharField(verbose_name='Content', max_length=200)
poll = models.ForeignKey(Poll, verbose_name='Poll', on_delete=models.PROTECT, related_name='questions')
question = models.ForeignKey(Question, verbose_name='Question', on_delete=models.PROTECT)
order = models.IntegerField(verbose_name='Order')
type = models.CharField(verbose_name='Type', max_length=20, choices=TYPE_CHOICES, default='x')
owner = models.ForeignKey(User, verbose_name='Owner', on_delete=models.PROTECT, related_name='polls_questions')
created = models.DateTimeField(auto_now_add=True, verbose_name='Create date')
updated = models.DateTimeField(auto_now=True, verbose_name='Update date')
visible = models.BooleanField(verbose_name='visible', default=True)
class Meta():
unique_together = (('poll', 'question'),)
verbose_name = 'Poll question'
verbose_name_plural = 'Poll questions'
verbose_name = 'Question'
verbose_name_plural = 'Questions'
ordering = ['-created']
def __str__(self):
return self.question.content
return self.content
# Model for Response from one user to one poll
class Response(models.Model):
......
from rest_framework.serializers import ModelSerializer, ValidationError
from api.utils import AsymetricRelatedField
from .models import Poll, Question, PollQuestion, Response, Answer
from .models import Poll, Question, Response, Answer
from project.serializers import ConsultationSerializer_temp
......@@ -8,18 +8,11 @@ class QuestionSerializer(ModelSerializer):
class Meta:
model = Question
fields = ('id', 'content')
fields = ('id', 'content', 'poll', 'type')
ref_name = 'Poll question'
class PollQuestionSerializer(ModelSerializer):
question = AsymetricRelatedField.from_serializer(QuestionSerializer)()
class Meta:
model = PollQuestion
fields = ('id', 'poll', 'question', 'order')
class PollSerializer(ModelSerializer):
questions = PollQuestionSerializer(many=True, read_only=True)
questions = QuestionSerializer(many=True, read_only=True)
consultation = AsymetricRelatedField.from_serializer(ConsultationSerializer_temp)()
class Meta:
......
from .views import PollViewSet, QuestionViewSet, PollQuestionViewSet, ResponseViewSet, AnswerViewSet
from .views import PollViewSet, QuestionViewSet, ResponseViewSet, AnswerViewSet
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'polls', PollViewSet, basename='polls')
router.register(r'questions', QuestionViewSet, basename='questions')
router.register(r'poll-questions', PollQuestionViewSet, basename='poll-questions')
router.register(r'responses', ResponseViewSet, basename='responses')
router.register(r'answers', AnswerViewSet, basename='answers')
poll_api_url = router.urls
\ No newline at end of file
from .api import PollViewSet, QuestionViewSet, PollQuestionViewSet, ResponseViewSet, AnswerViewSet
\ No newline at end of file
from .api import PollViewSet, QuestionViewSet, ResponseViewSet, AnswerViewSet
\ No newline at end of file
......@@ -3,9 +3,9 @@ from rest_framework import generics
from rest_framework import viewsets
from rest_framework.viewsets import ModelViewSet
from rest_framework_extensions.mixins import NestedViewSetMixin
from polls.serializers import PollSerializer, QuestionSerializer, PollQuestionSerializer, ResponseSerializer, AnswerSerializer
from polls.serializers import PollSerializer, QuestionSerializer, ResponseSerializer, AnswerSerializer
from api.permissions import IsStaffOrReadOnly
from polls.models import Poll, Question, PollQuestion, Response, Answer
from polls.models import Poll, Question, Response, Answer
from api.mixins import OwnerFieldMixin, DestroyToInvisibleMixin
############################################################
......@@ -30,20 +30,7 @@ class QuestionViewSet(DestroyToInvisibleMixin, OwnerFieldMixin, ModelViewSet):
serializer_class = QuestionSerializer
#Enable filter by code
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('content', )
############################################################
#REST API for relete a question with poll
############################################################
class PollQuestionViewSet(DestroyToInvisibleMixin, ModelViewSet):
#Everyone can read, just staff user can modificate
permission_classes = (IsStaffOrReadOnly,)
queryset = PollQuestion.objects.filter(visible=True)
serializer_class = PollQuestionSerializer
#Enable filter by code
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('poll', 'question')
filterset_fields = ('poll', 'type')
############################################################
#REST API for response
......
......@@ -24,4 +24,5 @@ drf-extensions==0.5.0
django-cors-headers==3.1.1
django-allauth==0.41.0
django-rest-auth==0.9.5
django-extra-fields==2.0.3
\ No newline at end of file
django-extra-fields==2.0.3
pymbtiles==0.5.0
\ 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