From e2c79cf698b80c9ac3f5353bb0b2e10bfaaf82d2 Mon Sep 17 00:00:00 2001 From: d0c Date: Sun, 5 Feb 2023 17:42:15 +0100 Subject: [PATCH] screen model with search to fix --- suitablephones/admin.py | 6 ++- .../management/commands/fetchphones.py | 40 ++++++++++++++++++- suitablephones/models.py | 12 +++++- suitablephones/views.py | 11 ++++- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/suitablephones/admin.py b/suitablephones/admin.py index 64bb077..a46da4b 100644 --- a/suitablephones/admin.py +++ b/suitablephones/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from suitablephones.models import Bluetooth, Camera, Device +from suitablephones.models import Bluetooth, Camera, Device, Screen @admin.register(Camera) @@ -16,3 +16,7 @@ class AdminBluetooth(admin.ModelAdmin): @admin.register(Device) class AdminDevice(admin.ModelAdmin): pass + +@admin.register(Screen) +class AdminScreen(admin.ModelAdmin): + pass diff --git a/suitablephones/management/commands/fetchphones.py b/suitablephones/management/commands/fetchphones.py index 5288e82..27fc5b9 100644 --- a/suitablephones/management/commands/fetchphones.py +++ b/suitablephones/management/commands/fetchphones.py @@ -4,10 +4,39 @@ import yaml from django.conf import settings from django.core.management.base import BaseCommand, CommandError -from suitablephones.models import Bluetooth, Camera, Device +from suitablephones.models import Bluetooth, Camera, Device, Screen +import subprocess import logging + +def update_repo(): + devicesdir = settings.LINEAGEWIKI + pull_status = subprocess.run(["git", "pull"], cwd=devicesdir) + if pull_status.returncode != 0: + logging.error("Error pulling repo") + +def set_screen(args): + #if type(args) + screen = Screen.objects.filter(**args) + if not screen.exists(): + screen = Screen() + for subkey, subvalue in args.items(): + if subkey == 'size': + size_mm = subvalue.split(' ')[0] + setattr(screen, 'size_mm', size_mm) + size_inch = subvalue.split(' ')[2].replace('(', '') + setattr(screen, 'size_in', size_inch) + setattr(screen, subkey, subvalue) + #elif subkey == 'technology': + # setattr(screen, 'type', subvalue) + setattr(screen, subkey, subvalue) + screen.save() + else: + screen = screen.get() + return screen + + def set_camera(args): cam = Camera.objects.filter(**args) if not cam.exists(): @@ -36,12 +65,14 @@ class Command(BaseCommand): Device.objects.all().delete() Bluetooth.objects.all().delete() Camera.objects.all().delete() + update_repo() devicesdir = settings.LINEAGEWIKI + "/_data/devices/" # import pdb; pdb.set_trace() for filename in os.listdir(devicesdir): dev = Device() cameras = [] bluetooth = {} + screen = [] peripherals = [] with open(os.path.join(devicesdir, filename), "r") as stream: @@ -84,6 +115,13 @@ class Command(BaseCommand): elif key == "storage": if str(value) == "None": value = None + elif key == "screen": + if type(value) is list: + for s in value: + for k,v in s.items(): + screen.append(set_screen(v)) + elif type(value) is dict: + screen.append(set_screen(value)) else: setattr(dev, key, value) except yaml.YAMLError as exc: diff --git a/suitablephones/models.py b/suitablephones/models.py index e8c37ef..998645b 100644 --- a/suitablephones/models.py +++ b/suitablephones/models.py @@ -22,6 +22,16 @@ class Camera(models.Model): def __str__(self): return f" {self.flash} - {str(self.megapixel)} - {self.info}" +class Screen(models.Model): + size = models.CharField(max_length=500, null=True) + size_mm = models.DecimalField(max_digits=5, decimal_places=1) + size_in = models.DecimalField(max_digits=5, decimal_places=1) + resolution = models.CharField(max_length=100) + density = models.CharField(max_length=100) + technology = models.CharField(max_length=100) + + def __str__(self): + return f"{self.size_in} - {self.density} - {self.technology}" class Device(models.Model): #architecture = models.JSONField(default=dict) @@ -51,7 +61,7 @@ class Device(models.Model): recovery_boot = models.CharField(max_length=1000) recovery_reboot = models.CharField(max_length=100) #release = models.DateField() - #screen = models.JSONField(default=dict) # dict or str + screen = models.ManyToManyField(Screen) #sdcard = models.JSONField(default=dict) # dict or str{1} soc = models.CharField(max_length=1000) storage = models.CharField(max_length=100, blank=True) diff --git a/suitablephones/views.py b/suitablephones/views.py index b13cb0a..65f0bfd 100644 --- a/suitablephones/views.py +++ b/suitablephones/views.py @@ -17,6 +17,9 @@ class DeviceViewSet(viewsets.ModelViewSet): 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: @@ -24,7 +27,13 @@ class DeviceViewSet(viewsets.ModelViewSet): if megapixel: self.queryset = self.queryset.filter(cameras__megapixel__gt=Decimal(megapixel)) if version: - self.queryset = self.queryset.filter(versions__icontains=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