From ed733b8bb7b4bf64c266a1516eab4b60d3bbcc25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 9 Nov 2021 16:35:11 +0100 Subject: [PATCH] Start Component pages --- shimatta_kenkyusho/parts/models.py | 7 +++ shimatta_kenkyusho/parts/urls.py | 5 +- shimatta_kenkyusho/parts/views.py | 39 ++++++++++-- shimatta_kenkyusho/templates/base.html | 1 + .../templates/parts/components-detail.html | 8 +++ .../templates/parts/components.html | 59 ++++++++++++++++++- 6 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 shimatta_kenkyusho/templates/parts/components-detail.html diff --git a/shimatta_kenkyusho/parts/models.py b/shimatta_kenkyusho/parts/models.py index cb19175..ea43b3e 100644 --- a/shimatta_kenkyusho/parts/models.py +++ b/shimatta_kenkyusho/parts/models.py @@ -182,6 +182,13 @@ class Component(models.Model): def get_qr_code(self): qrdata = '[comp_uuid]' + str(self.id) return qrdata + + def get_total_amount(self): + stocks = Stock.objects.filter(component=self) + sum = stocks.aggregate(Sum('amount'))['amount__sum'] + if sum is None: + sum = 0 + return sum class ComponentParameter(models.Model): diff --git a/shimatta_kenkyusho/parts/urls.py b/shimatta_kenkyusho/parts/urls.py index 590182a..95fe1f7 100644 --- a/shimatta_kenkyusho/parts/urls.py +++ b/shimatta_kenkyusho/parts/urls.py @@ -7,6 +7,7 @@ urlpatterns = [ 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'), - path('changepw/', parts_views.ChangePasswordView.as_view(), name='parts-change-pw'), - path('stocks/', parts_views.StockViewDetail.as_view(), name='parts-stocks-detail'), + path('changepw/', parts_views.ChangePasswordView.as_view(), name='change-pw'), + path('stocks//', parts_views.StockViewDetail.as_view(), name='parts-stocks-detail'), + path('components//', parts_views.ComponentDetailView.as_view(), name='parts-components-detail'), ] diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 6f16cb6..0d4b99f 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -11,7 +11,7 @@ from django.views import View import django.forms as forms from django.views.generic import TemplateView, DetailView from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin -from .models import Storage, Stock +from .models import Storage, Stock, Component, Distributor, Manufacturer, Package from .qr_parser import QrCodeValidator from django.core.paginator import Paginator from django.core.exceptions import ValidationError @@ -66,8 +66,6 @@ class ChangePasswordView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): return context def post(self, request, *args, **kwargs): - - if 'submit-change-pw' not in request.POST: return super().post(request, *args, **kwargs) @@ -137,6 +135,22 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): template_name = 'parts/components.html' base_title = 'Components' navbar_selected = 'Components' + default_page_size = 10 + + + 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 StockView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): template_name = 'parts/stocks.html' @@ -364,4 +378,21 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): elif 'submit-add-stock' in request.POST: return self.handle_add_stock_post(request, **kwargs) - return super().post(request, *args, **kwargs) \ No newline at end of file + return super().post(request, *args, **kwargs) + +class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): + template_name = 'parts/components-detail.html' + model = Component + pk_url_kwarg = 'uuid' + base_title = '' + navbar_selected = 'Components' + + def get_context_data(self, **kwargs): + self.base_title = 'Component / '+self.object.name + context = super().get_context_data(**kwargs) + context['component'] = self.object + + + + return context + diff --git a/shimatta_kenkyusho/templates/base.html b/shimatta_kenkyusho/templates/base.html index ac10fed..7253335 100644 --- a/shimatta_kenkyusho/templates/base.html +++ b/shimatta_kenkyusho/templates/base.html @@ -36,6 +36,7 @@