search and architecture

This commit is contained in:
d0c 2023-03-30 17:47:53 +02:00
parent 58f19486e8
commit ca5c5b93f4
5 changed files with 29 additions and 14 deletions

7
start.sh Executable file
View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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.