🚧 hromadne akcie na registraciach + zapisovanie casov + automaticke vytvaranie kol

This commit is contained in:
2025-03-06 20:07:46 +01:00
parent 2346e644a7
commit 2656626a37
14 changed files with 133 additions and 46 deletions

View File

@@ -5,7 +5,7 @@ from datetime import datetime
from django.views.generic import View
from django.http import JsonResponse
from events.models import Event
from events.models import Event, Lap
logger = logging.getLogger(__name__)
@@ -27,39 +27,47 @@ class WriteTimeApiView(View):
# create new TimeRecord object and save it to the database
TimeRecord(
card_id=kwargs['card_id'],
chip_id=kwargs['chip_id'],
timer_id=kwargs['timer_id'],
time=record_time,
).save()
# find the active event
event = Event.objects.filter(
is_active=True,
is_actual=True,
)
# if there is no active event, log error
if not event:
logger.error("No active event found")
return JsonResponse({"error": "No active event found"}, status=http.HTTPStatus.NOT_FOUND, safe=False)
return JsonResponse({"status": "ok"}, status=http.HTTPStatus.OK, safe=False)
# find registration with the given card_id
registration = event[0].registrations.filter(
card_id=kwargs['card_id'],
chip_id=kwargs['chip_id'],
)
if not registration:
logger.error(f"No registration found for card_id: {kwargs['card_id']}")
return JsonResponse({"status": "ok"}, status=http.HTTPStatus.OK ,safe=False)
logger.error(f"No registration found for chip_id: {kwargs['chip_id']}")
return JsonResponse({"status": "ok"}, status=http.HTTPStatus.OK, safe=False)
# if there is more than one registration with the same card_id, log error
if len(registration) > 1:
logger.error(f"Multiple registrations found for card_id: {kwargs['card_id']}")
logger.error(f"Multiple registrations found for chip_id: {kwargs['chip_id']}")
# create new lap or update existing lap
registration[0].laps.filter(end = None).update(end = record_time)
laps = Lap.objects.filter(registration=registration[0])
open_lap = laps.filter(end=None)
if not open_lap:
registration[0].laps.create(
start=record_time,
number=len(laps) + 1,
)
else:
open_lap[0].end = record_time
open_lap[0].save()
return JsonResponse({"status": "ok"}, status=http.HTTPStatus.OK ,safe=False)
except Exception as e:
logger.error(f"Error while getting prometheus data: {e}")
return JsonResponse({"error": f"Error while getting prometheus data: {e}"}, status=http.HTTPStatus.INTERNAL_SERVER_ERROR , safe=False)
logger.error(f"Error while writing lap: {e}")
return JsonResponse({"status": "ok"}, status=http.HTTPStatus.OK, safe=False)