From bc12a30f1cb506a02123013f114231294911c47d Mon Sep 17 00:00:00 2001 From: d0c Date: Mon, 23 Jan 2023 17:30:13 +0100 Subject: [PATCH] adjustment --- mysuitablephone/settings.py | 2 +- .../management/commands/fetchphones.py | 24 ++++++++++++------- suitablephones/models.py | 2 +- suitablephones/serializers.py | 1 + 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/mysuitablephone/settings.py b/mysuitablephone/settings.py index 32f634d..7d467d5 100644 --- a/mysuitablephone/settings.py +++ b/mysuitablephone/settings.py @@ -139,4 +139,4 @@ REST_FRAMEWORK = { 'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'] } -LINEAGEWIKI = '/home/d0c/Code/lineage_wiki' +LINEAGEWIKI = '/home/francesco/Code/lineage_wiki' diff --git a/suitablephones/management/commands/fetchphones.py b/suitablephones/management/commands/fetchphones.py index 33a1f5a..e58059c 100644 --- a/suitablephones/management/commands/fetchphones.py +++ b/suitablephones/management/commands/fetchphones.py @@ -39,13 +39,17 @@ class Command(BaseCommand): devicesdir = settings.LINEAGEWIKI + "/_data/devices/" # import pdb; pdb.set_trace() for filename in os.listdir(devicesdir): - + dev = Device() + cameras = [] + bluetooth = {} + peripherals = [] + with open(os.path.join(devicesdir, filename), "r") as stream: try: data = yaml.safe_load(stream) print(data['codename']) - dev = Device() for key, value in data.items(): + # if key == "architecture": # if type(value) == dict(): # setattr(dev, key, value) @@ -66,16 +70,14 @@ class Command(BaseCommand): if not hasattr(value, 'profiles'): setattr(bt, 'profiles', None) setattr(bt, subkey, subvalue) - bt.save() + bt.save() else: bt = bt.get(spec=value['spec'], profiles=value['profiles']) - dev.bluetooth = bt - dev.save() + bluetooth = bt elif key == "cameras": - dev.save() for c in value: cam = set_camera(c) - dev.cameras.add(cam) + cameras.append(cam) elif key == "peripherals": if str(value) == "None": value = None @@ -84,8 +86,12 @@ class Command(BaseCommand): value = None else: setattr(dev, key, value) - dev.save() except yaml.YAMLError as exc: print(exc) - stream.close() + stream.close() + dev.save() + dev.bluetooth = bluetooth + for camera in cameras: + dev.cameras.add(cam) + dev.save() \ No newline at end of file diff --git a/suitablephones/models.py b/suitablephones/models.py index de320a5..a94358d 100644 --- a/suitablephones/models.py +++ b/suitablephones/models.py @@ -20,7 +20,7 @@ class Camera(models.Model): info = models.CharField(max_length=100, null=True) def __str__(self): - return f" {self.flash - self.megapixel - self.info}" + return f" {self.flash} - {str(self.megapixel)} - {self.info}" class Device(models.Model): diff --git a/suitablephones/serializers.py b/suitablephones/serializers.py index 04dc939..02decc6 100644 --- a/suitablephones/serializers.py +++ b/suitablephones/serializers.py @@ -15,6 +15,7 @@ class BluetoothSerializer(serializers.ModelSerializer): exclude = [] + class DeviceSerializer(serializers.ModelSerializer): bluetooth = BluetoothSerializer(many=False, read_only=True) cameras = CameraSerializer(many=True, read_only=True)