diff --git a/shimatta_kenkyusho/parts/navbar.py b/shimatta_kenkyusho/parts/navbar.py index bc27446..fd2f45b 100644 --- a/shimatta_kenkyusho/parts/navbar.py +++ b/shimatta_kenkyusho/parts/navbar.py @@ -34,6 +34,7 @@ class NavBar(): items = { 'Main': BsNavBarItem('Main', reverse('parts-main'), False), 'Components':BsNavBarItem('Components', reverse('parts-components'), False), + 'Packages': BsNavBarItem('Packages', reverse('parts-packages'), False), 'Stocks':BsNavBarItem('Stocks', reverse('parts-stocks'), False), } @@ -45,6 +46,7 @@ class NavBar(): navitems = [ items['Main'], items['Components'], + items['Packages'], items['Stocks'], #items['Login'], ] diff --git a/shimatta_kenkyusho/parts/urls.py b/shimatta_kenkyusho/parts/urls.py index 0070fab..e448178 100644 --- a/shimatta_kenkyusho/parts/urls.py +++ b/shimatta_kenkyusho/parts/urls.py @@ -4,6 +4,7 @@ from . import views as parts_views urlpatterns = [ path('', parts_views.MainView.as_view(), name='parts-main'), path('components/', parts_views.ComponentView.as_view(), name='parts-components'), + path('packages/', parts_views.PackageView.as_view(), name='parts-packages'), path('stocks/', parts_views.StockView.as_view(), name='parts-stocks'), path('logout/', parts_views.logout_view, name='logout'), path('login/', parts_views.login_view, name='login'), diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index d1d8788..6e9b4e7 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -137,22 +137,70 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): template_name = 'parts/components.html' base_title = 'Components' navbar_selected = 'Components' - default_page_size = 10 - + default_page_size = 25 def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) comp_page_num = self.request.GET.get('comp_page', default=1) - pkg_page_num = self.request.GET.get('pkg_page', default= 1) comp_paginator = Paginator(Component.objects.all(), self.default_page_size) - pkg_paginator = Paginator(Package.objects.all(), self.default_page_size) context['components'] = comp_paginator.get_page(comp_page_num) - context['packages'] = pkg_paginator.get_page(pkg_page_num) return context +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 + + search = search.strip() + + qs = qs.filter(Q(name__contains = search)) + 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) + + package_queryset = self.search_packages(search_string) + + paginator = Paginator(package_queryset, self.default_page_size) + + + context['search_string'] = search_string + context['packages'] = paginator.get_page(page_num) + context['new_pkg_form'] = PackageForm() + + return context + + 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) + class StockView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): template_name = 'parts/stocks.html' @@ -522,7 +570,7 @@ class PackageDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): model = Package pk_url_kwarg = 'uuid' base_title = '' - navbar_selected = 'Components' + navbar_selected = 'Packages' def get_context_data(self, **kwargs): self.base_title = 'Package / '+self.object.name @@ -551,7 +599,7 @@ class PackageDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): context['protected_components'] = protected_objects return self.render_to_response(context) else: - return redirect('parts-main') + return redirect('parts-packages') def edit_package(self, request): edit_form = PackageForm(data=request.POST, files=request.FILES, instance=self.object) diff --git a/shimatta_kenkyusho/templates/parts/components.html b/shimatta_kenkyusho/templates/parts/components.html index ef5a4e4..9e4b78e 100644 --- a/shimatta_kenkyusho/templates/parts/components.html +++ b/shimatta_kenkyusho/templates/parts/components.html @@ -33,32 +33,6 @@ {% include 'paginator.html' with paginator=components get_param='comp_page' aria_label='Component Page Navigation' %} -