From 8303edb8af879448ef8ec85237bd651469edb972 Mon Sep 17 00:00:00 2001 From: zakhy <=> Date: Tue, 1 Apr 2025 11:48:17 +0700 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=BB=D0=B1=D0=B5=D1=86=20=D0=B2=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B7=D0=B8=D1=86=D0=B8=D0=BE=D0=BD=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B8=20=D0=BB=D1=8E=D0=B4=D0=B5=D0=B9?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Теперь подсвечивается ячейка, в зависимости от времени координат --- apps/util/datetime_funcs.py | 8 +++++ .../templates/wklocation_map.html | 12 ++++++++ apps/workerslocation/views/views_radiusapi.py | 29 ++++++++++++++----- mfssogr/app_settings.py | 6 +++- 4 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 apps/util/datetime_funcs.py diff --git a/apps/util/datetime_funcs.py b/apps/util/datetime_funcs.py new file mode 100644 index 0000000..b71a9af --- /dev/null +++ b/apps/util/datetime_funcs.py @@ -0,0 +1,8 @@ +from django.conf import settings +from django.utils import timezone + + +def get_current_time(): + current_time = timezone.now() + current_time_timezoned = current_time.astimezone(settings.TIME_ZONE_OBJECT) + return current_time_timezoned diff --git a/apps/workerslocation/templates/wklocation_map.html b/apps/workerslocation/templates/wklocation_map.html index 0f8bddf..2a5cec9 100644 --- a/apps/workerslocation/templates/wklocation_map.html +++ b/apps/workerslocation/templates/wklocation_map.html @@ -67,6 +67,18 @@ const tdWorkerCoord = document.createElement('td') tdWorkerCoord.textContent = `${worker.time}` + switch (worker.condition) { + case "ok": + tdWorkerCoord.style.backgroundColor = '' + break + case "warning": + tdWorkerCoord.style.backgroundColor = 'var(--warning)' + break + case "danger": + tdWorkerCoord.style.backgroundColor = '#bebebe' + break + } + tr.appendChild(tdWorkerID) tr.appendChild(tdWorkerCoord) return tr diff --git a/apps/workerslocation/views/views_radiusapi.py b/apps/workerslocation/views/views_radiusapi.py index 90483ac..cd50f7c 100644 --- a/apps/workerslocation/views/views_radiusapi.py +++ b/apps/workerslocation/views/views_radiusapi.py @@ -6,10 +6,12 @@ from django.conf import settings from django.contrib.auth.decorators import login_required from django.http import JsonResponse from django.shortcuts import render, get_object_or_404, redirect +from django.utils import timezone from django.views.decorators.cache import never_cache from apps.general.views import settingsmodule from apps.util import generalmodule +from apps.util.datetime_funcs import get_current_time from apps.util.natsort import natural_sort_key, get_priority from apps.workerslocation.forms.form_worker import WorkerForm from apps.workerslocation.models.workers_locations import WorkersLocation @@ -42,12 +44,10 @@ def worker_edit(request, pk): def map_index(request, context): try: lon = str( - float( - settingsmodule.get_settings("longitude", "86.10111978605353")) + float(settingsmodule.get_settings("longitude", "86.10111978605353")) ) # долгота центра lat = str( - float( - settingsmodule.get_settings("latitude", "55.344251210827814")) + float(settingsmodule.get_settings("latitude", "55.344251210827814")) ) # широта context.update( { @@ -66,15 +66,16 @@ def update_object(request, pk=None): date_start = datetime.now() - timedelta(days=1) if pk: workers = WorkersLocation.objects.filter( - active=True, update_time__gt=date_start, id=pk + active=True, id=pk ) else: workers = WorkersLocation.objects.filter( - active=True, update_time__gt=date_start + active=True, ) workers_mas = [] for worker in workers: worker_name = worker.name if worker.name else worker.uid + worker_connection_condition = get_worker_condition(worker.update_time) workers_mas.append( { "id": worker.id, @@ -85,10 +86,24 @@ def update_object(request, pk=None): "velocity": worker.velocity, "radius": worker.radius, "time": worker.update_time.strftime("%d.%m.%Y %H:%M:%S"), + "condition": worker_connection_condition, } ) - workers_mas.sort(key=lambda worker_ : get_priority(worker_['name'])) + workers_mas.sort(key=lambda worker_: get_priority(worker_["name"])) return JsonResponse(workers_mas, safe=False) except Exception as err: logging.error(traceback.format_exc()) return JsonResponse([], status=500, safe=False) + + +def get_worker_condition(update_time: datetime) -> str: + now = get_current_time() + record_time = update_time.replace(tzinfo=settings.TIME_ZONE_OBJECT) + # Разница во времени в часах между текущем временем и времени координаты + delta = (now - record_time).total_seconds() / 60 / 60 + if delta < 1: + return "ok" + elif delta < 24: + return "warning" + else: + return "danger" diff --git a/mfssogr/app_settings.py b/mfssogr/app_settings.py index 3ea4d4d..85539e0 100644 --- a/mfssogr/app_settings.py +++ b/mfssogr/app_settings.py @@ -1,5 +1,9 @@ +import zoneinfo + LANGUAGE_CODE = "ru" -TIME_ZONE = "Europe/Moscow" +TIME_ZONE = 'Asia/Krasnoyarsk' +TIME_ZONE_OBJECT = zoneinfo.ZoneInfo(TIME_ZONE) +UTC_TZ_OBJECT = zoneinfo.ZoneInfo("UTC") USE_I18N = True USE_L10N = True USE_TZ = False