301 lines
10 KiB
Python
301 lines
10 KiB
Python
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, UPSHostForm
|
|
|
|
|
|
class DashboardView(ListView):
|
|
template_name = 'ups_manager/index.html'
|
|
context_object_name = 'dashboard_data'
|
|
|
|
def get_queryset(self):
|
|
return None
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context['ups_count'] = UPSHost.objects.count()
|
|
context['battery_count'] = Battery.objects.count()
|
|
context['contact_count'] = Contact.objects.count()
|
|
context['supplier_count'] = Supplier.objects.count()
|
|
context['maintenance_count'] = MaintenanceRecord.objects.count()
|
|
context['recent_maintenances'] = MaintenanceRecord.objects.order_by('-maintenance_date')[:5]
|
|
context['recent_batteries'] = Battery.objects.order_by('-created_at')[:5]
|
|
|
|
ups_with_batteries = []
|
|
for ups in UPSHost.objects.prefetch_related('battery_set').all():
|
|
batteries = ups.battery_set.all()
|
|
if batteries:
|
|
for battery in batteries:
|
|
ups_with_batteries.append({
|
|
'ups_brand': ups.brand,
|
|
'ups_model': ups.model,
|
|
'ups_quantity': ups.quantity,
|
|
'battery_brand': battery.brand,
|
|
'battery_location': battery.location,
|
|
'battery_model': battery.model,
|
|
'battery_weight': battery.weight,
|
|
'battery_quantity': battery.quantity,
|
|
})
|
|
else:
|
|
ups_with_batteries.append({
|
|
'ups_brand': ups.brand,
|
|
'ups_model': ups.model,
|
|
'ups_quantity': ups.quantity,
|
|
'battery_brand': '-',
|
|
'battery_location': ups.location,
|
|
'battery_model': '-',
|
|
'battery_weight': '-',
|
|
'battery_quantity': '-',
|
|
})
|
|
context['ups_with_batteries'] = ups_with_batteries
|
|
|
|
locations = {}
|
|
for ups in UPSHost.objects.all():
|
|
if ups.location not in locations:
|
|
locations[ups.location] = {
|
|
'ups_count': 0,
|
|
'battery_count': 0,
|
|
'total_weight': 0,
|
|
'ups_models': []
|
|
}
|
|
locations[ups.location]['ups_count'] += ups.quantity
|
|
locations[ups.location]['ups_models'].append(f"{ups.brand} {ups.model}")
|
|
|
|
for battery in ups.battery_set.all():
|
|
locations[ups.location]['battery_count'] += battery.quantity
|
|
locations[ups.location]['total_weight'] += battery.weight * battery.quantity
|
|
|
|
context['location_summary'] = sorted(locations.items(), key=lambda x: x[0])
|
|
return context
|
|
|
|
|
|
class UPSHostListView(ListView):
|
|
model = UPSHost
|
|
template_name = 'ups_manager/ups_list.html'
|
|
context_object_name = 'ups_list'
|
|
paginate_by = 10
|
|
|
|
def get_queryset(self):
|
|
queryset = super().get_queryset().prefetch_related('battery_set')
|
|
brand = self.request.GET.get('brand')
|
|
model = self.request.GET.get('model')
|
|
location = self.request.GET.get('location')
|
|
contact = self.request.GET.get('contact')
|
|
|
|
if brand:
|
|
queryset = queryset.filter(brand__icontains=brand)
|
|
if model:
|
|
queryset = queryset.filter(model__icontains=model)
|
|
if location:
|
|
queryset = queryset.filter(location__icontains=location)
|
|
if contact:
|
|
queryset = queryset.filter(contact__name__icontains=contact)
|
|
|
|
return queryset.order_by('-created_at')
|
|
|
|
|
|
class UPSHostCreateView(CreateView):
|
|
model = UPSHost
|
|
template_name = 'ups_manager/ups_form.html'
|
|
form_class = UPSHostForm
|
|
success_url = reverse_lazy('ups_list')
|
|
|
|
|
|
class UPSHostUpdateView(UpdateView):
|
|
model = UPSHost
|
|
template_name = 'ups_manager/ups_form.html'
|
|
form_class = UPSHostForm
|
|
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 render(self.request, self.template_name, {
|
|
'form': form,
|
|
'battery_formset': battery_formset,
|
|
'object': self.object,
|
|
})
|
|
|
|
|
|
class UPSHostDeleteView(DeleteView):
|
|
model = UPSHost
|
|
template_name = 'ups_manager/ups_confirm_delete.html'
|
|
success_url = reverse_lazy('ups_list')
|
|
|
|
|
|
class BatteryListView(ListView):
|
|
model = Battery
|
|
template_name = 'ups_manager/battery_list.html'
|
|
context_object_name = 'battery_list'
|
|
paginate_by = 10
|
|
|
|
def get_queryset(self):
|
|
queryset = super().get_queryset()
|
|
model = self.request.GET.get('model')
|
|
brand = self.request.GET.get('brand')
|
|
location = self.request.GET.get('location')
|
|
|
|
if model:
|
|
queryset = queryset.filter(model__icontains=model)
|
|
if brand:
|
|
queryset = queryset.filter(brand__icontains=brand)
|
|
if location:
|
|
queryset = queryset.filter(location__icontains=location)
|
|
|
|
return queryset.order_by('-created_at')
|
|
|
|
|
|
class BatteryCreateView(CreateView):
|
|
model = Battery
|
|
template_name = 'ups_manager/battery_form.html'
|
|
fields = ['brand', 'model', 'weight', 'quantity', 'location', 'install_date', 'last_maintenance_date', 'ups_host']
|
|
success_url = reverse_lazy('battery_list')
|
|
|
|
|
|
class BatteryUpdateView(UpdateView):
|
|
model = Battery
|
|
template_name = 'ups_manager/battery_form.html'
|
|
fields = ['brand', 'model', 'weight', 'quantity', 'location', 'install_date', 'last_maintenance_date', 'ups_host']
|
|
success_url = reverse_lazy('battery_list')
|
|
|
|
|
|
class BatteryDeleteView(DeleteView):
|
|
model = Battery
|
|
template_name = 'ups_manager/battery_confirm_delete.html'
|
|
success_url = reverse_lazy('battery_list')
|
|
|
|
|
|
class ContactListView(ListView):
|
|
model = Contact
|
|
template_name = 'ups_manager/contact_list.html'
|
|
context_object_name = 'contact_list'
|
|
paginate_by = 10
|
|
|
|
def get_queryset(self):
|
|
queryset = super().get_queryset()
|
|
name = self.request.GET.get('name')
|
|
phone = self.request.GET.get('phone')
|
|
|
|
if name:
|
|
queryset = queryset.filter(name__icontains=name)
|
|
if phone:
|
|
queryset = queryset.filter(phone__icontains=phone)
|
|
|
|
return queryset.order_by('-created_at')
|
|
|
|
|
|
class ContactCreateView(CreateView):
|
|
model = Contact
|
|
template_name = 'ups_manager/contact_form.html'
|
|
fields = ['name', 'phone', 'email']
|
|
success_url = reverse_lazy('contact_list')
|
|
|
|
|
|
class ContactUpdateView(UpdateView):
|
|
model = Contact
|
|
template_name = 'ups_manager/contact_form.html'
|
|
fields = ['name', 'phone', 'email']
|
|
success_url = reverse_lazy('contact_list')
|
|
|
|
|
|
class ContactDeleteView(DeleteView):
|
|
model = Contact
|
|
template_name = 'ups_manager/contact_confirm_delete.html'
|
|
success_url = reverse_lazy('contact_list')
|
|
|
|
|
|
class SupplierListView(ListView):
|
|
model = Supplier
|
|
template_name = 'ups_manager/supplier_list.html'
|
|
context_object_name = 'supplier_list'
|
|
paginate_by = 10
|
|
|
|
def get_queryset(self):
|
|
queryset = super().get_queryset()
|
|
company_name = self.request.GET.get('company_name')
|
|
contact_person = self.request.GET.get('contact_person')
|
|
|
|
if company_name:
|
|
queryset = queryset.filter(company_name__icontains=company_name)
|
|
if contact_person:
|
|
queryset = queryset.filter(contact_person__icontains=contact_person)
|
|
|
|
return queryset.order_by('-created_at')
|
|
|
|
|
|
class SupplierCreateView(CreateView):
|
|
model = Supplier
|
|
template_name = 'ups_manager/supplier_form.html'
|
|
fields = ['company_name', 'contact_person', 'phone', 'email', 'address', 'remark']
|
|
success_url = reverse_lazy('supplier_list')
|
|
|
|
|
|
class SupplierUpdateView(UpdateView):
|
|
model = Supplier
|
|
template_name = 'ups_manager/supplier_form.html'
|
|
fields = ['company_name', 'contact_person', 'phone', 'email', 'address', 'remark']
|
|
success_url = reverse_lazy('supplier_list')
|
|
|
|
|
|
class SupplierDeleteView(DeleteView):
|
|
model = Supplier
|
|
template_name = 'ups_manager/supplier_confirm_delete.html'
|
|
success_url = reverse_lazy('supplier_list')
|
|
|
|
|
|
class MaintenanceListView(ListView):
|
|
model = MaintenanceRecord
|
|
template_name = 'ups_manager/maintenance_list.html'
|
|
context_object_name = 'maintenance_list'
|
|
paginate_by = 10
|
|
|
|
def get_queryset(self):
|
|
queryset = super().get_queryset()
|
|
ups_host = self.request.GET.get('ups_host')
|
|
technician = self.request.GET.get('technician')
|
|
|
|
if ups_host:
|
|
queryset = queryset.filter(ups_host__model__icontains=ups_host)
|
|
if technician:
|
|
queryset = queryset.filter(technician__icontains=technician)
|
|
|
|
return queryset.order_by('-maintenance_date')
|
|
|
|
|
|
class MaintenanceCreateView(CreateView):
|
|
model = MaintenanceRecord
|
|
template_name = 'ups_manager/maintenance_form.html'
|
|
fields = ['ups_host', 'battery', 'supplier', 'maintenance_date', 'technician', 'content']
|
|
success_url = reverse_lazy('maintenance_list')
|
|
|
|
|
|
class MaintenanceUpdateView(UpdateView):
|
|
model = MaintenanceRecord
|
|
template_name = 'ups_manager/maintenance_form.html'
|
|
fields = ['ups_host', 'battery', 'supplier', 'maintenance_date', 'technician', 'content']
|
|
success_url = reverse_lazy('maintenance_list')
|
|
|
|
|
|
class MaintenanceDeleteView(DeleteView):
|
|
model = MaintenanceRecord
|
|
template_name = 'ups_manager/maintenance_confirm_delete.html'
|
|
success_url = reverse_lazy('maintenance_list')
|