fixes
This commit is contained in:
@@ -3,6 +3,10 @@ from django.contrib import messages
|
||||
from django.http import JsonResponse
|
||||
from django.views.decorators.http import require_GET
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
from django.views.generic import TemplateView, FormView
|
||||
from django.contrib.auth.mixins import UserPassesTestMixin
|
||||
import pandas as pd
|
||||
from .utils import (
|
||||
fill_data_from_df,
|
||||
@@ -18,97 +22,101 @@ from .clusters import get_clusters
|
||||
from dbapp.settings import BASE_DIR
|
||||
|
||||
|
||||
def add_satellites(request):
|
||||
add_satellite_list()
|
||||
return redirect('home')
|
||||
class AddSatellitesView(View):
|
||||
def get(self, request):
|
||||
add_satellite_list()
|
||||
return redirect('home')
|
||||
|
||||
def add_transponders(request):
|
||||
try:
|
||||
parse_transponders_from_json(BASE_DIR / "transponders.json")
|
||||
except FileNotFoundError:
|
||||
print("Файл не найден")
|
||||
return redirect('home')
|
||||
class AddTranspondersView(View):
|
||||
def get(self, request):
|
||||
try:
|
||||
parse_transponders_from_json(BASE_DIR / "transponders.json")
|
||||
except FileNotFoundError:
|
||||
print("Файл не найден")
|
||||
return redirect('home')
|
||||
|
||||
class HomePageView(TemplateView):
|
||||
template_name = 'mainapp/home.html'
|
||||
|
||||
|
||||
def home_page(request):
|
||||
return render(request, 'mainapp/home.html')
|
||||
class LoadExcelDataView(FormView):
|
||||
template_name = 'mainapp/add_data_from_excel.html'
|
||||
form_class = LoadExcelData
|
||||
|
||||
def form_valid(self, form):
|
||||
uploaded_file = self.request.FILES['file']
|
||||
selected_sat = form.cleaned_data['sat_choice']
|
||||
number = form.cleaned_data['number_input']
|
||||
|
||||
try:
|
||||
import io
|
||||
df = pd.read_excel(io.BytesIO(uploaded_file.read()))
|
||||
if number > 0:
|
||||
df = df.head(number)
|
||||
result = fill_data_from_df(df, selected_sat)
|
||||
|
||||
messages.success(self.request, f"Данные успешно загружены! Обработано строк: {result}")
|
||||
return redirect('load_excel_data')
|
||||
except Exception as e:
|
||||
messages.error(self.request, f"Ошибка при обработке файла: {str(e)}")
|
||||
return redirect('load_excel_data')
|
||||
|
||||
def form_invalid(self, form):
|
||||
messages.error(self.request, "Форма заполнена некорректно.")
|
||||
return super().form_invalid(form)
|
||||
|
||||
|
||||
def load_excel_data(request):
|
||||
if request.method == "POST":
|
||||
form = LoadExcelData(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
uploaded_file = request.FILES['file']
|
||||
selected_sat = form.cleaned_data['sat_choice']
|
||||
number = form.cleaned_data['number_input']
|
||||
from django.views.generic import View
|
||||
|
||||
try:
|
||||
# Create a temporary file-like object from the uploaded file
|
||||
import io
|
||||
df = pd.read_excel(io.BytesIO(uploaded_file.read()))
|
||||
if number > 0:
|
||||
df = df.head(number)
|
||||
result = fill_data_from_df(df, selected_sat)
|
||||
class GetLocationsView(View):
|
||||
def get(self, request, sat_id):
|
||||
locations = ObjItem.objects.filter(id_vch_load__id_satellite=sat_id)
|
||||
if not locations:
|
||||
return JsonResponse({'error': 'Объектов не найдено'}, status=400)
|
||||
|
||||
messages.success(request, f"Данные успешно загружены! Обработано строк: {result}")
|
||||
return redirect('load_excel_data')
|
||||
except Exception as e:
|
||||
messages.error(request, f"Ошибка при обработке файла: {str(e)}")
|
||||
return redirect('load_excel_data')
|
||||
else:
|
||||
form = LoadExcelData()
|
||||
features = []
|
||||
for loc in locations:
|
||||
features.append({
|
||||
"type": "Feature",
|
||||
"geometry": {
|
||||
"type": "Point",
|
||||
"coordinates": [loc.id_geo.coords[0], loc.id_geo.coords[1]]
|
||||
},
|
||||
"properties": {
|
||||
"pol": loc.id_vch_load.polarization.name,
|
||||
"freq": loc.id_vch_load.frequency*1000000,
|
||||
"name": f"{loc.name}",
|
||||
"id": loc.id_geo.id
|
||||
}
|
||||
})
|
||||
|
||||
return render(request, 'mainapp/add_data_from_excel.html', {'form': form})
|
||||
|
||||
|
||||
def get_locations(request, sat_id):
|
||||
locations = ObjItem.objects.filter(id_vch_load__id_satellite=sat_id)
|
||||
if not locations:
|
||||
return JsonResponse({'error': 'Объектов не найдено'}, status=400)
|
||||
|
||||
features = []
|
||||
for loc in locations:
|
||||
features.append({
|
||||
"type": "Feature",
|
||||
"geometry": {
|
||||
"type": "Point",
|
||||
"coordinates": [loc.id_geo.coords[0], loc.id_geo.coords[1]]
|
||||
},
|
||||
"properties": {
|
||||
"pol": loc.id_vch_load.polarization.name,
|
||||
"freq": loc.id_vch_load.frequency*1000000,
|
||||
"name": f"{loc.name}",
|
||||
"id": loc.id_geo.id
|
||||
}
|
||||
return JsonResponse({
|
||||
"type": "FeatureCollection",
|
||||
"features": features
|
||||
})
|
||||
|
||||
return JsonResponse({
|
||||
"type": "FeatureCollection",
|
||||
"features": features
|
||||
})
|
||||
class LoadCsvDataView(FormView):
|
||||
template_name = 'mainapp/add_data_from_csv.html'
|
||||
form_class = LoadCsvData
|
||||
|
||||
def load_raw_csv_data(request):
|
||||
if request.method == "POST":
|
||||
form = LoadCsvData(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
uploaded_file = request.FILES['file']
|
||||
try:
|
||||
# Read the file content and pass it directly to the function
|
||||
content = uploaded_file.read()
|
||||
if isinstance(content, bytes):
|
||||
content = content.decode('utf-8')
|
||||
|
||||
get_points_from_csv(content)
|
||||
messages.success(request, f"Данные успешно загружены!")
|
||||
return redirect('load_csv_data')
|
||||
except Exception as e:
|
||||
messages.error(request, f"Ошибка при обработке файла: {str(e)}")
|
||||
return redirect('load_csv_data')
|
||||
|
||||
else:
|
||||
form = LoadCsvData()
|
||||
def form_valid(self, form):
|
||||
uploaded_file = self.request.FILES['file']
|
||||
try:
|
||||
# Read the file content and pass it directly to the function
|
||||
content = uploaded_file.read()
|
||||
if isinstance(content, bytes):
|
||||
content = content.decode('utf-8')
|
||||
|
||||
get_points_from_csv(content)
|
||||
messages.success(self.request, f"Данные успешно загружены!")
|
||||
return redirect('load_csv_data')
|
||||
except Exception as e:
|
||||
messages.error(self.request, f"Ошибка при обработке файла: {str(e)}")
|
||||
return redirect('load_csv_data')
|
||||
|
||||
return render(request, 'mainapp/add_data_from_csv.html', {'form': form})
|
||||
def form_invalid(self, form):
|
||||
messages.error(self.request, "Форма заполнена некорректно.")
|
||||
return super().form_invalid(form)
|
||||
|
||||
# def upload_file(request):
|
||||
# if request.method == 'POST' and request.FILES:
|
||||
@@ -127,87 +135,93 @@ def load_raw_csv_data(request):
|
||||
# return JsonResponse({'status': 'error', 'errors': form.errors}, status=400)
|
||||
# return render(request, 'mainapp/add_data_from_csv.html')
|
||||
from collections import defaultdict
|
||||
@staff_member_required
|
||||
def show_map_view(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)
|
||||
for obj in locations:
|
||||
points.append({
|
||||
'name': f"{obj.name}",
|
||||
'freq': f"{obj.id_vch_load.frequency} [{obj.id_vch_load.freq_range}] МГц",
|
||||
'point': (obj.id_geo.coords.x, obj.id_geo.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()
|
||||
]
|
||||
@method_decorator(staff_member_required, name='dispatch')
|
||||
class ShowMapView(UserPassesTestMixin, View):
|
||||
def test_func(self):
|
||||
return self.request.user.is_staff
|
||||
|
||||
|
||||
context = {
|
||||
'groups': groups,
|
||||
}
|
||||
return render(request, 'admin/map_custom.html', context)
|
||||
|
||||
|
||||
def cluster_test(request):
|
||||
objs = ObjItem.objects.filter(name__icontains="! Astra 4A 12654,040 [1,962] МГц H")
|
||||
coords = []
|
||||
for obj in objs:
|
||||
coords.append((obj.id_geo.coords[1], obj.id_geo.coords[0]))
|
||||
get_clusters(coords)
|
||||
|
||||
return JsonResponse({"success": "ок"})
|
||||
|
||||
def upload_vch_load_from_html(request):
|
||||
if request.method == 'POST':
|
||||
form = UploadFileForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
selected_sat = form.cleaned_data['sat_choice']
|
||||
uploaded_file = request.FILES['file']
|
||||
try:
|
||||
get_vch_load_from_html(uploaded_file, selected_sat)
|
||||
messages.success(request, "Файл успешно обработан")
|
||||
except ValueError as e:
|
||||
messages.error(request, f"Ошибка при чтении таблиц: {e}")
|
||||
except Exception as e:
|
||||
messages.error(request, f"Неизвестная ошибка: {e}")
|
||||
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)
|
||||
for obj in locations:
|
||||
points.append({
|
||||
'name': f"{obj.name}",
|
||||
'freq': f"{obj.id_vch_load.frequency} [{obj.id_vch_load.freq_range}] МГц",
|
||||
'point': (obj.id_geo.coords.x, obj.id_geo.coords.y)
|
||||
})
|
||||
else:
|
||||
messages.error(request, "Форма заполнена некорректно.")
|
||||
else:
|
||||
form = UploadFileForm()
|
||||
|
||||
return render(request, 'mainapp/upload_html.html', {'form': form})
|
||||
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()
|
||||
]
|
||||
|
||||
|
||||
def link_vch_sigma(request):
|
||||
if request.method == 'POST':
|
||||
form = VchLinkForm(request.POST)
|
||||
if form.is_valid():
|
||||
freq = form.cleaned_data['value1']
|
||||
freq_range = form.cleaned_data['value2']
|
||||
ku_range = float(form.cleaned_data['ku_range'])
|
||||
sat_id = form.cleaned_data['sat_choice']
|
||||
# print(freq, freq_range, ku_range, sat_id.pk)
|
||||
count_all, link_count = compare_and_link_vch_load(sat_id, freq, freq_range, ku_range)
|
||||
messages.success(request, f"Привязано {link_count} из {count_all} объектов")
|
||||
return redirect('link_vch_sigma')
|
||||
else:
|
||||
form = VchLinkForm()
|
||||
|
||||
return render(request, 'mainapp/link_vch.html', {'form': form})
|
||||
context = {
|
||||
'groups': groups,
|
||||
}
|
||||
return render(request, 'admin/map_custom.html', context)
|
||||
|
||||
|
||||
class ClusterTestView(View):
|
||||
def get(self, request):
|
||||
objs = ObjItem.objects.filter(name__icontains="! Astra 4A 12654,040 [1,962] МГц H")
|
||||
coords = []
|
||||
for obj in objs:
|
||||
coords.append((obj.id_geo.coords[1], obj.id_geo.coords[0]))
|
||||
get_clusters(coords)
|
||||
|
||||
return JsonResponse({"success": "ок"})
|
||||
|
||||
class UploadVchLoadView(FormView):
|
||||
template_name = 'mainapp/upload_html.html'
|
||||
form_class = UploadFileForm
|
||||
|
||||
def form_valid(self, form):
|
||||
selected_sat = form.cleaned_data['sat_choice']
|
||||
uploaded_file = self.request.FILES['file']
|
||||
try:
|
||||
get_vch_load_from_html(uploaded_file, selected_sat)
|
||||
messages.success(self.request, "Файл успешно обработан")
|
||||
except ValueError as e:
|
||||
messages.error(self.request, f"Ошибка при чтении таблиц: {e}")
|
||||
except Exception as e:
|
||||
messages.error(self.request, f"Неизвестная ошибка: {e}")
|
||||
return redirect('vch_load')
|
||||
|
||||
def form_invalid(self, form):
|
||||
messages.error(self.request, "Форма заполнена некорректно.")
|
||||
return super().form_invalid(form)
|
||||
|
||||
|
||||
class LinkVchSigmaView(FormView):
|
||||
template_name = 'mainapp/link_vch.html'
|
||||
form_class = VchLinkForm
|
||||
|
||||
def form_valid(self, form):
|
||||
freq = form.cleaned_data['value1']
|
||||
freq_range = form.cleaned_data['value2']
|
||||
ku_range = float(form.cleaned_data['ku_range'])
|
||||
sat_id = form.cleaned_data['sat_choice']
|
||||
# print(freq, freq_range, ku_range, sat_id.pk)
|
||||
count_all, link_count = compare_and_link_vch_load(sat_id, freq, freq_range, ku_range)
|
||||
messages.success(self.request, f"Привязано {link_count} из {count_all} объектов")
|
||||
return redirect('link_vch_sigma')
|
||||
|
||||
def form_invalid(self, form):
|
||||
return self.render_to_response(self.get_context_data(form=form))
|
||||
Reference in New Issue
Block a user