From ca5c5b93f4786e698be62a10b4c8a95a072ae098 Mon Sep 17 00:00:00 2001 From: d0c Date: Thu, 30 Mar 2023 17:47:53 +0200 Subject: [PATCH] search and architecture --- start.sh | 7 +++++++ .../management/commands/fetchphones.py | 15 ++++++--------- suitablephones/models.py | 2 +- suitablephones/urls.py | 3 ++- suitablephones/views.py | 16 +++++++++++++--- 5 files changed, 29 insertions(+), 14 deletions(-) create mode 100755 start.sh diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..8601b0b --- /dev/null +++ b/start.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +source bin/activate +pip install -r requirements.txt +docker compose up -d +python manage.py migrate +python manage.py runserver diff --git a/suitablephones/management/commands/fetchphones.py b/suitablephones/management/commands/fetchphones.py index dc43b2b..af1a075 100644 --- a/suitablephones/management/commands/fetchphones.py +++ b/suitablephones/management/commands/fetchphones.py @@ -81,15 +81,12 @@ class Command(BaseCommand): data = yaml.safe_load(stream) print(data['codename']) for key, value in data.items(): - - # if key == "architecture": - # if type(value) == dict(): - # setattr(dev, key, value) - # else: #type(value) == str(): - # setattr(dev, key, dict({ 'cpu' : value })) - #match caso: - # case dict(): - # print('dict') + if key == "architecture": + if type(value) == str: + setattr(dev, key, dict({ 'cpu' : value })) + else: + for k,v in value.items(): + setattr(dev, key, dict({ k : v })) if key == "bluetooth": if not 'profiles' in value.keys(): value['profiles'] = None diff --git a/suitablephones/models.py b/suitablephones/models.py index db0d388..c289a33 100644 --- a/suitablephones/models.py +++ b/suitablephones/models.py @@ -34,7 +34,7 @@ class Screen(models.Model): return f"{str(self.size_in)} - {str(self.density)} - {str(self.technology)}" class Device(models.Model): - #architecture = models.JSONField(default=dict) + architecture = models.JSONField(default=dict) #battery = models.JSONField(default=dict) #before_install = models.JSONField(default=dict, null=True) # dict or str bluetooth = models.ForeignKey(Bluetooth, on_delete=models.CASCADE, null=True) diff --git a/suitablephones/urls.py b/suitablephones/urls.py index 7067aa0..e1ef8e1 100644 --- a/suitablephones/urls.py +++ b/suitablephones/urls.py @@ -1,9 +1,10 @@ from django.urls import include, path from rest_framework import routers -from suitablephones.views import BluetoothViewSet, CameraViewSet, DeviceViewSet, SingleDeviceViewSet +from suitablephones.views import BluetoothViewSet, CameraViewSet, DeviceViewSet, SingleDeviceViewSet, SearchDeviceViewSet router = routers.DefaultRouter(trailing_slash=False) +router.register(r'search', SearchDeviceViewSet) router.register(r'device', SingleDeviceViewSet) router.register(r'devices', DeviceViewSet) router.register(r'bluetooth', BluetoothViewSet) diff --git a/suitablephones/views.py b/suitablephones/views.py index 4acaae8..254efe0 100644 --- a/suitablephones/views.py +++ b/suitablephones/views.py @@ -7,14 +7,24 @@ from suitablephones.models import Bluetooth, Camera, Device from suitablephones.serializers import (BluetoothSerializer, CameraSerializer, DeviceSerializer, DeviceSerializerShort) +class SearchDeviceViewSet(viewsets.ModelViewSet): + queryset = Device.objects.all() + serializer_class = DeviceSerializer + + def get_queryset(self): + query = self.request.query_params.get('q') + if query: + self.queryset = self.queryset.filter(codename__icontains=query) | self.queryset.filter(name__icontains=query) | self.queryset.filter(vendor__icontains=query) + return self.queryset + class SingleDeviceViewSet(viewsets.ModelViewSet): queryset = Device.objects.all() serializer_class = DeviceSerializer def get_queryset(self): - params = self.request.query_params.get('search') - if params: - self.queryset = self.queryset.filter(codename__icontains=params) | self.queryset.filter(name__icontains=params) | self.queryset.filter(vendor__icontains=params) + codename = self.request.query_params.get('codename') + if codename: + self.queryset = self.queryset.filter(codename__icontains=codename) return self.queryset # ViewSets define the view behavior.