Skip to content

Commit

Permalink
Added serializers and urls for devices and measures
Browse files Browse the repository at this point in the history
and API (+ swagger + redoc) for devices and measures
  • Loading branch information
frasanz committed Oct 3, 2024
1 parent c663f0c commit 7880f58
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 1 deletion.
13 changes: 13 additions & 0 deletions devices/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# devices/serializers.py
from rest_framework import serializers
from .models import DeviceModel, Device

class DeviceModelSerializer(serializers.ModelSerializer):
class Meta:
model = DeviceModel
fields = '__all__' # This will include all fields, or specify the fields you want

class DeviceSerializer(serializers.ModelSerializer):
class Meta:
model = Device
fields = '__all__' # Include all fields, or specify the fields you want
9 changes: 9 additions & 0 deletions devices/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# devices/urls.py
from rest_framework.routers import DefaultRouter
from .views import DeviceModelViewSet, DeviceViewSet

router = DefaultRouter()
router.register(r'device-models', DeviceModelViewSet, basename='device-model')
router.register(r'devices', DeviceViewSet, basename='device')

urlpatterns = router.urls
11 changes: 11 additions & 0 deletions devices/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
from django.shortcuts import render
from rest_framework import viewsets
from .models import DeviceModel, Device
from .serializers import DeviceModelSerializer, DeviceSerializer

# Create your views here.

class DeviceModelViewSet(viewsets.ModelViewSet):
queryset = DeviceModel.objects.all()
serializer_class = DeviceModelSerializer

class DeviceViewSet(viewsets.ModelViewSet):
queryset = Device.objects.all()
serializer_class = DeviceSerializer
13 changes: 13 additions & 0 deletions measures/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# measures/serializers.py
from rest_framework import serializers
from .models import Measurement
from devices.models import Device # Assuming Device is imported correctly
from django.contrib.auth.models import User

class MeasurementSerializer(serializers.ModelSerializer):
device = serializers.PrimaryKeyRelatedField(queryset=Device.objects.all()) # Represent the device with its ID
user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(), allow_null=True) # User can be null

class Meta:
model = Measurement
fields = '__all__' # You can specify the fields instead of using '__all__' if you want more control
7 changes: 7 additions & 0 deletions measures/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from rest_framework.routers import DefaultRouter
from .views import MeasurementViewSet

router = DefaultRouter()
router.register(r'measurements', MeasurementViewSet, basename='measurement')

urlpatterns = router.urls
8 changes: 8 additions & 0 deletions measures/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
from django.shortcuts import render
from rest_framework import viewsets
from .models import Measurement
from .serializers import MeasurementSerializer

# Create your views here.


class MeasurementViewSet(viewsets.ModelViewSet):
queryset = Measurement.objects.all()
serializer_class = MeasurementSerializer
11 changes: 10 additions & 1 deletion openred/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from pathlib import Path
from decouple import config
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -30,6 +31,8 @@

SITE_ID = 1

APPS_DIRS = True


# Application definition

Expand All @@ -47,9 +50,15 @@
'allauth.socialaccount',
'allauth.socialaccount.providers.google',

# Other apps
'rest_framework',
'drf_yasg',

# Custom apps
'devices',
'measures',


]

MIDDLEWARE = [
Expand All @@ -70,7 +79,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand Down
20 changes: 20 additions & 0 deletions openred/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,28 @@
"""
from django.contrib import admin
from django.urls import path, include
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
openapi.Info(
title="OpenRed API",
default_version='v1',
description="API documentation for managing devices and measurements",
terms_of_service="https://www.example.com/terms/",
contact=openapi.Contact(email="[email protected]"),
license=openapi.License(name="EUPL 1.2"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
path('api/', include('devices.urls')),
path('api/', include('measures.urls')),
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]

0 comments on commit 7880f58

Please sign in to comment.