added weather endpoint for mikrocontroller
This commit is contained in:
parent
180b71c0f0
commit
b3db1ca78f
@ -66,7 +66,20 @@ SPECTACULAR_SETTINGS = {
|
|||||||
'TITLE': 'Weather Mikro API',
|
'TITLE': 'Weather Mikro API',
|
||||||
'DESCRIPTION': 'Weather stats by weather',
|
'DESCRIPTION': 'Weather stats by weather',
|
||||||
'VERSION': '1.0.0',
|
'VERSION': '1.0.0',
|
||||||
'COMPONENT_SPLIT_REQUEST': True
|
'COMPONENT_SPLIT_REQUEST': True,
|
||||||
|
'SECURITY': [{
|
||||||
|
'Mikro-Key': []
|
||||||
|
}],
|
||||||
|
'COMPONENTS': {
|
||||||
|
'securitySchemes': {
|
||||||
|
'Mikro-Key': {
|
||||||
|
'type': 'apiKey',
|
||||||
|
'in': 'header',
|
||||||
|
'name': 'X-Mikro-Key',
|
||||||
|
'description': 'Secret key for mikrocontroller'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
CORS_ALLOW_ALL_ORIGINS = True
|
CORS_ALLOW_ALL_ORIGINS = True
|
||||||
|
|||||||
@ -24,6 +24,6 @@ urlpatterns = [
|
|||||||
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
|
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
|
||||||
path('swagger/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
|
path('swagger/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
|
||||||
path('api/', include([
|
path('api/', include([
|
||||||
# path('', include('weather.urls')),
|
path('', include('weather.urls')),
|
||||||
])),
|
])),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,3 +1,9 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from .models import WeatherStats
|
||||||
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
|
class WeatherStatsAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('temperature', 'humidityAir', 'humidityGround', 'light', 'created_at')
|
||||||
|
readonly_fields = ('created_at',)
|
||||||
|
|
||||||
|
admin.site.register(WeatherStats, WeatherStatsAdmin)
|
||||||
@ -9,4 +9,4 @@ class WeatherStats(models.Model):
|
|||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Weather data by ' + self.created_at
|
return 'Weather data by ' + str(self.created_at)
|
||||||
9
weather/serializers.py
Normal file
9
weather/serializers.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from .models import WeatherStats
|
||||||
|
|
||||||
|
class WeatherStatSerializer(serializers.ModelSerializer):
|
||||||
|
created_at = serializers.DateTimeField(read_only=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = WeatherStats
|
||||||
|
fields = ('humidityAir', 'humidityGround', 'temperature', 'light', 'created_at')
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
from django.urls import path
|
||||||
|
from .views import CreateStatAPI
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('stats/', CreateStatAPI.as_view())
|
||||||
|
]
|
||||||
11
weather/utils.py
Normal file
11
weather/utils.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import os
|
||||||
|
from rest_framework.request import Request
|
||||||
|
from rest_framework import permissions
|
||||||
|
|
||||||
|
class IsMikrokontroller(permissions.BasePermission):
|
||||||
|
|
||||||
|
def has_permission(self, request: Request, view):
|
||||||
|
mikro_key = os.environ['MIKRO_SECRET_KEY']
|
||||||
|
request_key = request.headers.get('X-Mikro-Key')
|
||||||
|
|
||||||
|
return request_key == mikro_key
|
||||||
@ -1,3 +1,24 @@
|
|||||||
from django.shortcuts import render
|
from rest_framework.generics import CreateAPIView
|
||||||
|
from drf_spectacular.utils import extend_schema, OpenApiParameter
|
||||||
|
|
||||||
|
from .models import WeatherStats
|
||||||
|
from .serializers import WeatherStatSerializer
|
||||||
|
from .utils import IsMikrokontroller
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
@extend_schema(tags=['Weather'],
|
||||||
|
description="Call method by mikrocontroller to set new data (required MIKRO_SECRET_KEY with header X-Mikro-Key)",
|
||||||
|
parameters=[
|
||||||
|
OpenApiParameter(
|
||||||
|
name='X-Mikro-Key',
|
||||||
|
type=str,
|
||||||
|
location=OpenApiParameter.HEADER,
|
||||||
|
description='Secret Key for microcontroller',
|
||||||
|
required=True
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
class CreateStatAPI(CreateAPIView):
|
||||||
|
serializer_class = WeatherStatSerializer
|
||||||
|
permission_classes = [ IsMikrokontroller ]
|
||||||
|
queryset = WeatherStats.objects.all()
|
||||||
Loading…
x
Reference in New Issue
Block a user