Fixed create api to save data
This commit is contained in:
parent
4cf4c4661d
commit
35e32c996c
@ -82,4 +82,6 @@ def get_weather_api_data():
|
||||
responses = openmeteo.weather_api(url, params=params)
|
||||
response = responses[0]
|
||||
|
||||
days = response.Daily()
|
||||
|
||||
return response
|
||||
@ -1,3 +1,4 @@
|
||||
import time
|
||||
from django.core.cache import cache
|
||||
from rest_framework.generics import CreateAPIView, ListAPIView
|
||||
from rest_framework.views import APIView
|
||||
@ -10,6 +11,7 @@ from .models import WeatherStats
|
||||
from .serializers import WeatherStatSerializer
|
||||
from .utils import IsMikrokontroller, PageNumberPagination
|
||||
|
||||
CACHE_SAVE_CONTROL = 'stats_latest_saved_control'
|
||||
CACHE_KEY_STATS = 'stats_latest_data'
|
||||
CACHE_KEY_WEATHER = 'weather_latest_data'
|
||||
CACHE_TIMEOUT = 30 # 30 seconds
|
||||
@ -34,10 +36,12 @@ class CreateStatAPI(CreateAPIView):
|
||||
|
||||
def perform_create(self, serializer: WeatherStatSerializer):
|
||||
new_data = serializer.validated_data
|
||||
cached_data = cache.get(CACHE_KEY_STATS)
|
||||
last_saved = cache.get(CACHE_SAVE_CONTROL)
|
||||
current_time = time.time()
|
||||
|
||||
if cached_data is None:
|
||||
if last_saved is None or (current_time - last_saved) >= CACHE_TIMEOUT:
|
||||
serializer.save()
|
||||
cache.set(CACHE_SAVE_CONTROL, current_time, CACHE_TIMEOUT)
|
||||
|
||||
cache.set(CACHE_KEY_STATS, new_data, CACHE_TIMEOUT)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user