forked from wafflestudio/seminar-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
52 lines (37 loc) · 1.67 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
from django.http import JsonResponse, HttpResponseNotAllowed, HttpResponseNotFound, HttpResponseBadRequest
from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_http_methods
from survey.models import SurveyResult, OperatingSystem
from survey.serializers import serialize_survey_result, serialize_os
def get_survey_results(request):
if request.method == 'GET':
if param := request.GET.get('os'):
# os validation
try:
queryset = SurveyResult.objects.filter(os__name=param)
except OperatingSystem.DoesNotExist:
return HttpResponseBadRequest("Bad Request!")
else:
queryset = SurveyResult.objects.all()
survey_results = list(map(lambda result: serialize_survey_result(result), queryset))
return JsonResponse({"surveys": survey_results}, status=200)
else:
return HttpResponseNotAllowed(['GET', ])
def get_survey(request, survey_id):
if request.method == 'GET':
survey = get_object_or_404(SurveyResult, id=survey_id)
return JsonResponse(serialize_survey_result(survey))
else:
return HttpResponseNotAllowed(['GET', ])
@require_http_methods(['GET', ])
def get_os_results(request):
objs = OperatingSystem.objects.all()
result = list(map(lambda x: serialize_os(x), objs))
return JsonResponse({"os": result})
def get_os(request, operatingsystem_id):
try:
obj = OperatingSystem.objects.get(id=operatingsystem_id)
except OperatingSystem.DoesNotExist:
return HttpResponseNotFound("검색하신 os가 없어요.")
return JsonResponse(serialize_os(obj))