forked from wafflestudio/seminar-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
34 lines (25 loc) · 1.23 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from django.shortcuts import get_object_or_404
from rest_framework import status, viewsets
from rest_framework.response import Response
from survey.serializers import OperatingSystemSerializer, SurveyResultSerializer
from survey.models import OperatingSystem, SurveyResult
class SurveyResultViewSet(viewsets.GenericViewSet):
queryset = SurveyResult.objects.all()
serializer_class = SurveyResultSerializer
def list(self, request):
surveys = self.get_queryset().select_related('os')
return Response(self.get_serializer(surveys, many=True).data)
def retrieve(self, request, pk=None):
survey = get_object_or_404(SurveyResult, pk=pk)
return Response(self.get_serializer(survey).data)
class OperatingSystemViewSet(viewsets.GenericViewSet):
queryset = OperatingSystem.objects.all()
serializer_class = OperatingSystemSerializer
def list(self, request):
return Response(self.get_serializer(self.get_queryset(), many=True).data)
def retrieve(self, request, pk=None):
try:
os = OperatingSystem.objects.get(id=pk)
except OperatingSystem.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
return Response(self.get_serializer(os).data)