2021-07-30 20:52:18 +02:00
|
|
|
from django.shortcuts import render, redirect
|
2024-11-10 20:46:45 +01:00
|
|
|
from django.urls import reverse
|
2021-08-07 17:37:36 +02:00
|
|
|
from django.contrib.auth import logout, login
|
2021-07-30 20:52:18 +02:00
|
|
|
from .navbar import NavBar
|
2021-08-07 17:37:36 +02:00
|
|
|
from django.contrib.auth.forms import AuthenticationForm as AuthForm
|
2021-11-09 14:50:57 +01:00
|
|
|
from django.contrib.auth.forms import PasswordChangeForm
|
|
|
|
from django.contrib.auth import update_session_auth_hash
|
2021-08-07 17:37:36 +02:00
|
|
|
import django.forms as forms
|
|
|
|
from django.views.generic import TemplateView, DetailView
|
2024-11-17 19:54:15 +01:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from .models import Storage, Stock, Component, Distributor, Manufacturer, Package, ComponentParameter, DistributorNum
|
2021-08-07 17:37:36 +02:00
|
|
|
from .qr_parser import QrCodeValidator
|
2021-08-14 00:25:19 +02:00
|
|
|
from django.core.paginator import Paginator
|
2021-10-24 21:14:11 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2021-11-11 20:51:02 +01:00
|
|
|
from django.db import IntegrityError
|
2021-11-14 16:48:34 +01:00
|
|
|
from django.db.models import ProtectedError
|
|
|
|
from .forms import *
|
2024-11-17 19:54:15 +01:00
|
|
|
from .component_import import import_components_from_csv
|
2021-10-31 20:57:28 +01:00
|
|
|
from django.db.models import Q
|
2021-11-01 22:17:20 +01:00
|
|
|
from django.db.models.functions import Lower
|
2022-01-03 13:18:31 +01:00
|
|
|
from django.forms import formset_factory
|
2024-11-17 19:10:28 +01:00
|
|
|
from django.http import HttpResponse
|
2021-10-31 20:57:28 +01:00
|
|
|
import uuid
|
2021-08-07 17:37:36 +02:00
|
|
|
|
2024-11-17 19:10:28 +01:00
|
|
|
|
2022-01-03 13:18:31 +01:00
|
|
|
ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=1)
|
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
class QrSearchForm(forms.Form):
|
|
|
|
my_qr_validator = QrCodeValidator()
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
qr_search = forms.CharField(label='qr_search', validators=[my_qr_validator])
|
|
|
|
|
|
|
|
class BaseTemplateMixin(object):
|
|
|
|
navbar_selected = ''
|
|
|
|
base_title = ''
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
base_context = {
|
|
|
|
'navbar': NavBar.get_navbar(self.navbar_selected, self.request.user),
|
|
|
|
'title': NavBar.get_brand()+' / '+ self.base_title,
|
|
|
|
'login_active': False,
|
|
|
|
}
|
|
|
|
context['base'] = base_context
|
|
|
|
return context
|
2021-07-30 20:52:18 +02:00
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
data = request.POST
|
|
|
|
if 'qr_search' not in data:
|
|
|
|
super().post(request, *args, **kwargs)
|
2021-07-30 20:52:18 +02:00
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
print('QR',data['qr_search'])
|
|
|
|
f = QrSearchForm(data)
|
|
|
|
if f.is_valid():
|
|
|
|
return redirect(f.my_qr_validator.get_redirect_url(f.cleaned_data['qr_search']))
|
2021-07-30 20:52:18 +02:00
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
return self.get(request)
|
|
|
|
|
2021-11-09 14:50:57 +01:00
|
|
|
class ChangePasswordView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
|
|
|
template_name = 'parts/change-pw.html'
|
|
|
|
navbar_selected = 'Main'
|
|
|
|
base_title = 'Change Password'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['form'] = PasswordChangeForm(self.request.user)
|
|
|
|
return context
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
if 'submit-change-pw' not in request.POST:
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
form = PasswordChangeForm(request.user, data=request.POST)
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
|
|
user = form.save()
|
|
|
|
update_session_auth_hash(request, user)
|
|
|
|
return redirect('parts-main')
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
if form.errors:
|
|
|
|
context['form'] = form
|
|
|
|
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
class MainView(BaseTemplateMixin, TemplateView):
|
|
|
|
template_name = 'parts/main.html'
|
|
|
|
navbar_selected = 'Main'
|
|
|
|
base_title = 'Main'
|
2022-01-01 22:47:12 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['user'] = self.request.user
|
|
|
|
return context
|
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
def logout_view(request):
|
|
|
|
logout(request)
|
|
|
|
return redirect('parts-main')
|
|
|
|
|
|
|
|
def login_view(request):
|
2021-07-30 20:52:18 +02:00
|
|
|
base_context = {
|
2021-08-07 17:37:36 +02:00
|
|
|
'navbar': NavBar.get_navbar('Login', request.user),
|
|
|
|
'title': NavBar.get_brand()+' / '+'Login',
|
|
|
|
'login_active': True,
|
2021-07-30 20:52:18 +02:00
|
|
|
}
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
next_param = request.GET.get('next')
|
|
|
|
if next_param is not None:
|
|
|
|
return redirect(next_param)
|
|
|
|
else:
|
|
|
|
return redirect('parts-main')
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = AuthForm(data=request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
valid_user = form.get_user()
|
|
|
|
login(request, valid_user)
|
|
|
|
next_param = request.GET.get('next')
|
|
|
|
if next_param is not None:
|
|
|
|
return redirect(next_param)
|
|
|
|
else:
|
|
|
|
return redirect('parts-main')
|
|
|
|
else:
|
|
|
|
form = AuthForm()
|
|
|
|
|
|
|
|
|
2021-07-30 20:52:18 +02:00
|
|
|
context = {
|
|
|
|
'base': base_context,
|
2021-08-07 17:37:36 +02:00
|
|
|
'form': form,
|
2021-07-30 20:52:18 +02:00
|
|
|
}
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
return render(request, 'parts/login.html', context)
|
2021-07-30 20:52:18 +02:00
|
|
|
|
|
|
|
# Create your views here.
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
|
|
|
template_name = 'parts/components.html'
|
|
|
|
base_title = 'Components'
|
|
|
|
navbar_selected = 'Components'
|
2021-11-14 18:40:21 +01:00
|
|
|
default_page_size = 25
|
2021-11-09 16:35:11 +01:00
|
|
|
|
2022-01-02 17:57:00 +01:00
|
|
|
def get_component_query_set(self, search_string):
|
|
|
|
queryset = Component.objects.all()
|
|
|
|
|
|
|
|
if not search_string:
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
search_fragments = search_string.strip().split()
|
|
|
|
for search in search_fragments:
|
|
|
|
queryset = queryset.filter(Q(name__icontains = search) | Q(manufacturer__name__icontains = search) | Q(package__name__icontains = search))
|
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
2022-01-02 20:00:29 +01:00
|
|
|
def get_component_queryset_from_advanced_search(self, cleaned_data):
|
|
|
|
queryset = Component.objects.all()
|
|
|
|
|
|
|
|
if cleaned_data['name']:
|
|
|
|
queryset = queryset.filter(Q(name__icontains=cleaned_data['name']))
|
|
|
|
if cleaned_data['package']:
|
|
|
|
queryset = queryset.filter(package=cleaned_data['package'])
|
|
|
|
if cleaned_data['package_pin_count']:
|
|
|
|
queryset = queryset.filter(package__pin_count=cleaned_data['package_pin_count'])
|
|
|
|
if cleaned_data['component_type']:
|
|
|
|
queryset = queryset.filter(component_type=cleaned_data['component_type'])
|
|
|
|
if cleaned_data['distributor_num']:
|
|
|
|
if cleaned_data['distributor']:
|
|
|
|
distri = cleaned_data['distributor']
|
|
|
|
queryset = queryset.filter(Q(distributornum__distributor_part_number__icontains=cleaned_data['distributor_num']) & Q(distributornum__distributor=distri))
|
|
|
|
else:
|
|
|
|
queryset = queryset.filter(Q(distributornum__distributor_part_number__icontains=cleaned_data['distributor_num']))
|
|
|
|
if cleaned_data['manufacturer']:
|
|
|
|
queryset = queryset.filter(manufacturer=cleaned_data['manufacturer'])
|
|
|
|
return queryset
|
|
|
|
|
2022-01-03 13:18:31 +01:00
|
|
|
def get_context_data_int(self, advanced_search, parameter_formset : ParameterSearchFormSet, **kwargs):
|
2021-11-09 16:35:11 +01:00
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
comp_page_num = self.request.GET.get('comp_page', default=1)
|
|
|
|
|
2022-01-03 13:18:31 +01:00
|
|
|
if advanced_search and parameter_formset:
|
2022-01-02 20:00:29 +01:00
|
|
|
search = None
|
|
|
|
context['advanced_search_shown'] = True
|
|
|
|
context['advanced_search_form'] = advanced_search
|
2022-01-03 13:18:31 +01:00
|
|
|
context['advanced_search_param_formset'] = parameter_formset
|
2022-01-02 20:00:29 +01:00
|
|
|
if advanced_search.is_valid():
|
|
|
|
paginator_queryset = self.get_component_queryset_from_advanced_search(advanced_search.cleaned_data)
|
|
|
|
else:
|
|
|
|
paginator_queryset = Component.objects.all()
|
2022-01-03 13:18:31 +01:00
|
|
|
|
|
|
|
if parameter_formset.is_valid():
|
|
|
|
# Process parameters
|
|
|
|
pass
|
|
|
|
|
2022-01-02 20:00:29 +01:00
|
|
|
else:
|
|
|
|
search = self.request.GET.get('search', default=None)
|
|
|
|
paginator_queryset = self.get_component_query_set(search)
|
2022-01-02 17:57:00 +01:00
|
|
|
|
2022-01-02 20:00:29 +01:00
|
|
|
comp_paginator = Paginator(paginator_queryset, self.default_page_size)
|
2021-11-09 16:35:11 +01:00
|
|
|
|
|
|
|
context['components'] = comp_paginator.get_page(comp_page_num)
|
2022-01-01 17:53:10 +01:00
|
|
|
context['comp_form'] = ComponentForm()
|
2024-11-17 19:54:15 +01:00
|
|
|
context['import_comp_form'] = ImportComponentForm()
|
2022-01-02 17:57:00 +01:00
|
|
|
context['search_string'] = search
|
2022-01-03 13:18:31 +01:00
|
|
|
|
|
|
|
if not parameter_formset:
|
|
|
|
context['advanced_search_param_formset'] = ParameterSearchFormSet()
|
|
|
|
|
|
|
|
|
2022-01-02 20:00:29 +01:00
|
|
|
if not advanced_search:
|
|
|
|
context['advanced_search_form'] = AdvancedComponentSearchForm(auto_id='adv_search_%s')
|
2022-01-01 17:53:10 +01:00
|
|
|
|
2021-11-09 16:35:11 +01:00
|
|
|
return context
|
2022-01-02 20:00:29 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2022-01-03 13:18:31 +01:00
|
|
|
return self.get_context_data_int(advanced_search = None, parameter_formset=None, **kwargs)
|
2022-01-01 17:53:10 +01:00
|
|
|
|
2022-01-03 13:18:31 +01:00
|
|
|
def handle_new_component_post(self, request, open=False, **kwargs):
|
2022-01-01 17:53:10 +01:00
|
|
|
cform = ComponentForm(data=request.POST, files=request.FILES)
|
2022-01-03 13:18:31 +01:00
|
|
|
new_component = None
|
2022-01-01 17:53:10 +01:00
|
|
|
if cform.is_valid():
|
2022-01-03 13:18:31 +01:00
|
|
|
new_component = cform.save()
|
2022-01-01 17:53:10 +01:00
|
|
|
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
if not cform.is_valid():
|
|
|
|
context['comp_form'] = cform
|
2022-01-03 13:18:31 +01:00
|
|
|
|
|
|
|
if open and new_component:
|
|
|
|
return redirect(reverse('parts-components-detail', kwargs={'uuid':new_component.id}))
|
2022-01-01 17:53:10 +01:00
|
|
|
return self.render_to_response(context)
|
2024-11-17 19:54:15 +01:00
|
|
|
|
|
|
|
def handle_import_components_post(self, request, open=False, **kwargs):
|
|
|
|
cform = ImportComponentForm(data=request.POST, files=request.FILES)
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
if cform.is_valid():
|
|
|
|
try:
|
|
|
|
import_components_from_csv(cform.files['csv_file'].file)
|
|
|
|
except Exception as ex:
|
|
|
|
cform.add_error('csv_file', str(ex))
|
|
|
|
context['import_comp_form'] = cform
|
|
|
|
else:
|
|
|
|
context['import_comp_form'] = cform
|
|
|
|
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
2022-01-02 20:00:29 +01:00
|
|
|
def handle_advanced_search_post(self, request, **kwargs):
|
|
|
|
|
|
|
|
form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=request.POST)
|
2022-01-03 13:18:31 +01:00
|
|
|
param_formset = ParameterSearchFormSet(data=request.POST)
|
|
|
|
|
2022-01-02 20:00:29 +01:00
|
|
|
if form.is_valid():
|
|
|
|
print('Valid')
|
|
|
|
|
2022-01-03 13:18:31 +01:00
|
|
|
if param_formset.is_valid():
|
|
|
|
print('Formset is valid!')
|
|
|
|
|
2022-01-02 20:00:29 +01:00
|
|
|
|
2022-01-03 13:18:31 +01:00
|
|
|
context = self.get_context_data_int(form, param_formset, **kwargs)
|
2022-01-02 20:00:29 +01:00
|
|
|
return self.render_to_response(context)
|
2022-01-01 17:53:10 +01:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
if 'submit-edit-component' in request.POST:
|
2022-01-03 13:18:31 +01:00
|
|
|
return self.handle_new_component_post(request, open=False, **kwargs)
|
|
|
|
elif 'submit-edit-component-open' in request.POST:
|
|
|
|
return self.handle_new_component_post(request, open=True, **kwargs)
|
2024-11-17 19:54:15 +01:00
|
|
|
elif 'submit-import-components' in request.POST:
|
|
|
|
return self.handle_import_components_post(request, open=True, **kwargs)
|
2022-01-02 20:00:29 +01:00
|
|
|
elif 'submit-advanced-search' in request.POST:
|
|
|
|
return self.handle_advanced_search_post(request, **kwargs)
|
2022-01-01 17:53:10 +01:00
|
|
|
else:
|
|
|
|
return super().post(request, *args, **kwargs)
|
2021-11-09 16:35:11 +01:00
|
|
|
|
2021-11-14 18:40:21 +01:00
|
|
|
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
|
|
|
|
|
2021-11-15 17:57:01 +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)
|
2021-11-14 18:40:21 +01:00
|
|
|
|
2021-11-15 17:57:01 +01:00
|
|
|
qs = qs.filter(s_filter)
|
2021-11-14 18:40:21 +01:00
|
|
|
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)
|
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
|
2021-11-14 20:38:19 +01:00
|
|
|
class DistributorView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
|
|
|
template_name = 'parts/distributors.html'
|
|
|
|
base_title = 'Distributors'
|
|
|
|
navbar_selected = 'Distributors'
|
|
|
|
default_page_size = 25
|
|
|
|
|
|
|
|
def search_distributors(self, search):
|
|
|
|
qs = Distributor.objects.all()
|
|
|
|
|
|
|
|
if not search:
|
|
|
|
return qs
|
|
|
|
|
2021-11-15 17:57:01 +01:00
|
|
|
search_fragments = search.strip().split()
|
2021-11-14 20:38:19 +01:00
|
|
|
|
2021-11-15 17:57:01 +01:00
|
|
|
for search in search_fragments:
|
|
|
|
qs = qs.filter(Q(name__icontains = search) | Q(website__icontains = search))
|
2021-11-14 20:38:19 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
queryset = self.search_distributors(search_string)
|
|
|
|
|
|
|
|
paginator = Paginator(queryset, self.default_page_size)
|
|
|
|
|
|
|
|
|
|
|
|
context['search_string'] = search_string
|
|
|
|
context['distributors'] = paginator.get_page(page_num)
|
|
|
|
context['new_distri_form'] = DistributorForm()
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
def handle_add_new_distributor(self, request):
|
|
|
|
|
|
|
|
form = DistributorForm(data=request.POST, files=request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
|
|
|
|
context = self.get_context_data()
|
|
|
|
|
|
|
|
if not form.is_valid():
|
|
|
|
context['new_distri_form'] = form
|
|
|
|
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
if 'submit-distri-add-new' in request.POST:
|
|
|
|
return self.handle_add_new_distributor(request)
|
|
|
|
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
class StockView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
|
|
|
template_name = 'parts/stocks.html'
|
|
|
|
base_title = 'Stocks'
|
|
|
|
navbar_selected = 'Stocks'
|
2021-08-14 00:25:19 +02:00
|
|
|
default_pagination_size = 25
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2021-08-14 00:25:19 +02:00
|
|
|
|
|
|
|
storage_page = self.request.GET.get('storage_page')
|
|
|
|
if storage_page is None:
|
|
|
|
storage_page = 1
|
|
|
|
low_stock_page = self.request.GET.get('low_stock_page')
|
|
|
|
if low_stock_page is None:
|
|
|
|
low_stock_page = 1
|
|
|
|
|
|
|
|
storage_paginator = Paginator(Storage.objects.filter(parent_storage=None), self.default_pagination_size)
|
|
|
|
low_stock_paginator = Paginator(Stock.get_under_watermark(),
|
|
|
|
self.default_pagination_size)
|
|
|
|
|
|
|
|
context['low_stocks'] = low_stock_paginator.get_page(low_stock_page)
|
|
|
|
context['storages'] = storage_paginator.get_page(storage_page)
|
2021-10-25 19:31:50 +02:00
|
|
|
add_stor_form = AddSubStorageForm()
|
2024-11-10 20:46:45 +01:00
|
|
|
add_stor_form.fields['responsible'].initial = self.request.user.id
|
2021-10-25 19:31:50 +02:00
|
|
|
context['add_storage_form'] = add_stor_form
|
2021-08-07 17:37:36 +02:00
|
|
|
return context
|
2021-10-25 19:31:50 +02:00
|
|
|
|
|
|
|
def handle_add_storage(self, request, **kwargs):
|
|
|
|
f = AddSubStorageForm(data=request.POST)
|
|
|
|
if f.is_valid():
|
2024-11-10 20:46:45 +01:00
|
|
|
sub_name = f.cleaned_data['storage_name']
|
|
|
|
try:
|
|
|
|
Storage.objects.create(name=sub_name,
|
|
|
|
responsible=f.cleaned_data['responsible'],
|
|
|
|
is_template=f.cleaned_data['is_template'],
|
|
|
|
template=f.cleaned_data.get('template'))
|
|
|
|
except ValidationError as v_err:
|
|
|
|
f.add_error('storage_name', '. '.join(v_err.messages))
|
2021-10-25 19:31:50 +02:00
|
|
|
context = self.get_context_data(**kwargs)
|
2024-11-10 20:46:45 +01:00
|
|
|
context['add_storage_form'] = f
|
2021-10-25 19:31:50 +02:00
|
|
|
return self.render_to_response(context)
|
2024-11-10 20:46:45 +01:00
|
|
|
|
2021-10-25 19:31:50 +02:00
|
|
|
def post(self, request, **kwargs):
|
|
|
|
if 'submit-add-storage' in request.POST:
|
|
|
|
return self.handle_add_storage(request, **kwargs)
|
2021-10-24 21:14:11 +02:00
|
|
|
|
2021-10-25 19:31:50 +02:00
|
|
|
return super().post(request, **kwargs)
|
2024-11-10 20:46:45 +01:00
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
|
|
|
template_name = 'parts/stocks-detail.html'
|
|
|
|
model = Storage
|
|
|
|
pk_url_kwarg = 'uuid'
|
|
|
|
base_title = ''
|
|
|
|
navbar_selected = 'Stocks'
|
2021-08-14 00:25:19 +02:00
|
|
|
default_pagination_size = 8
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
def get_breadcrumbs(self):
|
|
|
|
crumbs = self.object.get_path_components()
|
|
|
|
# Reverse list and drop the last element of the reversed list
|
|
|
|
crumbs = crumbs[::-1][:-1]
|
|
|
|
return crumbs
|
|
|
|
|
2021-10-31 20:57:28 +01:00
|
|
|
def search_stock_queryset(self, search):
|
2021-11-01 22:17:20 +01:00
|
|
|
stocks_in_storage = Stock.objects.filter(storage=self.object).order_by(Lower('component__name'))
|
2021-10-31 20:57:28 +01:00
|
|
|
|
|
|
|
if search is None or search == '':
|
|
|
|
return stocks_in_storage
|
|
|
|
|
2021-11-10 19:38:39 +01:00
|
|
|
if search.startswith('[comp_uuid]'):
|
|
|
|
search = search.replace('[comp_uuid]', '')
|
|
|
|
|
|
|
|
|
2021-10-31 20:57:28 +01:00
|
|
|
# Check if the searhc equals a UUID
|
|
|
|
test_uuid = None
|
|
|
|
try:
|
|
|
|
test_uuid = uuid.UUID(search)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if test_uuid is not None:
|
2021-11-01 22:17:20 +01:00
|
|
|
stocks_in_storage = stocks_in_storage.filter(Q(component__id = test_uuid) | Q(id= test_uuid))
|
2021-10-31 20:57:28 +01:00
|
|
|
else:
|
2021-11-15 11:20:31 +01:00
|
|
|
stocks_in_storage = stocks_in_storage.filter(Q(component__name__icontains = search) |
|
|
|
|
Q(component__package__name__icontains = search) |
|
|
|
|
Q(component__manufacturer__name__icontains = search))
|
2021-10-31 20:57:28 +01:00
|
|
|
|
|
|
|
return stocks_in_storage
|
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
self.base_title = 'Stocks / ' + self.object.name
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['breadcrumbs'] = self.get_breadcrumbs()
|
2021-08-14 00:25:19 +02:00
|
|
|
|
2021-10-31 20:57:28 +01:00
|
|
|
storage_page = self.request.GET.get('storage_page', default=1)
|
2021-08-14 00:25:19 +02:00
|
|
|
storage_paginator = Paginator(Storage.objects.filter(parent_storage=self.object), self.default_pagination_size)
|
2021-10-31 20:57:28 +01:00
|
|
|
stock_search_input = self.request.GET.get('search')
|
|
|
|
|
|
|
|
componente_stock_page = self.request.GET.get('stock_page', default=1)
|
|
|
|
|
|
|
|
stock_paginator = Paginator(self.search_stock_queryset(stock_search_input), self.default_pagination_size)
|
2021-08-14 00:25:19 +02:00
|
|
|
|
|
|
|
context['storages'] = storage_paginator.get_page(storage_page)
|
2021-11-01 22:17:20 +01:00
|
|
|
stocks = stock_paginator.get_page(componente_stock_page)
|
|
|
|
context['stocks'] = stocks
|
2021-10-31 20:57:28 +01:00
|
|
|
context['stock_search'] = stock_search_input
|
2021-10-24 21:14:11 +02:00
|
|
|
add_storage_form = AddSubStorageForm()
|
2024-11-10 20:46:45 +01:00
|
|
|
add_storage_form.fields['responsible'].initial = self.request.user.id
|
2021-10-24 21:14:11 +02:00
|
|
|
context['add_storage_form'] = add_storage_form
|
2024-11-10 21:13:56 +01:00
|
|
|
change_storage_form = ChangeStorageForm()
|
|
|
|
change_storage_form.fields['storage_name'].initial = self.object.name
|
2024-11-16 13:22:59 +01:00
|
|
|
change_storage_form.fields['verbose_name'].initial = self.object.verbose_name
|
2024-11-10 21:13:56 +01:00
|
|
|
change_storage_form.fields['responsible'].initial = self.object.responsible.id
|
|
|
|
change_storage_form.fields['is_template'].initial = self.object.is_template
|
|
|
|
context['change_storage_form'] = change_storage_form
|
2021-10-25 19:31:50 +02:00
|
|
|
context['delete_storage_error'] = None
|
2021-11-08 23:11:05 +01:00
|
|
|
context['add_stock_form'] = AddStockForm()
|
2021-11-01 22:17:20 +01:00
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
return context
|
2021-08-14 02:35:09 +02:00
|
|
|
|
2021-10-24 21:14:11 +02:00
|
|
|
def handle_add_storage_post(self, request, **kwargs):
|
|
|
|
f = AddSubStorageForm(data=request.POST)
|
|
|
|
if f.is_valid():
|
|
|
|
sub_name = f.cleaned_data['storage_name']
|
|
|
|
try:
|
2024-11-10 20:46:45 +01:00
|
|
|
Storage.objects.create(name=sub_name,
|
2024-11-16 13:22:59 +01:00
|
|
|
verbose_name=f.cleaned_data.get('verbose_name'),
|
2024-11-10 20:46:45 +01:00
|
|
|
parent_storage=self.object,
|
|
|
|
responsible=f.cleaned_data['responsible'],
|
|
|
|
is_template=f.cleaned_data['is_template'],
|
|
|
|
template=f.cleaned_data.get('template'))
|
|
|
|
except ValidationError as v_err:
|
|
|
|
f.add_error('storage_name', '. '.join(v_err.messages))
|
2021-10-24 21:14:11 +02:00
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
context['add_storage_form'] = f
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
2024-11-10 21:13:56 +01:00
|
|
|
def handle_change_storage_post(self, request, **kwargs):
|
|
|
|
f = ChangeStorageForm(data=request.POST)
|
|
|
|
if f.is_valid():
|
|
|
|
sub_name = f.cleaned_data['storage_name']
|
|
|
|
try:
|
|
|
|
self.object.name = f.cleaned_data['storage_name']
|
2024-11-16 13:22:59 +01:00
|
|
|
self.object.verbose_name = f.cleaned_data.get('verbose_name')
|
2024-11-10 21:13:56 +01:00
|
|
|
self.object.responsible = f.cleaned_data['responsible']
|
|
|
|
self.object.is_template = f.cleaned_data['is_template']
|
|
|
|
self.object.save()
|
|
|
|
except ValidationError as v_err:
|
|
|
|
f.add_error('storage_name', '. '.join(v_err.messages))
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
context['change_storage_form'] = f
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
2021-10-25 19:31:50 +02:00
|
|
|
def handle_del_storage_post(self, request, **kwargs):
|
|
|
|
parent = self.object.parent_storage
|
|
|
|
try:
|
|
|
|
self.object.delete()
|
|
|
|
except:
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
context['delete_storage_errors'] = ['Error deleting Storage '+str(self.object)]
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
if parent is None:
|
|
|
|
return redirect('parts-stocks')
|
|
|
|
else:
|
|
|
|
return redirect(reverse('parts-stocks-detail', kwargs={'uuid':parent.id}))
|
2024-11-10 20:46:45 +01:00
|
|
|
|
2021-11-01 22:17:20 +01:00
|
|
|
def handle_del_stock_post(self, request, **kwargs):
|
|
|
|
del_error = None
|
|
|
|
if 'stock_uuid' in request.POST:
|
|
|
|
f = DeleteStockForm(data=request.POST)
|
|
|
|
if f.is_valid():
|
|
|
|
try:
|
|
|
|
s = Stock.objects.get(id=f.cleaned_data['stock_uuid'])
|
|
|
|
print(s.storage)
|
|
|
|
print(self.object)
|
|
|
|
if s.storage == self.object:
|
|
|
|
s.delete()
|
|
|
|
else:
|
|
|
|
del_error = 'Cannot delete stock from another storage.'
|
|
|
|
except:
|
|
|
|
del_error = 'Could not find requested stock in this storage.'
|
|
|
|
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
2021-11-07 20:36:12 +01:00
|
|
|
def handle_update_watermark(self, request, **kwargs):
|
|
|
|
edit_form = EditWatermarkForm(data=request.POST)
|
|
|
|
update_watermark_error = None
|
|
|
|
if edit_form.is_valid():
|
|
|
|
edit_form.save()
|
|
|
|
else:
|
|
|
|
pass # Todo: Handle error
|
|
|
|
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def handle_amount_change_post(self, request, increase, **kwargs):
|
|
|
|
edit_form = EditStockAmountForm(data=request.POST)
|
|
|
|
if edit_form.is_valid():
|
|
|
|
edit_form.save(increase)
|
|
|
|
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
return self.render_to_response(context)
|
2021-10-25 19:31:50 +02:00
|
|
|
|
2021-11-08 23:11:05 +01:00
|
|
|
def handle_add_stock_post(self, request, **kwargs):
|
|
|
|
f = AddStockForm(data=request.POST)
|
|
|
|
error_occured = False
|
|
|
|
|
|
|
|
if f.is_valid():
|
|
|
|
try:
|
|
|
|
f.save(self.object)
|
|
|
|
except Exception as ex:
|
|
|
|
f.add_error('', str(ex))
|
|
|
|
error_occured = True
|
|
|
|
else:
|
|
|
|
error_occured = True
|
|
|
|
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
if error_occured:
|
|
|
|
context['add_stock_form'] = f
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
2021-10-24 21:14:11 +02:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
|
|
|
|
if 'submit-add-storage' in request.POST:
|
2021-10-25 19:31:50 +02:00
|
|
|
return self.handle_add_storage_post(request, **kwargs)
|
2024-11-15 22:55:30 +01:00
|
|
|
elif 'submit-change-storage' in request.POST:
|
2024-11-10 21:13:56 +01:00
|
|
|
return self.handle_change_storage_post(request, **kwargs)
|
2021-10-25 19:31:50 +02:00
|
|
|
elif 'submit-delete-storage' in request.POST:
|
|
|
|
return self.handle_del_storage_post(request, **kwargs)
|
2021-11-01 22:17:20 +01:00
|
|
|
elif 'submit-delete-stock' in request.POST:
|
|
|
|
return self.handle_del_stock_post(request, **kwargs)
|
2021-11-07 20:36:12 +01:00
|
|
|
elif 'submit-edit-watermark' in request.POST:
|
|
|
|
return self.handle_update_watermark(request, **kwargs)
|
|
|
|
elif 'submit-amount-reduce' in request.POST:
|
|
|
|
return self.handle_amount_change_post(request, False, **kwargs)
|
|
|
|
elif 'submit-amount-increase' in request.POST:
|
|
|
|
return self.handle_amount_change_post(request, True, **kwargs)
|
2021-11-08 23:11:05 +01:00
|
|
|
elif 'submit-add-stock' in request.POST:
|
|
|
|
return self.handle_add_stock_post(request, **kwargs)
|
|
|
|
|
2021-11-09 16:35:11 +01:00
|
|
|
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'
|
2022-01-02 16:23:49 +01:00
|
|
|
|
2021-11-09 16:35:11 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
self.base_title = 'Component / '+self.object.name
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['component'] = self.object
|
2021-11-12 20:31:34 +01:00
|
|
|
context['stocks'] = Stock.objects.filter(component=self.object)
|
2021-12-31 22:29:47 +01:00
|
|
|
context['comp_form'] = ComponentForm(instance=self.object)
|
2022-01-02 16:23:49 +01:00
|
|
|
context['new_distri_num_form'] = DistributorNumberCreateForm()
|
2022-01-03 17:38:50 +01:00
|
|
|
context['new_param_form'] = ComponentParameterCreateForm()
|
2022-01-03 22:53:57 +01:00
|
|
|
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')
|
2021-11-13 21:05:52 +01:00
|
|
|
|
2021-11-10 19:38:39 +01:00
|
|
|
return context
|
2021-11-09 16:35:11 +01:00
|
|
|
|
2021-12-31 22:29:47 +01:00
|
|
|
def handle_submit_edit_component_post(self, request, **kwargs):
|
|
|
|
cform = ComponentForm(instance=self.object, data=request.POST, files=request.FILES)
|
|
|
|
if cform.is_valid():
|
|
|
|
cform.save()
|
2022-01-01 17:53:10 +01:00
|
|
|
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
if not cform.is_valid():
|
|
|
|
context['comp_form'] = cform
|
2021-12-31 22:29:47 +01:00
|
|
|
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
2022-01-01 17:53:10 +01:00
|
|
|
def handle_submit_delete_post(self, request, **kwargs):
|
|
|
|
delete_error = None
|
|
|
|
protected_stuff = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.object.delete()
|
|
|
|
except ProtectedError as pe:
|
|
|
|
delete_error = 'Component is protected'
|
|
|
|
protected_stuff = pe.protected_objects
|
|
|
|
except:
|
|
|
|
delete_error = 'Cannot delete component. Unknown error'
|
|
|
|
|
|
|
|
if delete_error is None:
|
|
|
|
return redirect('parts-components')
|
|
|
|
else:
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
context['delete_error'] = delete_error
|
|
|
|
context['protected_stuff'] = protected_stuff
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
2022-01-02 16:23:49 +01:00
|
|
|
def handle_submit_new_distri_num_post(self, request, **kwargs):
|
|
|
|
form = DistributorNumberCreateForm(data=request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
new_number = form.save(commit=False)
|
|
|
|
new_number.component = self.object
|
|
|
|
try:
|
|
|
|
new_number.save()
|
|
|
|
except IntegrityError as ie:
|
|
|
|
form.add_error('__all__', 'Number for given distributor already exists')
|
|
|
|
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
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():
|
|
|
|
form.cleaned_data['distributor_num'].delete()
|
|
|
|
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
return self.render_to_response(context)
|
2022-01-03 17:38:50 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
except IntegrityError:
|
|
|
|
form.add_error('__all__', 'This parameter is already set')
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
if not form.is_valid():
|
|
|
|
context['new_param_form'] = form
|
|
|
|
return self.render_to_response(context)
|
2022-01-02 16:23:49 +01:00
|
|
|
|
2021-11-10 19:38:39 +01:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
2021-12-31 22:29:47 +01:00
|
|
|
if 'submit-edit-component' in request.POST:
|
|
|
|
return self.handle_submit_edit_component_post(request, **kwargs)
|
2022-01-01 17:53:10 +01:00
|
|
|
elif 'submit-component-delete' in request.POST:
|
|
|
|
return self.handle_submit_delete_post(request, **kwargs)
|
2022-01-02 16:23:49 +01:00
|
|
|
elif 'submit-create-new-distri-num' in request.POST:
|
|
|
|
return self.handle_submit_new_distri_num_post(request, **kwargs)
|
|
|
|
elif 'submit-delete-distributor-num' in request.POST:
|
|
|
|
return self.handle_submit_delete_distri_num_post(request, **kwargs)
|
2022-01-03 17:38:50 +01:00
|
|
|
elif 'submit-delete-param' in request.POST:
|
|
|
|
return self.handle_submit_delete_param_post(request, **kwargs)
|
|
|
|
elif 'submit-create-new-param' in request.POST:
|
|
|
|
return self.handle_submit_new_param_post(request, **kwargs)
|
2022-01-01 17:53:10 +01:00
|
|
|
else:
|
|
|
|
return super().post(request, *args, **kwargs)
|
2021-11-09 18:44:28 +01:00
|
|
|
|
|
|
|
class PackageDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
|
|
|
template_name = 'parts/packages-detail.html'
|
|
|
|
model = Package
|
|
|
|
pk_url_kwarg = 'uuid'
|
|
|
|
base_title = ''
|
2021-11-14 18:40:21 +01:00
|
|
|
navbar_selected = 'Packages'
|
2021-11-09 16:35:11 +01:00
|
|
|
|
2021-11-09 18:44:28 +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
|
2021-11-14 16:48:34 +01:00
|
|
|
context['edit_form'] = PackageForm(instance=self.object)
|
2021-11-09 18:44:28 +01:00
|
|
|
|
|
|
|
return context
|
2021-11-14 16:48:34 +01:00
|
|
|
|
|
|
|
def handle_delete_package(self, request):
|
|
|
|
delete_error = None
|
|
|
|
protected_objects = None
|
|
|
|
|
|
|
|
# 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'
|
|
|
|
|
|
|
|
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:
|
2021-11-14 18:40:21 +01:00
|
|
|
return redirect('parts-packages')
|
2021-11-14 16:48:34 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-11-14 20:38:19 +01:00
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
class DistributorDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
|
|
|
template_name = 'parts/distributors-detail.html'
|
|
|
|
model = Distributor
|
|
|
|
pk_url_kwarg = 'uuid'
|
|
|
|
base_title = ''
|
|
|
|
navbar_selected = 'Distributors'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
self.base_title = 'Distributor / '+self.object.name
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['distributor'] = self.object
|
|
|
|
context['edit_form'] = DistributorForm(instance=self.object)
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
def handle_delete_distributor(self, request):
|
|
|
|
delete_error = None
|
|
|
|
protected_objects = None
|
|
|
|
|
|
|
|
# Try to delete this instance
|
|
|
|
try:
|
|
|
|
self.object.delete()
|
|
|
|
except ProtectedError as pe:
|
|
|
|
delete_error = 'Cannot delete this distributor. It is referenced by a component.'
|
|
|
|
protected_objects = pe.protected_objects
|
|
|
|
except:
|
|
|
|
delete_error = 'Cannot delete this distributor. Unknown error'
|
|
|
|
|
|
|
|
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-distributors')
|
|
|
|
|
|
|
|
def edit_distributor(self, request):
|
|
|
|
edit_form = DistributorForm(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-distri-delete' in request.POST:
|
|
|
|
return self.handle_delete_distributor(request)
|
|
|
|
elif 'submit-distri-edit' in request.POST:
|
|
|
|
return self.edit_distributor(request)
|
|
|
|
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
class ManufacturersViewSet(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
|
|
|
template_name = 'parts/manufacturers.html'
|
|
|
|
base_title = 'Manufacturers'
|
|
|
|
navbar_selected = 'Manufacturers'
|
|
|
|
default_page_size = 25
|
|
|
|
|
|
|
|
def search_manufacturers(self, search):
|
|
|
|
qs = Manufacturer.objects.all()
|
|
|
|
|
|
|
|
if not search:
|
|
|
|
return qs
|
|
|
|
|
2021-11-15 17:57:01 +01:00
|
|
|
search_fragements = search.strip().split()
|
|
|
|
|
|
|
|
for search in search_fragements:
|
|
|
|
qs = qs.filter(Q(name__icontains = search) | Q(website__icontains = search))
|
2021-11-14 20:38:19 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
queryset = self.search_manufacturers(search_string)
|
|
|
|
|
|
|
|
paginator = Paginator(queryset, self.default_page_size)
|
|
|
|
|
|
|
|
|
|
|
|
context['search_string'] = search_string
|
|
|
|
context['manufacturers'] = paginator.get_page(page_num)
|
|
|
|
context['new_manufacturer_form'] = ManufacturerForm()
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
def handle_add_new_manufacturer(self, request):
|
|
|
|
|
|
|
|
form = ManufacturerForm(data=request.POST, files=request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
|
|
|
|
context = self.get_context_data()
|
|
|
|
|
|
|
|
if not form.is_valid():
|
|
|
|
context['new_manufacturer_form'] = form
|
|
|
|
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
if 'submit-manufacturer-add-new' in request.POST:
|
|
|
|
return self.handle_add_new_manufacturer(request)
|
|
|
|
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
class ManufacturerDetailViewSet(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
|
|
|
template_name = 'parts/manufacturers-detail.html'
|
|
|
|
model = Manufacturer
|
|
|
|
pk_url_kwarg = 'uuid'
|
|
|
|
base_title = ''
|
|
|
|
navbar_selected = 'Manufacturers'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
self.base_title = 'Manufacturer / '+self.object.name
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['manufacturer'] = self.object
|
|
|
|
context['edit_form'] = ManufacturerForm(instance=self.object)
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
def handle_delete_manufacturer(self, request):
|
|
|
|
delete_error = None
|
|
|
|
protected_objects = None
|
|
|
|
|
|
|
|
# Try to delete this instance
|
|
|
|
try:
|
|
|
|
self.object.delete()
|
|
|
|
except ProtectedError as pe:
|
|
|
|
delete_error = 'Cannot delete this distributor. It is referenced by a component.'
|
|
|
|
protected_objects = pe.protected_objects
|
|
|
|
except:
|
|
|
|
delete_error = 'Cannot delete this distributor. Unknown error'
|
|
|
|
|
|
|
|
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-manufacturers')
|
|
|
|
|
|
|
|
def edit_manufacturer(self, request):
|
|
|
|
edit_form = ManufacturerForm(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-manufacturer-delete' in request.POST:
|
|
|
|
return self.handle_delete_manufacturer(request)
|
|
|
|
elif 'submit-manufacturer-edit' in request.POST:
|
|
|
|
return self.edit_manufacturer(request)
|
|
|
|
|
2024-11-17 19:10:28 +01:00
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def health_check_view(request) -> HttpResponse:
|
|
|
|
"""
|
|
|
|
Health checking view. Returns empty http response with HTTP status OK.
|
2024-11-17 23:03:43 +01:00
|
|
|
This will be used to check if the system is actually running correctly
|
2024-11-17 19:10:28 +01:00
|
|
|
"""
|
|
|
|
return HttpResponse(status=200)
|