Fixup import of component_views.py

This commit is contained in:
Mario Hüttel 2024-11-18 21:08:55 +01:00
parent 173f0e3c91
commit b3f8041f08

View File

@ -1,24 +1,16 @@
from django.shortcuts import render, redirect import uuid
from django.shortcuts import redirect
from django.urls import reverse 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.views.generic import TemplateView, DetailView
from django.contrib.auth.mixins import LoginRequiredMixin 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.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 import IntegrityError
from django.db.models import ProtectedError from django.db.models import ProtectedError
from ..models import Stock, Component, ComponentParameter, DistributorNum
from ..forms import * from ..forms import *
from .component_import import import_components_from_csv 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 from .generic_views import BaseTemplateMixin
@ -34,7 +26,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
def get_component_query_set(self, search_string): def get_component_query_set(self, search_string):
queryset = Component.objects.all() queryset = Component.objects.all()
if not search_string: if not search_string:
return queryset return queryset
@ -76,14 +68,15 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
context['advanced_search_form'] = advanced_search context['advanced_search_form'] = advanced_search
context['advanced_search_param_formset'] = parameter_formset context['advanced_search_param_formset'] = parameter_formset
if advanced_search.is_valid(): 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: else:
paginator_queryset = Component.objects.all() paginator_queryset = Component.objects.all()
if parameter_formset.is_valid(): if parameter_formset.is_valid():
# Process parameters # Process parameters
pass pass
else: else:
search = self.request.GET.get('search', default=None) search = self.request.GET.get('search', default=None)
paginator_queryset = self.get_component_query_set(search) 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') context['advanced_search_form'] = AdvancedComponentSearchForm(auto_id='adv_search_%s')
return context return context
def get_context_data(self, **kwargs): 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): def handle_new_component_post(self, request, open=False, **kwargs):
cform = ComponentForm(data=request.POST, files=request.FILES) cform = ComponentForm(data=request.POST, files=request.FILES)
@ -136,7 +129,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
return self.render_to_response(context) return self.render_to_response(context)
def handle_advanced_search_post(self, request, **kwargs): def handle_advanced_search_post(self, request, **kwargs):
form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=request.POST) form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=request.POST)
param_formset = ParameterSearchFormSet(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) return self.handle_advanced_search_post(request, **kwargs)
else: else:
return super().post(request, *args, **kwargs) return super().post(request, *args, **kwargs)
class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
template_name = 'parts/components-detail.html' template_name = 'parts/components-detail.html'
@ -169,7 +161,7 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
pk_url_kwarg = 'uuid' pk_url_kwarg = 'uuid'
base_title = '' base_title = ''
navbar_selected = 'Components' navbar_selected = 'Components'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
self.base_title = 'Component / '+self.object.name self.base_title = 'Component / '+self.object.name
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
@ -178,8 +170,10 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
context['comp_form'] = ComponentForm(instance=self.object) context['comp_form'] = ComponentForm(instance=self.object)
context['new_distri_num_form'] = DistributorNumberCreateForm() context['new_distri_num_form'] = DistributorNumberCreateForm()
context['new_param_form'] = ComponentParameterCreateForm() context['new_param_form'] = ComponentParameterCreateForm()
context['distri_nums'] = DistributorNum.objects.filter(component=self.object).order_by('distributor__name') context['distri_nums'] = DistributorNum.objects.filter(component=self.object).order_by(
context['parameters'] = ComponentParameter.objects.filter(component=self.object).order_by('parameter_type__parameter_name') 'distributor__name')
context['parameters'] = ComponentParameter.objects.filter(component=self.object).order_by(
'parameter_type__parameter_name')
return context return context
@ -187,7 +181,7 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
cform = ComponentForm(instance=self.object, data=request.POST, files=request.FILES) cform = ComponentForm(instance=self.object, data=request.POST, files=request.FILES)
if cform.is_valid(): if cform.is_valid():
cform.save() cform.save()
context = self.get_context_data(**kwargs) context = self.get_context_data(**kwargs)
if not cform.is_valid(): if not cform.is_valid():
context['comp_form'] = cform context['comp_form'] = cform
@ -228,7 +222,7 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
if not form.is_valid(): if not form.is_valid():
context['new_distri_num_form'] = form context['new_distri_num_form'] = form
return self.render_to_response(context) return self.render_to_response(context)
def handle_submit_delete_distri_num_post(self, request, **kwargs): def handle_submit_delete_distri_num_post(self, request, **kwargs):
form = DistributorNumberDeleteForm(data=request.POST) form = DistributorNumberDeleteForm(data=request.POST)
if form.is_valid(): if form.is_valid():
@ -236,18 +230,18 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
context = self.get_context_data(**kwargs) context = self.get_context_data(**kwargs)
return self.render_to_response(context) return self.render_to_response(context)
def handle_submit_delete_param_post(self, request, **kwargs): def handle_submit_delete_param_post(self, request, **kwargs):
form = ComponentParameterDeleteForm(data=request.POST) form = ComponentParameterDeleteForm(data=request.POST)
if form.is_valid(): if form.is_valid():
form.cleaned_data['param_num'].delete() form.cleaned_data['param_num'].delete()
context = self.get_context_data(**kwargs) context = self.get_context_data(**kwargs)
return self.render_to_response(context) return self.render_to_response(context)
def handle_submit_new_param_post(self, request, **kwargs): def handle_submit_new_param_post(self, request, **kwargs):
form = ComponentParameterCreateForm(data=request.POST) form = ComponentParameterCreateForm(data=request.POST)
if form.is_valid(): if form.is_valid():
try: try:
form.save(self.object) form.save(self.object)