Привязка данных LyngSat
This commit is contained in:
@@ -3,6 +3,7 @@ from collections import defaultdict
|
||||
from io import BytesIO
|
||||
|
||||
# Django imports
|
||||
from django.utils import timezone
|
||||
from django.contrib import messages
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from django.contrib.auth import logout
|
||||
@@ -38,6 +39,7 @@ from .forms import (
|
||||
UploadVchLoad,
|
||||
VchLinkForm,
|
||||
FillLyngsatDataForm,
|
||||
LinkLyngsatForm,
|
||||
)
|
||||
from .mixins import CoordinateProcessingMixin, FormMessageMixin, RoleRequiredMixin
|
||||
from .models import Geo, Modulation, ObjItem, Polarization, Satellite
|
||||
@@ -375,6 +377,108 @@ class LinkVchSigmaView(LoginRequiredMixin, FormView):
|
||||
return self.render_to_response(self.get_context_data(form=form))
|
||||
|
||||
|
||||
class LinkLyngsatSourcesView(LoginRequiredMixin, FormMessageMixin, FormView):
|
||||
"""Представление для привязки источников LyngSat к объектам"""
|
||||
template_name = "mainapp/link_lyngsat.html"
|
||||
form_class = LinkLyngsatForm
|
||||
success_message = "Привязка источников LyngSat завершена"
|
||||
error_message = "Ошибка при привязке источников"
|
||||
|
||||
def form_valid(self, form):
|
||||
from lyngsatapp.models import LyngSat
|
||||
|
||||
satellites = form.cleaned_data.get("satellites")
|
||||
frequency_tolerance = form.cleaned_data.get("frequency_tolerance", 0.5)
|
||||
|
||||
# Если спутники не выбраны, обрабатываем все
|
||||
if satellites:
|
||||
objitems = ObjItem.objects.filter(
|
||||
parameter_obj__id_satellite__in=satellites
|
||||
).select_related('parameter_obj', 'parameter_obj__polarization')
|
||||
else:
|
||||
objitems = ObjItem.objects.filter(
|
||||
parameter_obj__isnull=False
|
||||
).select_related('parameter_obj', 'parameter_obj__polarization')
|
||||
|
||||
linked_count = 0
|
||||
total_count = objitems.count()
|
||||
|
||||
for objitem in objitems:
|
||||
if not hasattr(objitem, 'parameter_obj') or not objitem.parameter_obj:
|
||||
continue
|
||||
|
||||
param = objitem.parameter_obj
|
||||
|
||||
# Округляем частоту объекта
|
||||
if param.frequency:
|
||||
rounded_freq = round(param.frequency, 0) # Округление до целого
|
||||
|
||||
# Ищем подходящий источник LyngSat
|
||||
# Сравниваем по округленной частоте и поляризации
|
||||
lyngsat_sources = LyngSat.objects.filter(
|
||||
id_satellite=param.id_satellite,
|
||||
polarization=param.polarization,
|
||||
frequency__gte=rounded_freq - frequency_tolerance,
|
||||
frequency__lte=rounded_freq + frequency_tolerance
|
||||
).order_by('frequency')
|
||||
|
||||
if lyngsat_sources.exists():
|
||||
# Берем первый подходящий источник
|
||||
objitem.lyngsat_source = lyngsat_sources.first()
|
||||
objitem.save(update_fields=['lyngsat_source'])
|
||||
linked_count += 1
|
||||
|
||||
messages.success(
|
||||
self.request,
|
||||
f"Привязано {linked_count} из {total_count} объектов к источникам LyngSat"
|
||||
)
|
||||
return redirect("mainapp:link_lyngsat")
|
||||
|
||||
def form_invalid(self, form):
|
||||
return self.render_to_response(self.get_context_data(form=form))
|
||||
|
||||
|
||||
class LyngsatDataAPIView(LoginRequiredMixin, View):
|
||||
"""API для получения данных LyngSat источника"""
|
||||
|
||||
def get(self, request, lyngsat_id):
|
||||
from lyngsatapp.models import LyngSat
|
||||
|
||||
try:
|
||||
lyngsat = LyngSat.objects.select_related(
|
||||
'id_satellite',
|
||||
'polarization',
|
||||
'modulation',
|
||||
'standard'
|
||||
).get(id=lyngsat_id)
|
||||
|
||||
# Форматируем дату с учетом локального времени
|
||||
last_update_str = '-'
|
||||
if lyngsat.last_update:
|
||||
local_time = timezone.localtime(lyngsat.last_update)
|
||||
last_update_str = local_time.strftime("%d.%m.%Y")
|
||||
|
||||
data = {
|
||||
'id': lyngsat.id,
|
||||
'satellite': lyngsat.id_satellite.name if lyngsat.id_satellite else '-',
|
||||
'frequency': f"{lyngsat.frequency:.3f}" if lyngsat.frequency else '-',
|
||||
'polarization': lyngsat.polarization.name if lyngsat.polarization else '-',
|
||||
'modulation': lyngsat.modulation.name if lyngsat.modulation else '-',
|
||||
'standard': lyngsat.standard.name if lyngsat.standard else '-',
|
||||
'sym_velocity': f"{lyngsat.sym_velocity:.0f}" if lyngsat.sym_velocity else '-',
|
||||
'fec': lyngsat.fec or '-',
|
||||
'channel_info': lyngsat.channel_info or '-',
|
||||
'last_update': last_update_str,
|
||||
'url': lyngsat.url or None,
|
||||
}
|
||||
|
||||
return JsonResponse(data)
|
||||
except LyngSat.DoesNotExist:
|
||||
return JsonResponse({'error': 'Источник LyngSat не найден'}, status=404)
|
||||
except Exception as e:
|
||||
return JsonResponse({'error': str(e)}, status=500)
|
||||
|
||||
|
||||
class ProcessKubsatView(LoginRequiredMixin, FormMessageMixin, FormView):
|
||||
template_name = "mainapp/process_kubsat.html"
|
||||
form_class = NewEventForm
|
||||
@@ -474,6 +578,7 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
"geo_obj",
|
||||
"updated_by__user",
|
||||
"created_by__user",
|
||||
"lyngsat_source",
|
||||
"parameter_obj",
|
||||
"parameter_obj__id_satellite",
|
||||
"parameter_obj__polarization",
|
||||
@@ -487,6 +592,7 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
"geo_obj",
|
||||
"updated_by__user",
|
||||
"created_by__user",
|
||||
"lyngsat_source",
|
||||
"parameter_obj",
|
||||
"parameter_obj__id_satellite",
|
||||
"parameter_obj__polarization",
|
||||
@@ -763,6 +869,9 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
comment = obj.geo_obj.comment or "-"
|
||||
is_average = "Да" if obj.geo_obj.is_average else "Нет" if obj.geo_obj.is_average is not None else "-"
|
||||
|
||||
# Check if LyngSat source is linked
|
||||
source_type = "ТВ" if obj.lyngsat_source else "-"
|
||||
|
||||
processed_objects.append(
|
||||
{
|
||||
"id": obj.id,
|
||||
@@ -785,6 +894,7 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
"updated_by": obj.updated_by if obj.updated_by else "-",
|
||||
"comment": comment,
|
||||
"is_average": is_average,
|
||||
"source_type": source_type,
|
||||
"standard": standard_name,
|
||||
"obj": obj,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user