fix write times

This commit is contained in:
2025-03-13 21:16:08 +01:00
parent 7f08d1972c
commit b3a429b3f2
3 changed files with 21 additions and 7 deletions

3
API.md
View File

@@ -20,4 +20,5 @@ zapisuje cas ako end.
**Example Request:** **Example Request:**
```bash ```bash
curl -X GET "http://your-domain/api/timer/write/12345/222-222-222-222/1741169316.149847/" curl -X GET "http://your-domain/api/timer/write/12345/222-222-222-222/1741169316.149847/"

View File

@@ -1,14 +1,17 @@
from django.urls import path from django.urls import path
from timer.views import WriteTimeApiView from timer.views import WriteTimeApiView, HealthCheckApiView
urlpatterns = [ urlpatterns = [
path(
"healthcheck/",
HealthCheckApiView.as_view(),
name="healthcheck",
),
path( path(
"write/<str:timer_id>/<str:chip_id>/<str:time>/", "write/<str:timer_id>/<str:chip_id>/<str:time>/",
WriteTimeApiView.as_view(), WriteTimeApiView.as_view(),
name="write_time", name="write_time",
), ),
] ]

View File

@@ -1,7 +1,7 @@
import http import http
import logging import logging
from datetime import datetime from datetime import datetime
from django.utils import timezone
from django.views.generic import View from django.views.generic import View
from django.http import JsonResponse from django.http import JsonResponse
@@ -12,6 +12,16 @@ logger = logging.getLogger(__name__)
from timer.models import TimeRecord from timer.models import TimeRecord
class HealthCheckApiView(View):
"""
View to check if the server is running
returns standard JsonResponse with status 'ok'
"""
@staticmethod
def get(request, *args, **kwargs):
return JsonResponse({"status": "ok"}, status=http.HTTPStatus.OK, safe=False)
class WriteTimeApiView(View): class WriteTimeApiView(View):
""" """
View to write time records View to write time records
@@ -23,7 +33,7 @@ class WriteTimeApiView(View):
try: try:
# parse datetime from string of unix timestamp with milliseconds '1741169756.049847' # parse datetime from string of unix timestamp with milliseconds '1741169756.049847'
record_time = datetime.fromtimestamp(float(kwargs['time'])) record_time = timezone.make_aware(datetime.fromtimestamp(float(kwargs['time'])))
# create new TimeRecord object and save it to the database # create new TimeRecord object and save it to the database
TimeRecord( TimeRecord(