2024-11-18 21:19:59 +01:00
|
|
|
from django.shortcuts import redirect
|
2024-11-18 20:56:23 +01:00
|
|
|
from django.views.generic import TemplateView, DetailView
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
from django.db.models import ProtectedError
|
|
|
|
from django.db.models import Q
|
2024-11-18 21:28:37 +01:00
|
|
|
from ..forms import *
|
|
|
|
from ..models import Package
|
2024-11-18 20:56:23 +01:00
|
|
|
from .generic_views import BaseTemplateMixin
|
|
|
|
|
|
|
|
class PackageView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
|
|
|
template_name = 'parts/packages.html'
|
|
|
|
base_title = 'Packages'
|
|
|
|
navbar_selected = 'Packages'
|
|
|
|
default_page_size = 25
|
|
|
|
|
|
|
|
def search_packages(self, search):
|
|
|
|
qs = Package.objects.all()
|
|
|
|
|
|
|
|
if not search:
|
|
|
|
return qs
|
2024-11-18 21:19:59 +01:00
|
|
|
|
2024-11-18 20:56:23 +01:00
|
|
|
search_fragments = search.strip().split()
|
|
|
|
for search in search_fragments:
|
|
|
|
if search.lower() == 'smd':
|
|
|
|
s_filter = Q(name__icontains = search) | Q(smd = True)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
pin_count = int(search)
|
|
|
|
s_filter = Q(name__icontains = search) | Q(pin_count=pin_count)
|
|
|
|
except:
|
|
|
|
s_filter = Q(name__icontains = search)
|
|
|
|
|
|
|
|
qs = qs.filter(s_filter)
|
|
|
|
return qs
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
page_num = self.request.GET.get('page', default=1)
|
|
|
|
search_string = self.request.GET.get('search', default=None)
|
2024-11-18 21:19:59 +01:00
|
|
|
|
2024-11-18 20:56:23 +01:00
|
|
|
package_queryset = self.search_packages(search_string)
|
|
|
|
|
2024-11-18 21:19:59 +01:00
|
|
|
paginator = Paginator(package_queryset, self.default_page_size)
|
2024-11-18 20:56:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
context['search_string'] = search_string
|
|
|
|
context['packages'] = paginator.get_page(page_num)
|
|
|
|
context['new_pkg_form'] = PackageForm()
|
|
|
|
|
|
|
|
return context
|
2024-11-18 21:19:59 +01:00
|
|
|
|
2024-11-18 20:56:23 +01:00
|
|
|
def handle_add_new_package(self, request):
|
|
|
|
|
|
|
|
form = PackageForm(data=request.POST, files=request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
|
|
|
|
context = self.get_context_data()
|
|
|
|
|
|
|
|
if not form.is_valid():
|
|
|
|
context['new_pkg_form'] = form
|
|
|
|
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
if 'submit-pkg-add-new' in request.POST:
|
|
|
|
return self.handle_add_new_package(request)
|
|
|
|
|
|
|
|
return super().post(request, *args, **kwargs)
|
2024-11-18 21:19:59 +01:00
|
|
|
|
2024-11-18 20:56:23 +01:00
|
|
|
|
|
|
|
class PackageDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
|
|
|
template_name = 'parts/packages-detail.html'
|
|
|
|
model = Package
|
|
|
|
pk_url_kwarg = 'uuid'
|
|
|
|
base_title = ''
|
|
|
|
navbar_selected = 'Packages'
|
2024-11-18 21:19:59 +01:00
|
|
|
|
2024-11-18 20:56:23 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
self.base_title = 'Package / '+self.object.name
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['package'] = self.object
|
|
|
|
context['edit_form'] = PackageForm(instance=self.object)
|
2024-11-18 21:19:59 +01:00
|
|
|
|
2024-11-18 20:56:23 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
def handle_delete_package(self, request):
|
|
|
|
delete_error = None
|
|
|
|
protected_objects = None
|
2024-11-18 21:19:59 +01:00
|
|
|
|
2024-11-18 20:56:23 +01:00
|
|
|
# Try to delete this instance
|
|
|
|
try:
|
|
|
|
self.object.delete()
|
|
|
|
except ProtectedError as pe:
|
|
|
|
delete_error = 'Cannot delete this package. It is referenced by a component.'
|
|
|
|
protected_objects = pe.protected_objects
|
|
|
|
except:
|
|
|
|
delete_error = 'Cannot delete this package. Unknown error'
|
2024-11-18 21:19:59 +01:00
|
|
|
|
2024-11-18 20:56:23 +01:00
|
|
|
if delete_error:
|
|
|
|
context = self.get_context_data()
|
|
|
|
context['delete_error'] = delete_error
|
|
|
|
context['protected_components'] = protected_objects
|
|
|
|
return self.render_to_response(context)
|
|
|
|
else:
|
|
|
|
return redirect('parts-packages')
|
|
|
|
|
|
|
|
def edit_package(self, request):
|
|
|
|
edit_form = PackageForm(data=request.POST, files=request.FILES, instance=self.object)
|
|
|
|
|
|
|
|
if edit_form.is_valid():
|
|
|
|
edit_form.save()
|
|
|
|
|
|
|
|
context = self.get_context_data()
|
|
|
|
if not edit_form.is_valid():
|
|
|
|
context['edit_form'] = edit_form
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
|
|
|
|
if 'submit-pkg-delete' in request.POST:
|
|
|
|
return self.handle_delete_package(request)
|
|
|
|
elif 'submit-pkg-edit' in request.POST:
|
|
|
|
return self.edit_package(request)
|
2024-11-18 21:19:59 +01:00
|
|
|
|
2024-11-18 20:56:23 +01:00
|
|
|
return super().post(request, *args, **kwargs)
|