Исправил отображения объектов в источниках

This commit is contained in:
2025-11-16 00:16:50 +03:00
parent 9a816e62c2
commit d9cb243388
13 changed files with 1198 additions and 48 deletions

View File

@@ -46,22 +46,30 @@ class LinkLyngsatSourcesView(LoginRequiredMixin, FormMessageMixin, FormView):
param = objitem.parameter_obj
# Round object frequency
# Round object frequency to 1 decimal place
if param.frequency:
rounded_freq = round(param.frequency, 0) # Round to integer
rounded_freq = round(param.frequency, 1) # Round to 1 decimal place
# Find matching LyngSat source
# Compare by rounded frequency and polarization
# Compare by rounded frequency (with tolerance) and polarization
# LyngSat frequencies are also rounded to 1 decimal place for comparison
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')
polarization=param.polarization
).select_related('id_satellite', 'polarization')
if lyngsat_sources.exists():
# Take first matching source
objitem.lyngsat_source = lyngsat_sources.first()
# Filter by rounded frequency with tolerance
matching_sources = []
for lyngsat in lyngsat_sources:
if lyngsat.frequency:
rounded_lyngsat_freq = round(lyngsat.frequency, 1)
if abs(rounded_lyngsat_freq - rounded_freq) <= frequency_tolerance:
matching_sources.append(lyngsat)
if matching_sources:
# Take first matching source (sorted by frequency difference)
matching_sources.sort(key=lambda x: abs(round(x.frequency, 1) - rounded_freq))
objitem.lyngsat_source = matching_sources[0]
objitem.save(update_fields=['lyngsat_source'])
linked_count += 1
@@ -159,3 +167,35 @@ class ClearLyngsatCacheView(LoginRequiredMixin, View):
def get(self, request):
"""Cache management page."""
return render(request, 'mainapp/clear_lyngsat_cache.html')
class UnlinkAllLyngsatSourcesView(LoginRequiredMixin, View):
"""View for unlinking all LyngSat sources from ObjItems."""
def post(self, request):
"""Unlink all LyngSat sources."""
try:
# Count objects with LyngSat sources before unlinking
linked_count = ObjItem.objects.filter(lyngsat_source__isnull=False).count()
# Unlink all LyngSat sources
ObjItem.objects.filter(lyngsat_source__isnull=False).update(lyngsat_source=None)
messages.success(
request,
f"Успешно отвязано {linked_count} объектов от источников LyngSat"
)
except Exception as e:
messages.error(request, f"Ошибка при отвязке источников: {str(e)}")
return redirect('mainapp:actions')
def get(self, request):
"""Show confirmation page."""
# Count objects with LyngSat sources
linked_count = ObjItem.objects.filter(lyngsat_source__isnull=False).count()
context = {
'linked_count': linked_count
}
return render(request, 'mainapp/unlink_lyngsat_confirm.html', context)