diff --git a/devices/serializers.py b/devices/serializers.py new file mode 100644 index 0000000..c3d35ea --- /dev/null +++ b/devices/serializers.py @@ -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 \ No newline at end of file diff --git a/devices/urls.py b/devices/urls.py new file mode 100644 index 0000000..f7bc31d --- /dev/null +++ b/devices/urls.py @@ -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 \ No newline at end of file diff --git a/devices/views.py b/devices/views.py index 91ea44a..ef06d5e 100644 --- a/devices/views.py +++ b/devices/views.py @@ -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 diff --git a/measures/serializers.py b/measures/serializers.py new file mode 100644 index 0000000..f51a330 --- /dev/null +++ b/measures/serializers.py @@ -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 \ No newline at end of file diff --git a/measures/urls.py b/measures/urls.py new file mode 100644 index 0000000..4ab06fd --- /dev/null +++ b/measures/urls.py @@ -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 \ No newline at end of file diff --git a/measures/views.py b/measures/views.py index 91ea44a..6a41681 100644 --- a/measures/views.py +++ b/measures/views.py @@ -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 \ No newline at end of file diff --git a/openred/settings.py b/openred/settings.py index 84aa572..fd32dbd 100644 --- a/openred/settings.py +++ b/openred/settings.py @@ -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 @@ -30,6 +31,8 @@ SITE_ID = 1 +APPS_DIRS = True + # Application definition @@ -47,9 +50,15 @@ 'allauth.socialaccount', 'allauth.socialaccount.providers.google', + # Other apps + 'rest_framework', + 'drf_yasg', + # Custom apps 'devices', 'measures', + + ] MIDDLEWARE = [ @@ -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': [ diff --git a/openred/urls.py b/openred/urls.py index 2578835..e979060 100644 --- a/openred/urls.py +++ b/openred/urls.py @@ -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="frasanz@ibercivis.es"), + 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'), ]