277 lines
9.8 KiB
Python
277 lines
9.8 KiB
Python
from django.shortcuts import render, redirect
|
|
from django.views.generic import ListView, CreateView, UpdateView, DeleteView
|
|
from django.urls import reverse_lazy
|
|
from .models import UPSHost, Battery, Contact, Supplier, MaintenanceRecord
|
|
|
|
|
|
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()
|
|
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'
|
|
fields = ['brand', 'model', 'ip_address', 'quantity', 'location', 'last_maintenance_date', 'contact']
|
|
success_url = reverse_lazy('ups_list')
|
|
|
|
|
|
class UPSHostUpdateView(UpdateView):
|
|
model = UPSHost
|
|
template_name = 'ups_manager/ups_form.html'
|
|
fields = ['brand', 'model', 'ip_address', 'quantity', 'location', 'last_maintenance_date', 'contact']
|
|
success_url = reverse_lazy('ups_list')
|
|
|
|
|
|
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 MaintenanceRecordListView(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_model = self.request.GET.get('ups_model')
|
|
technician = self.request.GET.get('technician')
|
|
supplier = self.request.GET.get('supplier')
|
|
|
|
if ups_model:
|
|
queryset = queryset.filter(ups_host__model__icontains=ups_model)
|
|
if technician:
|
|
queryset = queryset.filter(technician__icontains=technician)
|
|
if supplier:
|
|
queryset = queryset.filter(supplier__company_name__icontains=supplier)
|
|
|
|
return queryset.order_by('-maintenance_date')
|
|
|
|
|
|
class MaintenanceRecordCreateView(CreateView):
|
|
model = MaintenanceRecord
|
|
template_name = 'ups_manager/maintenance_form.html'
|
|
fields = ['ups_host', 'battery', 'supplier', 'maintenance_date', 'content', 'technician']
|
|
success_url = reverse_lazy('maintenance_list')
|
|
|
|
|
|
class MaintenanceRecordUpdateView(UpdateView):
|
|
model = MaintenanceRecord
|
|
template_name = 'ups_manager/maintenance_form.html'
|
|
fields = ['ups_host', 'battery', 'supplier', 'maintenance_date', 'content', 'technician']
|
|
success_url = reverse_lazy('maintenance_list')
|
|
|
|
|
|
class MaintenanceRecordDeleteView(DeleteView):
|
|
model = MaintenanceRecord
|
|
template_name = 'ups_manager/maintenance_confirm_delete.html'
|
|
success_url = reverse_lazy('maintenance_list')
|