suitablephones/suitablephones/views.py

53 lines
No EOL
2 KiB
Python

import logging
from decimal import Decimal
from rest_framework import viewsets
from suitablephones.models import Bluetooth, Camera, Device
from suitablephones.serializers import (BluetoothSerializer, CameraSerializer,
DeviceSerializer)
# ViewSets define the view behavior.
class DeviceViewSet(viewsets.ModelViewSet):
queryset = Device.objects.all()
serializer_class = DeviceSerializer
#http://url/api/devices?megapixel=3
def get_queryset(self):
name = self.request.query_params.get('name')
megapixel = self.request.query_params.get('megapixel')
size_in = self.request.query_params.get('size_in')
resolution = self.request.query_params.get('resolution')
screen_type = self.request.query_params.get('screen_type')
#maintainers = self.request.query_params.getlist('maintainers')
version = self.request.query_params.get('version')
if name:
self.queryset = self.queryset.filter(name__icontains=name)
if megapixel:
self.queryset = self.queryset.filter(cameras__megapixel__gt=Decimal(megapixel))
if version:
self.queryset = self.queryset.filter(versions__icontains=version)
if size_in:
self.queryset = self.queryset.filter(screen__size_in__gt=Decimal(size_in))
if resolution:
self.queryset = self.queryset.filter(screen__resolution__icontains=resolution)
if screen_type:
self.queryset = self.queryset.filter(screen__technology__icontains=screen_type)
#if maintainers:
self.queryset = self.queryset.filter(maintainers__len__gt=0)
return self.queryset
def get_cameras(self):
return self.request.GET.getlist("cameras")
class BluetoothViewSet(viewsets.ModelViewSet):
queryset = Bluetooth.objects.all()
serializer_class = BluetoothSerializer
class CameraViewSet(viewsets.ModelViewSet):
queryset = Camera.objects.all()
serializer_class = CameraSerializer