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 %} +