-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added serializers and urls for devices and measures
and API (+ swagger + redoc) for devices and measures
- Loading branch information
Showing
8 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'), | ||
] |