first working filter
This commit is contained in:
parent
398f1d72c5
commit
938734f34f
7 changed files with 83 additions and 49 deletions
|
@ -37,6 +37,7 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'django_filters',
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
'suitablephones',
|
'suitablephones',
|
||||||
]
|
]
|
||||||
|
@ -123,3 +124,9 @@ STATIC_URL = 'static/'
|
||||||
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
|
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
|
||||||
|
|
||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
|
REST_FRAMEWORK = {
|
||||||
|
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
|
||||||
|
}
|
||||||
|
|
||||||
|
LINEAGEWIKI = '/home/d0c/Code/lineage_wiki'
|
|
@ -1,23 +0,0 @@
|
||||||
import yaml
|
|
||||||
import json
|
|
||||||
|
|
||||||
LINEAGEWIKI = '/home/d0c/Code/lineage_wiki'
|
|
||||||
|
|
||||||
with open(LINEAGEWIKI+"/_data/devices/surnia.yml", "r") as stream:
|
|
||||||
try:
|
|
||||||
data=yaml.safe_load(stream)
|
|
||||||
#print(type(data))
|
|
||||||
for i in data.keys():
|
|
||||||
value=data[i]
|
|
||||||
print(i, " = ", type(value))
|
|
||||||
# if isinstance(value, (list, dict)):
|
|
||||||
# print(value)
|
|
||||||
##print(json.dumps(data, sort_keys=True, indent=4))
|
|
||||||
except yaml.YAMLError as exc:
|
|
||||||
print(exc)
|
|
||||||
|
|
||||||
## create instance of model
|
|
||||||
# m = MyModel(**data_dict)
|
|
||||||
# don't forget to save to database!
|
|
||||||
# m.save()
|
|
||||||
#
|
|
38
suitablephones/management/commands/fetchphones.py
Normal file
38
suitablephones/management/commands/fetchphones.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
|
|
||||||
|
from suitablephones.models import Bluetooth, Camera, Device
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = 'Closes the specified poll for voting'
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument('poll_ids', nargs='+', type=int)
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
Device.objects.all().delete()
|
||||||
|
Bluetooth.objects.all().delete()
|
||||||
|
Camera.objects.all().delete()
|
||||||
|
for filename in os.listdir(settings.LINEAGEWIKI + "/_data/devices/"):
|
||||||
|
with open(filename, "r") as stream:
|
||||||
|
try:
|
||||||
|
data = yaml.safe_load(stream)
|
||||||
|
dev = Device()
|
||||||
|
for key, value in data.items():
|
||||||
|
if key == "cameras":
|
||||||
|
cam = Camera.objects.filter(*value.items())
|
||||||
|
if not cam.exists():
|
||||||
|
cam = Camera()
|
||||||
|
for subkey, subvalue in value.items():
|
||||||
|
cam
|
||||||
|
for subkey in value.items():
|
||||||
|
setattr(cam, subkey, subvalue)
|
||||||
|
else:
|
||||||
|
setattr(dev, key, value)
|
||||||
|
dev.save()
|
||||||
|
except yaml.YAMLError as exc:
|
||||||
|
print(exc)
|
|
@ -3,19 +3,22 @@ from rest_framework import serializers
|
||||||
from suitablephones.models import Bluetooth, Camera, Device
|
from suitablephones.models import Bluetooth, Camera, Device
|
||||||
|
|
||||||
|
|
||||||
class DeviceSerializer(serializers.HyperlinkedModelSerializer):
|
class CameraSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
|
||||||
model = Device
|
|
||||||
exclude = []
|
|
||||||
|
|
||||||
|
|
||||||
class CameraSerializer(serializers.HyperlinkedModelSerializer):
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Camera
|
model = Camera
|
||||||
|
exclude = ["id"]
|
||||||
|
|
||||||
|
|
||||||
|
class BluetoothSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Bluetooth
|
||||||
exclude = []
|
exclude = []
|
||||||
|
|
||||||
|
|
||||||
class BluetoothSerializer(serializers.HyperlinkedModelSerializer):
|
class DeviceSerializer(serializers.ModelSerializer):
|
||||||
|
bluetooth = BluetoothSerializer(many=False, read_only=True)
|
||||||
|
cameras = CameraSerializer(many=True, read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Bluetooth
|
model = Device
|
||||||
exclude = []
|
exclude = ["id"]
|
11
suitablephones/templates/suitablephones/device_list.html
Normal file
11
suitablephones/templates/suitablephones/device_list.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
{% for device in device_list %}
|
||||||
|
<h1>{{ device.name }}</h1>
|
||||||
|
<h2>Cameras</h2>
|
||||||
|
{% for camera in device.cameras.all %}
|
||||||
|
<div>Camera with {{ camera.megapixel }} MP</div>
|
||||||
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -1,8 +1,7 @@
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
|
|
||||||
from suitablephones.views import (BluetoothViewSet, CameraViewSet,
|
from suitablephones.views import BluetoothViewSet, CameraViewSet, DeviceViewSet
|
||||||
DeviceViewSet, SuitablePhones)
|
|
||||||
|
|
||||||
router = routers.DefaultRouter()
|
router = routers.DefaultRouter()
|
||||||
router.register(r'devices', DeviceViewSet)
|
router.register(r'devices', DeviceViewSet)
|
||||||
|
@ -12,6 +11,5 @@ router.register(r'camera', CameraViewSet)
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/', include(router.urls)),
|
path('api/', include(router.urls)),
|
||||||
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
||||||
path('suitablephones/', SuitablePhones.as_view(), name='suitablephones'),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
from django.views.generic.list import ListView
|
import logging
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
|
|
||||||
from suitablephones.models import Bluetooth, Camera, Device
|
from suitablephones.models import Bluetooth, Camera, Device
|
||||||
|
@ -6,23 +8,21 @@ from suitablephones.serializers import (BluetoothSerializer, CameraSerializer,
|
||||||
DeviceSerializer)
|
DeviceSerializer)
|
||||||
|
|
||||||
|
|
||||||
class SuitablePhones(ListView):
|
|
||||||
model = Device
|
|
||||||
|
|
||||||
def get_request_camera(self):
|
|
||||||
return self.request.GET.get("camera")
|
|
||||||
|
|
||||||
|
|
||||||
def get_queryset(self):
|
|
||||||
camera = self.get_request_camera()
|
|
||||||
return self.model.objects.filter(camera=camera)
|
|
||||||
|
|
||||||
|
|
||||||
# ViewSets define the view behavior.
|
# ViewSets define the view behavior.
|
||||||
class DeviceViewSet(viewsets.ModelViewSet):
|
class DeviceViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Device.objects.all()
|
queryset = Device.objects.all()
|
||||||
serializer_class = DeviceSerializer
|
serializer_class = DeviceSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
megapixel = self.request.query_params.get('megapixel')
|
||||||
|
if megapixel:
|
||||||
|
self.queryset = self.queryset.filter(cameras__megapixel__lt=Decimal(megapixel))
|
||||||
|
return self.queryset
|
||||||
|
|
||||||
|
|
||||||
|
def get_cameras(self):
|
||||||
|
return self.request.GET.getlist("cameras")
|
||||||
|
|
||||||
|
|
||||||
class BluetoothViewSet(viewsets.ModelViewSet):
|
class BluetoothViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Bluetooth.objects.all()
|
queryset = Bluetooth.objects.all()
|
||||||
|
|
Loading…
Reference in a new issue