Реструктуризация views
This commit is contained in:
139
dbapp/mainapp/views/map.py
Normal file
139
dbapp/mainapp/views/map.py
Normal file
@@ -0,0 +1,139 @@
|
||||
"""
|
||||
Map related views for displaying objects on maps.
|
||||
"""
|
||||
from collections import defaultdict
|
||||
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import redirect, render
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from ..clusters import get_clusters
|
||||
from ..mixins import RoleRequiredMixin
|
||||
from ..models import ObjItem
|
||||
|
||||
|
||||
@method_decorator(staff_member_required, name="dispatch")
|
||||
class ShowMapView(RoleRequiredMixin, View):
|
||||
"""View for displaying objects on map (admin interface)."""
|
||||
|
||||
required_roles = ["admin", "moderator"]
|
||||
|
||||
def get(self, request):
|
||||
ids = request.GET.get("ids", "")
|
||||
points = []
|
||||
if ids:
|
||||
id_list = [int(x) for x in ids.split(",") if x.isdigit()]
|
||||
locations = ObjItem.objects.filter(id__in=id_list).select_related(
|
||||
"parameter_obj",
|
||||
"parameter_obj__id_satellite",
|
||||
"parameter_obj__polarization",
|
||||
"parameter_obj__modulation",
|
||||
"parameter_obj__standard",
|
||||
"geo_obj",
|
||||
)
|
||||
for obj in locations:
|
||||
if (
|
||||
not hasattr(obj, "geo_obj")
|
||||
or not obj.geo_obj
|
||||
or not obj.geo_obj.coords
|
||||
):
|
||||
continue
|
||||
param = getattr(obj, 'parameter_obj', None)
|
||||
if not param:
|
||||
continue
|
||||
points.append(
|
||||
{
|
||||
"name": f"{obj.name}",
|
||||
"freq": f"{param.frequency} [{param.freq_range}] МГц",
|
||||
"point": (obj.geo_obj.coords.x, obj.geo_obj.coords.y),
|
||||
}
|
||||
)
|
||||
else:
|
||||
return redirect("admin")
|
||||
|
||||
grouped = defaultdict(list)
|
||||
for p in points:
|
||||
grouped[p["name"]].append({"point": p["point"], "frequency": p["freq"]})
|
||||
|
||||
groups = [
|
||||
{"name": name, "points": coords_list}
|
||||
for name, coords_list in grouped.items()
|
||||
]
|
||||
|
||||
context = {
|
||||
"groups": groups,
|
||||
}
|
||||
return render(request, "admin/map_custom.html", context)
|
||||
|
||||
|
||||
class ShowSelectedObjectsMapView(LoginRequiredMixin, View):
|
||||
"""View for displaying selected objects on map."""
|
||||
|
||||
def get(self, request):
|
||||
ids = request.GET.get("ids", "")
|
||||
points = []
|
||||
if ids:
|
||||
id_list = [int(x) for x in ids.split(",") if x.isdigit()]
|
||||
locations = ObjItem.objects.filter(id__in=id_list).select_related(
|
||||
"parameter_obj",
|
||||
"parameter_obj__id_satellite",
|
||||
"parameter_obj__polarization",
|
||||
"parameter_obj__modulation",
|
||||
"parameter_obj__standard",
|
||||
"geo_obj",
|
||||
)
|
||||
for obj in locations:
|
||||
if (
|
||||
not hasattr(obj, "geo_obj")
|
||||
or not obj.geo_obj
|
||||
or not obj.geo_obj.coords
|
||||
):
|
||||
continue
|
||||
param = getattr(obj, 'parameter_obj', None)
|
||||
if not param:
|
||||
continue
|
||||
points.append(
|
||||
{
|
||||
"name": f"{obj.name}",
|
||||
"freq": f"{param.frequency} [{param.freq_range}] МГц",
|
||||
"point": (obj.geo_obj.coords.x, obj.geo_obj.coords.y),
|
||||
}
|
||||
)
|
||||
else:
|
||||
return redirect("mainapp:objitem_list")
|
||||
|
||||
# Group points by object name
|
||||
grouped = defaultdict(list)
|
||||
for p in points:
|
||||
grouped[p["name"]].append({"point": p["point"], "frequency": p["freq"]})
|
||||
|
||||
groups = [
|
||||
{"name": name, "points": coords_list}
|
||||
for name, coords_list in grouped.items()
|
||||
]
|
||||
|
||||
context = {
|
||||
"groups": groups,
|
||||
}
|
||||
return render(request, "mainapp/objitem_map.html", context)
|
||||
|
||||
|
||||
class ClusterTestView(LoginRequiredMixin, View):
|
||||
"""Test view for clustering functionality."""
|
||||
|
||||
def get(self, request):
|
||||
objs = ObjItem.objects.filter(
|
||||
name__icontains="! Astra 4A 12654,040 [1,962] МГц H"
|
||||
)
|
||||
coords = []
|
||||
for obj in objs:
|
||||
if hasattr(obj, "geo_obj") and obj.geo_obj and obj.geo_obj.coords:
|
||||
coords.append(
|
||||
(obj.geo_obj.coords.coords[1], obj.geo_obj.coords.coords[0])
|
||||
)
|
||||
get_clusters(coords)
|
||||
|
||||
return JsonResponse({"success": "ок"})
|
||||
Reference in New Issue
Block a user