From cea28e5b624728af810c37981837b12631577131 Mon Sep 17 00:00:00 2001 From: xiaji Date: Thu, 30 Apr 2026 10:34:07 +0800 Subject: [PATCH] Add battery inline editing for UPS host --- ups_management/ups_manager/forms.py | 35 +++++ .../templates/ups_manager/ups_form.html | 129 ++++++++++++++++++ ups_management/ups_manager/views.py | 23 ++++ 3 files changed, 187 insertions(+) create mode 100644 ups_management/ups_manager/forms.py diff --git a/ups_management/ups_manager/forms.py b/ups_management/ups_manager/forms.py new file mode 100644 index 0000000..82a8076 --- /dev/null +++ b/ups_management/ups_manager/forms.py @@ -0,0 +1,35 @@ +from django import forms +from django.forms import inlineformset_factory +from .models import UPSHost, Battery + + +class BatteryForm(forms.ModelForm): + class Meta: + model = Battery + fields = ['brand', 'model', 'weight', 'quantity', 'location', 'install_date', 'last_maintenance_date'] + labels = { + 'brand': '电池品牌', + 'model': '电池型号', + 'weight': '重量(kg)', + 'quantity': '数量', + 'location': '存放位置', + 'install_date': '安装日期', + 'last_maintenance_date': '上次维保时间', + } + widgets = { + 'brand': forms.TextInput(attrs={'class': 'form-control form-control-sm'}), + 'model': forms.TextInput(attrs={'class': 'form-control form-control-sm'}), + 'weight': forms.NumberInput(attrs={'class': 'form-control form-control-sm', 'step': '0.1'}), + 'quantity': forms.NumberInput(attrs={'class': 'form-control form-control-sm', 'min': '1'}), + 'location': forms.TextInput(attrs={'class': 'form-control form-control-sm'}), + } + + +BatteryFormSet = inlineformset_factory( + UPSHost, + Battery, + form=BatteryForm, + extra=0, + can_delete=True, + fields=['brand', 'model', 'weight', 'quantity', 'location', 'install_date', 'last_maintenance_date'] +) diff --git a/ups_management/ups_manager/templates/ups_manager/ups_form.html b/ups_management/ups_manager/templates/ups_manager/ups_form.html index 293d479..a81bb8f 100644 --- a/ups_management/ups_manager/templates/ups_manager/ups_form.html +++ b/ups_management/ups_manager/templates/ups_manager/ups_form.html @@ -116,6 +116,114 @@ + {% if object and battery_formset %} +
+
+
+
下挂电池管理
+
+
+ {{ battery_formset.management_form }} +
+ + + + + + + + + + + + + + + {% for battery_form in battery_formset %} + + + + + + + + + + + {% empty %} + + + + {% endfor %} + +
电池品牌电池型号重量(kg)数量存放位置安装日期上次维保删除
+ {{ battery_form.id }} + {{ battery_form.brand }} + {% if battery_form.brand.errors %} + {% for error in battery_form.brand.errors %} +
{{ error }}
+ {% endfor %} + {% endif %} +
+ {{ battery_form.model }} + {% if battery_form.model.errors %} + {% for error in battery_form.model.errors %} +
{{ error }}
+ {% endfor %} + {% endif %} +
+ {{ battery_form.weight }} + {% if battery_form.weight.errors %} + {% for error in battery_form.weight.errors %} +
{{ error }}
+ {% endfor %} + {% endif %} +
+ {{ battery_form.quantity }} + {% if battery_form.quantity.errors %} + {% for error in battery_form.quantity.errors %} +
{{ error }}
+ {% endfor %} + {% endif %} +
+ {{ battery_form.location }} + {% if battery_form.location.errors %} + {% for error in battery_form.location.errors %} +
{{ error }}
+ {% endfor %} + {% endif %} +
+ + {% if battery_form.install_date.errors %} + {% for error in battery_form.install_date.errors %} +
{{ error }}
+ {% endfor %} + {% endif %} +
+ + {% if battery_form.last_maintenance_date.errors %} + {% for error in battery_form.last_maintenance_date.errors %} +
{{ error }}
+ {% endfor %} + {% endif %} +
+ {{ battery_form.DELETE }} +
暂未下挂电池
+
+
+
+ {% endif %} +
取消 @@ -143,6 +251,16 @@ minDate: '1900-01-01', maxDate: new Date() }); + + flatpickr('.datepicker-battery', { + dateFormat: 'Y-m-d', + locale: 'zh', + allowInput: true, + todayButton: true, + clearButton: true, + minDate: '1900-01-01', + maxDate: new Date() + }); }); @@ -164,6 +282,17 @@ border-radius: 8px; box-shadow: 0 10px 40px rgba(0,0,0,0.15); } + .table-responsive { + overflow-x: auto; + } + .form-control-sm { + padding: 0.25rem 0.5rem; + font-size: 0.875rem; + } + .table th { + font-size: 0.875rem; + white-space: nowrap; + } {% endblock %} diff --git a/ups_management/ups_manager/views.py b/ups_management/ups_manager/views.py index d1f1d32..f764e97 100644 --- a/ups_management/ups_manager/views.py +++ b/ups_management/ups_manager/views.py @@ -1,7 +1,9 @@ from django.shortcuts import render, redirect from django.views.generic import ListView, CreateView, UpdateView, DeleteView from django.urls import reverse_lazy +from django.contrib import messages from .models import UPSHost, Battery, Contact, Supplier, MaintenanceRecord +from .forms import BatteryFormSet class DashboardView(ListView): @@ -106,6 +108,27 @@ class UPSHostUpdateView(UpdateView): template_name = 'ups_manager/ups_form.html' fields = ['brand', 'model', 'ip_address', 'quantity', 'location', 'last_maintenance_date', 'contact'] success_url = reverse_lazy('ups_list') + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + if self.request.POST: + context['battery_formset'] = BatteryFormSet(self.request.POST, instance=self.object) + else: + context['battery_formset'] = BatteryFormSet(instance=self.object) + return context + + def form_valid(self, form): + context = self.get_context_data() + battery_formset = context['battery_formset'] + + if battery_formset.is_valid(): + self.object = form.save() + battery_formset.instance = self.object + battery_formset.save() + messages.success(self.request, 'UPS主机和电池信息已更新') + return redirect(self.success_url) + else: + return self.form_invalid(form) class UPSHostDeleteView(DeleteView):