diff --git a/shimatta_kenkyusho/parts/views/component_views.py b/shimatta_kenkyusho/parts/views/component_views.py index fd0adb8..76eed61 100644 --- a/shimatta_kenkyusho/parts/views/component_views.py +++ b/shimatta_kenkyusho/parts/views/component_views.py @@ -1,24 +1,16 @@ -from django.shortcuts import render, redirect +import uuid +from django.shortcuts import redirect from django.urls import reverse -from django.contrib.auth import logout, login -from django.contrib.auth.forms import AuthenticationForm as AuthForm -from django.contrib.auth.forms import PasswordChangeForm -from django.contrib.auth import update_session_auth_hash -import django.forms as forms from django.views.generic import TemplateView, DetailView from django.contrib.auth.mixins import LoginRequiredMixin -from ..models import Storage, Stock, Component, Distributor, Manufacturer, Package, ComponentParameter, DistributorNum from django.core.paginator import Paginator -from django.core.exceptions import ValidationError +from django.db.models import Q +from django.forms import formset_factory from django.db import IntegrityError from django.db.models import ProtectedError +from ..models import Stock, Component, ComponentParameter, DistributorNum from ..forms import * from .component_import import import_components_from_csv -from django.db.models import Q -from django.db.models.functions import Lower -from django.forms import formset_factory -from django.http import HttpResponse -import uuid from .generic_views import BaseTemplateMixin @@ -34,7 +26,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): def get_component_query_set(self, search_string): queryset = Component.objects.all() - + if not search_string: return queryset @@ -76,14 +68,15 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): context['advanced_search_form'] = advanced_search context['advanced_search_param_formset'] = parameter_formset if advanced_search.is_valid(): - paginator_queryset = self.get_component_queryset_from_advanced_search(advanced_search.cleaned_data) + paginator_queryset = self.get_component_queryset_from_advanced_search( + advanced_search.cleaned_data) else: paginator_queryset = Component.objects.all() if parameter_formset.is_valid(): # Process parameters pass - + else: search = self.request.GET.get('search', default=None) paginator_queryset = self.get_component_query_set(search) @@ -103,9 +96,9 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): context['advanced_search_form'] = AdvancedComponentSearchForm(auto_id='adv_search_%s') return context - + def get_context_data(self, **kwargs): - return self.get_context_data_int(advanced_search = None, parameter_formset=None, **kwargs) + return self.get_context_data_int(advanced_search = None, parameter_formset=None, **kwargs) def handle_new_component_post(self, request, open=False, **kwargs): cform = ComponentForm(data=request.POST, files=request.FILES) @@ -136,7 +129,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): return self.render_to_response(context) def handle_advanced_search_post(self, request, **kwargs): - + form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=request.POST) param_formset = ParameterSearchFormSet(data=request.POST) @@ -161,7 +154,6 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): return self.handle_advanced_search_post(request, **kwargs) else: return super().post(request, *args, **kwargs) - class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): template_name = 'parts/components-detail.html' @@ -169,7 +161,7 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): 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) @@ -178,8 +170,10 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): context['comp_form'] = ComponentForm(instance=self.object) context['new_distri_num_form'] = DistributorNumberCreateForm() context['new_param_form'] = ComponentParameterCreateForm() - context['distri_nums'] = DistributorNum.objects.filter(component=self.object).order_by('distributor__name') - context['parameters'] = ComponentParameter.objects.filter(component=self.object).order_by('parameter_type__parameter_name') + context['distri_nums'] = DistributorNum.objects.filter(component=self.object).order_by( + 'distributor__name') + context['parameters'] = ComponentParameter.objects.filter(component=self.object).order_by( + 'parameter_type__parameter_name') return context @@ -187,7 +181,7 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): cform = ComponentForm(instance=self.object, data=request.POST, files=request.FILES) if cform.is_valid(): cform.save() - + context = self.get_context_data(**kwargs) if not cform.is_valid(): context['comp_form'] = cform @@ -228,7 +222,7 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): if not form.is_valid(): context['new_distri_num_form'] = form return self.render_to_response(context) - + def handle_submit_delete_distri_num_post(self, request, **kwargs): form = DistributorNumberDeleteForm(data=request.POST) if form.is_valid(): @@ -236,18 +230,18 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): context = self.get_context_data(**kwargs) return self.render_to_response(context) - + def handle_submit_delete_param_post(self, request, **kwargs): form = ComponentParameterDeleteForm(data=request.POST) if form.is_valid(): form.cleaned_data['param_num'].delete() - + context = self.get_context_data(**kwargs) return self.render_to_response(context) - + def handle_submit_new_param_post(self, request, **kwargs): form = ComponentParameterCreateForm(data=request.POST) - + if form.is_valid(): try: form.save(self.object)