Пофиксил журнал ошибок
This commit is contained in:
@@ -17,10 +17,8 @@ class ErrorsReportView(TemplateView, LoginRequiredMixin, PermissionRequiredMixin
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['issue_types'] = IssueType.objects.all().order_by('category', 'name')
|
||||
context['errors'] = context['issue_types'].filter(category='error')
|
||||
context['malfunctions'] = context['issue_types'].filter(category='malfunction')
|
||||
context['full_width_page'] = True
|
||||
context['location_places'] = DailyReport.PLACES
|
||||
return context
|
||||
|
||||
|
||||
@@ -31,18 +29,24 @@ class ErrorsReportAPIView(View, LoginRequiredMixin, PermissionRequiredMixin):
|
||||
# Получаем параметры фильтрации
|
||||
date_from = request.GET.get('date_from')
|
||||
date_to = request.GET.get('date_to')
|
||||
location_places = request.GET.getlist('location_place')
|
||||
location_place = request.GET.get('location_place')
|
||||
error_filters = request.GET.getlist('error_filter')
|
||||
malfunction_filters = request.GET.getlist('malfunction_filter')
|
||||
|
||||
reports = DailyReport.objects.all()
|
||||
# location_place обязателен
|
||||
if not location_place:
|
||||
return JsonResponse({
|
||||
'data': [],
|
||||
'columns': [],
|
||||
'error': 'Выберите комплекс'
|
||||
})
|
||||
|
||||
reports = DailyReport.objects.filter(location_place=location_place)
|
||||
|
||||
if date_from:
|
||||
reports = reports.filter(date__gte=date_from)
|
||||
if date_to:
|
||||
reports = reports.filter(date__lte=date_to)
|
||||
if location_places:
|
||||
reports = reports.filter(location_place__in=location_places)
|
||||
|
||||
# Фильтрация по ошибкам/неисправностям
|
||||
if error_filters:
|
||||
@@ -52,8 +56,8 @@ class ErrorsReportAPIView(View, LoginRequiredMixin, PermissionRequiredMixin):
|
||||
|
||||
reports = reports.prefetch_related('downtime_periods', 'issue_marks__issue_type').distinct()
|
||||
|
||||
# Получаем все типы ошибок/неисправностей
|
||||
issue_types = IssueType.objects.all().order_by('category', 'name')
|
||||
# Получаем типы ошибок/неисправностей для выбранного комплекса
|
||||
issue_types = IssueType.objects.filter(location_place=location_place).order_by('category', 'name')
|
||||
|
||||
data = []
|
||||
for report in reports:
|
||||
@@ -83,7 +87,7 @@ class ErrorsReportAPIView(View, LoginRequiredMixin, PermissionRequiredMixin):
|
||||
'location_place_display': report.get_location_place_display() if report.location_place else '',
|
||||
}
|
||||
|
||||
# Добавляем отметки по каждому типу
|
||||
# Добавляем отметки по каждому типу (только для выбранного комплекса)
|
||||
for it in issue_types:
|
||||
row[f'issue_{it.id}'] = marks_dict.get(it.id, False)
|
||||
|
||||
@@ -124,13 +128,27 @@ class ErrorsReportSaveAPIView(View):
|
||||
return JsonResponse({'success': False, 'error': 'Неверный формат даты'}, status=400)
|
||||
|
||||
report_id = data.get('id')
|
||||
location_place = data.get('location_place', 'kr')
|
||||
|
||||
# Проверка на дублирование даты при создании новой записи
|
||||
# Проверка на дублирование даты + комплекса при создании новой записи
|
||||
if not report_id:
|
||||
if DailyReport.objects.filter(date=report_date).exists():
|
||||
if DailyReport.objects.filter(date=report_date, location_place=location_place).exists():
|
||||
place_display = dict(DailyReport.PLACES).get(location_place, location_place)
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'error': f'Запись за {report_date.strftime("%d.%m.%Y")} уже существует'
|
||||
'error': f'Запись за {report_date.strftime("%d.%m.%Y")} для комплекса {place_display} уже существует'
|
||||
}, status=400)
|
||||
else:
|
||||
# При обновлении проверяем, не занята ли комбинация другой записью
|
||||
existing = DailyReport.objects.filter(
|
||||
date=report_date,
|
||||
location_place=location_place
|
||||
).exclude(id=report_id).first()
|
||||
if existing:
|
||||
place_display = dict(DailyReport.PLACES).get(location_place, location_place)
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'error': f'Запись за {report_date.strftime("%d.%m.%Y")} для комплекса {place_display} уже существует'
|
||||
}, status=400)
|
||||
|
||||
with transaction.atomic():
|
||||
|
||||
Reference in New Issue
Block a user