После рефакторинга
This commit is contained in:
@@ -5,7 +5,7 @@ from django.contrib import messages
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.core.paginator import Paginator
|
||||
from django.db import models
|
||||
from django.db.models import F
|
||||
from django.db.models import F, Prefetch
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import redirect, render
|
||||
from django.urls import reverse_lazy
|
||||
@@ -14,7 +14,7 @@ from django.views.generic import CreateView, DeleteView, UpdateView
|
||||
|
||||
from ..forms import GeoForm, ObjItemForm, ParameterForm
|
||||
from ..mixins import CoordinateProcessingMixin, FormMessageMixin, RoleRequiredMixin
|
||||
from ..models import Geo, Modulation, ObjItem, Polarization, Satellite
|
||||
from ..models import Geo, Modulation, ObjItem, ObjectMark, Polarization, Satellite
|
||||
from ..utils import (
|
||||
format_coordinate,
|
||||
format_coords_display,
|
||||
@@ -69,7 +69,7 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
selected_sat_id = str(first_satellite.id)
|
||||
|
||||
page_number, items_per_page = parse_pagination_params(request)
|
||||
sort_param = request.GET.get("sort", "")
|
||||
sort_param = request.GET.get("sort", "-id")
|
||||
|
||||
freq_min = request.GET.get("freq_min")
|
||||
freq_max = request.GET.get("freq_max")
|
||||
@@ -99,6 +99,18 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
selected_satellites = []
|
||||
|
||||
if selected_satellites:
|
||||
# Create optimized prefetch for mirrors through geo_obj
|
||||
mirrors_prefetch = Prefetch(
|
||||
'geo_obj__mirrors',
|
||||
queryset=Satellite.objects.only('id', 'name').order_by('id')
|
||||
)
|
||||
|
||||
# Create optimized prefetch for marks (through source)
|
||||
marks_prefetch = Prefetch(
|
||||
'source__marks',
|
||||
queryset=ObjectMark.objects.select_related('created_by__user').order_by('-timestamp')
|
||||
)
|
||||
|
||||
objects = (
|
||||
ObjItem.objects.select_related(
|
||||
"geo_obj",
|
||||
@@ -111,14 +123,31 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
"parameter_obj__polarization",
|
||||
"parameter_obj__modulation",
|
||||
"parameter_obj__standard",
|
||||
"transponder",
|
||||
"transponder__sat_id",
|
||||
"transponder__polarization",
|
||||
)
|
||||
.prefetch_related(
|
||||
"parameter_obj__sigma_parameter",
|
||||
"parameter_obj__sigma_parameter__polarization",
|
||||
mirrors_prefetch,
|
||||
marks_prefetch,
|
||||
)
|
||||
.filter(parameter_obj__id_satellite_id__in=selected_satellites)
|
||||
)
|
||||
else:
|
||||
# Create optimized prefetch for mirrors through geo_obj
|
||||
mirrors_prefetch = Prefetch(
|
||||
'geo_obj__mirrors',
|
||||
queryset=Satellite.objects.only('id', 'name').order_by('id')
|
||||
)
|
||||
|
||||
# Create optimized prefetch for marks (through source)
|
||||
marks_prefetch = Prefetch(
|
||||
'source__marks',
|
||||
queryset=ObjectMark.objects.select_related('created_by__user').order_by('-timestamp')
|
||||
)
|
||||
|
||||
objects = ObjItem.objects.select_related(
|
||||
"geo_obj",
|
||||
"source",
|
||||
@@ -130,9 +159,14 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
"parameter_obj__polarization",
|
||||
"parameter_obj__modulation",
|
||||
"parameter_obj__standard",
|
||||
"transponder",
|
||||
"transponder__sat_id",
|
||||
"transponder__polarization",
|
||||
).prefetch_related(
|
||||
"parameter_obj__sigma_parameter",
|
||||
"parameter_obj__sigma_parameter__polarization",
|
||||
mirrors_prefetch,
|
||||
marks_prefetch,
|
||||
)
|
||||
|
||||
if freq_min is not None and freq_min.strip() != "":
|
||||
@@ -272,7 +306,10 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
first_param_mod_name=F("parameter_obj__modulation__name"),
|
||||
)
|
||||
|
||||
# Define valid sort fields with their database mappings
|
||||
valid_sort_fields = {
|
||||
"id": "id",
|
||||
"-id": "-id",
|
||||
"name": "name",
|
||||
"-name": "-name",
|
||||
"updated_at": "updated_at",
|
||||
@@ -301,8 +338,12 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
"-modulation": "-first_param_mod_name",
|
||||
}
|
||||
|
||||
# Apply sorting if valid, otherwise use default
|
||||
if sort_param in valid_sort_fields:
|
||||
objects = objects.order_by(valid_sort_fields[sort_param])
|
||||
else:
|
||||
# Default sort by id descending
|
||||
objects = objects.order_by("-id")
|
||||
|
||||
paginator = Paginator(objects, items_per_page)
|
||||
page_obj = paginator.get_page(page_number)
|
||||
@@ -325,8 +366,8 @@ class ObjItemListView(LoginRequiredMixin, View):
|
||||
geo_timestamp = obj.geo_obj.timestamp
|
||||
geo_location = obj.geo_obj.location
|
||||
|
||||
# Get mirrors
|
||||
mirrors_list = list(obj.geo_obj.mirrors.values_list('name', flat=True))
|
||||
# Get mirrors - use prefetched data
|
||||
mirrors_list = [mirror.name for mirror in obj.geo_obj.mirrors.all()]
|
||||
|
||||
if obj.geo_obj.coords:
|
||||
geo_coords = format_coords_display(obj.geo_obj.coords)
|
||||
@@ -489,7 +530,7 @@ class ObjItemFormView(
|
||||
model = ObjItem
|
||||
form_class = ObjItemForm
|
||||
template_name = "mainapp/objitem_form.html"
|
||||
success_url = reverse_lazy("mainapp:home")
|
||||
success_url = reverse_lazy("mainapp:source_list")
|
||||
required_roles = ["admin", "moderator"]
|
||||
|
||||
def get_success_url(self):
|
||||
|
||||
Reference in New Issue
Block a user