Browse Source

first working

Matteo Parrucci 1 year ago
parent
commit
2a78fa8262
6 changed files with 41 additions and 9 deletions
  1. 1 1
      Readme.md
  2. 1 1
      mysuitablephone/urls.py
  3. 10 2
      suitablephones/models.py
  4. 10 0
      suitablephones/serializers.py
  5. 9 2
      suitablephones/urls.py
  6. 10 3
      suitablephones/views.py

+ 1 - 1
Readme.md

@@ -24,4 +24,4 @@ virtualenv .
 bin/pip install -r requirements.txt
 bin/python manage.py migrate
 bin/python manage.py createsuperuser
-bin/python manage.py startserver
+bin/python manage.py runserver

+ 1 - 1
mysuitablephone/urls.py

@@ -17,7 +17,7 @@ from django.contrib import admin
 from django.urls import include, path
 
 urlpatterns = [
-    path('api-auth/', include('rest_framework.urls')),
+    path('', include('suitablephones.urls')),
     path('admin/', admin.site.urls),
 ]
 

+ 10 - 2
suitablephones/models.py

@@ -7,6 +7,9 @@ class Bluetooth(models.Model):
     spec = models.CharField(max_length=100)
     profile = models.CharField(max_length=100)
 
+    def __str__(self):
+       return f"Bluetooth:  {self.spec}"
+
 
 class Camera(models.Model):
     class FLASHES(models.TextChoices):
@@ -15,6 +18,9 @@ class Camera(models.Model):
 
     megapixel = models.DecimalField(max_digits=5, decimal_places=1)
     flash = models.CharField(max_length=100, choices=FLASHES.choices)
+
+    def __str__(self):
+       return f"Camera:  {self.megapixel} - {self.flash}"
                
 #Devices.object.filter(flash=Camera.FLASEHS.LED)
 
@@ -53,5 +59,7 @@ class Device(models.Model):
     vendor  = models.CharField(max_length=100)
     vendor_short  = models.CharField(max_length=100)
     #versions  =  <class 'list'>
-    
-    wifi  = models.CharField(max_length=100)
+    wifi  = models.CharField(max_length=100)
+
+    def __str__(self):
+       return 'Device: ' + self.name

+ 10 - 0
suitablephones/serializers.py

@@ -0,0 +1,10 @@
+from rest_framework import serializers
+
+from suitablephones.models import Device
+
+
+# Serializers define the API representation.
+class DeviceSerializer(serializers.HyperlinkedModelSerializer):
+    class Meta:
+        model = Device
+        fields = ['cpu', 'kernel', 'name']

+ 9 - 2
suitablephones/urls.py

@@ -1,7 +1,14 @@
-from django.urls import path
-from suitablephones.views import SuitablePhones
+from django.urls import include, path
+from rest_framework import routers
+
+from suitablephones.views import DeviceViewSet, SuitablePhones
+
+router = routers.DefaultRouter()
+router.register(r'devices', DeviceViewSet)
 
 urlpatterns = [
+    path('api/', include(router.urls)),
+    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
     path('suitablephones/', SuitablePhones.as_view(), name='suitablephones'),
 ]
 

+ 10 - 3
suitablephones/views.py

@@ -1,7 +1,8 @@
-from django.shortcuts import render
-from django.http import HttpResponse
 from django.views.generic.list import ListView
-from suitablephones.models import Device, Camera
+from rest_framework import viewsets
+
+from suitablephones.models import Device
+from suitablephones.serializers import DeviceSerializer
 
 
 class SuitablePhones(ListView):
@@ -14,3 +15,9 @@ class SuitablePhones(ListView):
     def get_queryset(self):
         camera = self.get_request_camera()
         return self.model.objects.filter(camera=camera)
+
+
+# ViewSets define the view behavior.
+class DeviceViewSet(viewsets.ModelViewSet):
+    queryset = Device.objects.all()
+    serializer_class = DeviceSerializer