From 938734f34fb5138c62a87138c47fd2d528f68911 Mon Sep 17 00:00:00 2001 From: Matteo Parrucci Date: Tue, 3 Jan 2023 13:26:28 +0100 Subject: [PATCH] first working filter --- mysuitablephone/settings.py | 7 ++++ suitablephones/fetchphones.py | 23 ----------- .../management/commands/fetchphones.py | 38 +++++++++++++++++++ suitablephones/serializers.py | 23 ++++++----- .../templates/suitablephones/device_list.html | 11 ++++++ suitablephones/urls.py | 4 +- suitablephones/views.py | 26 ++++++------- 7 files changed, 83 insertions(+), 49 deletions(-) delete mode 100644 suitablephones/fetchphones.py create mode 100644 suitablephones/management/commands/fetchphones.py create mode 100644 suitablephones/templates/suitablephones/device_list.html diff --git a/mysuitablephone/settings.py b/mysuitablephone/settings.py index 69ad4e4..d05937f 100644 --- a/mysuitablephone/settings.py +++ b/mysuitablephone/settings.py @@ -37,6 +37,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'django_filters', 'rest_framework', 'suitablephones', ] @@ -123,3 +124,9 @@ STATIC_URL = 'static/' # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +REST_FRAMEWORK = { + 'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'] +} + +LINEAGEWIKI = '/home/d0c/Code/lineage_wiki' \ No newline at end of file diff --git a/suitablephones/fetchphones.py b/suitablephones/fetchphones.py deleted file mode 100644 index f06aae9..0000000 --- a/suitablephones/fetchphones.py +++ /dev/null @@ -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() -# diff --git a/suitablephones/management/commands/fetchphones.py b/suitablephones/management/commands/fetchphones.py new file mode 100644 index 0000000..839ae51 --- /dev/null +++ b/suitablephones/management/commands/fetchphones.py @@ -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) \ No newline at end of file diff --git a/suitablephones/serializers.py b/suitablephones/serializers.py index 0f06ccf..04dc939 100644 --- a/suitablephones/serializers.py +++ b/suitablephones/serializers.py @@ -3,19 +3,22 @@ from rest_framework import serializers from suitablephones.models import Bluetooth, Camera, Device -class DeviceSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = Device - exclude = [] - - -class CameraSerializer(serializers.HyperlinkedModelSerializer): +class CameraSerializer(serializers.ModelSerializer): class Meta: model = Camera + exclude = ["id"] + + +class BluetoothSerializer(serializers.ModelSerializer): + class Meta: + model = Bluetooth 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: - model = Bluetooth - exclude = [] \ No newline at end of file + model = Device + exclude = ["id"] \ No newline at end of file diff --git a/suitablephones/templates/suitablephones/device_list.html b/suitablephones/templates/suitablephones/device_list.html new file mode 100644 index 0000000..669093e --- /dev/null +++ b/suitablephones/templates/suitablephones/device_list.html @@ -0,0 +1,11 @@ + + +{% for device in device_list %} +

{{ device.name }}

+

Cameras

+ {% for camera in device.cameras.all %} +
Camera with {{ camera.megapixel }} MP
+ {% endfor %} +{% endfor %} + + \ No newline at end of file diff --git a/suitablephones/urls.py b/suitablephones/urls.py index c077360..283581b 100644 --- a/suitablephones/urls.py +++ b/suitablephones/urls.py @@ -1,8 +1,7 @@ from django.urls import include, path from rest_framework import routers -from suitablephones.views import (BluetoothViewSet, CameraViewSet, - DeviceViewSet, SuitablePhones) +from suitablephones.views import BluetoothViewSet, CameraViewSet, DeviceViewSet router = routers.DefaultRouter() router.register(r'devices', DeviceViewSet) @@ -12,6 +11,5 @@ router.register(r'camera', CameraViewSet) urlpatterns = [ path('api/', include(router.urls)), path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), - path('suitablephones/', SuitablePhones.as_view(), name='suitablephones'), ] diff --git a/suitablephones/views.py b/suitablephones/views.py index 033caeb..0a791f6 100644 --- a/suitablephones/views.py +++ b/suitablephones/views.py @@ -1,4 +1,6 @@ -from django.views.generic.list import ListView +import logging +from decimal import Decimal + from rest_framework import viewsets from suitablephones.models import Bluetooth, Camera, Device @@ -6,23 +8,21 @@ from suitablephones.serializers import (BluetoothSerializer, CameraSerializer, 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. class DeviceViewSet(viewsets.ModelViewSet): queryset = Device.objects.all() 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): queryset = Bluetooth.objects.all()