Add manufacturers and distributors to Website

This commit is contained in:
Mario Hüttel 2021-11-14 20:38:19 +01:00
parent 3bbc05db1e
commit 01124d9904
9 changed files with 573 additions and 3 deletions

View File

@ -296,4 +296,13 @@ class DistributorNumberFormSet(forms.BaseModelFormSet):
class PackageForm(forms.ModelForm):
class Meta:
model = parts_models.Package
fields = '__all__'
class DistributorForm(forms.ModelForm):
class Meta:
model = parts_models.Distributor
fields = '__all__'
class ManufacturerForm(forms.ModelForm):
class Meta:
model = parts_models.Manufacturer
fields = '__all__'

View File

@ -36,6 +36,8 @@ class NavBar():
'Components':BsNavBarItem('Components', reverse('parts-components'), False),
'Packages': BsNavBarItem('Packages', reverse('parts-packages'), False),
'Stocks':BsNavBarItem('Stocks', reverse('parts-stocks'), False),
'Distributors':BsNavBarItem('Distributors', reverse('parts-distributors'), False),
'Manufacturers':BsNavBarItem('Manufacturers', reverse('parts-manufacturers'), False),
}
try:
@ -48,6 +50,8 @@ class NavBar():
items['Components'],
items['Packages'],
items['Stocks'],
items['Distributors'],
items['Manufacturers'],
#items['Login'],
]
nb = NavBar()

View File

@ -5,6 +5,7 @@ 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('distributors/', parts_views.DistributorView.as_view(), name='parts-distributors'),
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'),
@ -12,4 +13,7 @@ urlpatterns = [
path('stocks/<slug:uuid>/', parts_views.StockViewDetail.as_view(), name='parts-stocks-detail'),
path('components/<slug:uuid>/', parts_views.ComponentDetailView.as_view(), name='parts-components-detail'),
path('packages/<slug:uuid>/', parts_views.PackageDetailView.as_view(), name='parts-packages-detail'),
path('distributors/<slug:uuid>/', parts_views.DistributorDetailView.as_view(), name='parts-distributors-detail'),
path('manufacturers/', parts_views.ManufacturersViewSet.as_view(), name='parts-manufacturers'),
path("manufacturers/<slug:uuid>/", parts_views.ManufacturerDetailViewSet.as_view(), name='parts-manufacturers-detail'),
]

View File

@ -202,6 +202,58 @@ class PackageView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
return super().post(request, *args, **kwargs)
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
search = search.strip()
qs = qs.filter(Q(name__contains = search) | Q(website__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)
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)
class StockView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
template_name = 'parts/stocks.html'
base_title = 'Stocks'
@ -440,8 +492,6 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
base_title = ''
navbar_selected = 'Components'
def prepare_initial_param_formset_data(self):
parameters = ComponentParameter.objects.filter(component=self.object)
initdata = []
@ -620,4 +670,170 @@ class PackageDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
elif 'submit-pkg-edit' in request.POST:
return self.edit_package(request)
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
search = search.strip()
qs = qs.filter(Q(name__contains = search) | Q(website__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)
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)
return super().post(request, *args, **kwargs)

View File

@ -50,7 +50,7 @@
</td>
<td class="align-middle" >
{% if component.manufacturer %}
{{component.manufacturer.name}}
<a href="{% url 'parts-manufacturers-detail' uuid=component.manufacturer.id %}" class="link-primary text-decoration-none">{{component.manufacturer.name}}</a>
{% if component.manufacturer.image %}
<img src="{{component.manufacturer.image.url}}" class="component-img-small">
{% endif %}

View File

@ -0,0 +1,94 @@
{% extends 'base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="row justify-content-center">
{% if distributor.image %}
<img src="{{distributor.image.url}}" alt="{{distributor.name}}" class="component-img-big btn" data-bs-toggle="modal" data-bs-target="#distri-img-modal">
{% else %}
<img src="{% static 'css/icons/card-image.svg' %}" alt="{{distributor.name}}" class="component-img-def-big">
{% endif %}
</div>
<div class="row">
<button class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#distri-delete-modal"><i class="bi bi-file-x"></i> Delete {{distributor.name}}</button>
</div>
</div>
<div class="col m-1">
<h2>Distributor {{distributor.name}}</h2>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{edit_form|crispy}}
<input type="submit" class="btn btn-primary" value="Save" name="submit-distri-edit">
</form>
</div>
</div>
</div>
{% if distributor.image %}
<div class="modal fade" id="distri-img-modal" tabindex="-1">
<div class="modal-dialog modal-lg modal-fullscreen-lg-down">
<div class="modal-content">
<div class="modal-header">
<h5>{{distributor.name}}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="text-center">
<img class="component-img-huge" src="{{distributor.image.url}}">
</div>
</div>
</div>
</div>
</div>
{% endif %}
<!-- Delete modal -->
<div class="modal fade" id="distri-delete-modal" tabindex="-1">
<div class="modal-dialog modal-lg modal-fullscreen-lg-down">
<div class="modal-content">
<div class="modal-header">
<h5>{{distributor.name}}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<h4>Delete this Distributor?</h4>
{% if delete_error %}
<div class="alert alert-danger">
{{delete_error}}
</div>
{% if protected_components %}
<h4>Following components prevent the deletion:</h4>
<ul>
{% for comp in protected_components %}
<li><a class="text-decoration-none text-primary" href="{% url 'parts-components-detail' uuid=comp.id %}">{{comp}}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
</div>
<div class="modal-footer">
<form action="" method="post">
{% csrf_token %}
<button type="submit" class="btn btn-danger" name="submit-distri-delete"><i class="bi bi-file-x"></i> Delete {{distributor.name}}</button>
</form>
</div>
</div>
</div>
</div>
{% endblock content %}
{% block custom_scripts %}
<script type="text/javascript">
{% if delete_error %}
bootstrap.Modal.getOrCreateInstance(document.getElementById('distri-delete-modal')).show();
{% endif %}
</script>
{% endblock custom_scripts %}

View File

@ -0,0 +1,75 @@
{% extends 'base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md">
<h2>Distributors</h2>
<form action="" method="get">
<div class="input-group mb-3">
<input class="form-control" name="search" type="search" placeholder="Search Distributor..." {% if search_string %}value="{{search_string}}"{% endif %}>
<button type="submit" class="btn btn-primary">
<i class="bi bi-search"></i>
</button>
<button class="btn btn-success" type="button" data-bs-toggle="modal" data-bs-target="#add-distri-modal"><i class="bi bi-plus-circle"></i> Add Distributor</button>
</div>
</form>
<div class="mb-3">
{% include 'paginator.html' with paginator=distributors get_param='page' aria_label='Distributor Page Navigation' %}
</div>
<div class="list-group mb-3">
{% for distri in distributors %}
<a href="{% url 'parts-distributors-detail' uuid=distri.id %}" class="text-decoration-none">
<li class="list-group-item list-group-item-action d-flex align-items-center">
<div class="flex-shrink-0">
{% if distri.image %}
<img src="{{distri.image.url}}" class="component-img-small" alt="{{ distri.name }}" class="mr-3">
{% else %}
<img src="{% static 'css/icons/card-image.svg' %}" class="component-img-def-small" alt="{{ distri.name }}" class="mr-3">
{% endif %}
</div>
<div class="flex-grow-1 ms-3">
<h6 class="mt-0 text-primary">{{ distri.name }}</h6>
{{distri.website|default_if_none:"No website specified"}}
</div>
</li>
</a>
{% endfor %}
</div>
{% include 'paginator.html' with paginator=distributors get_param='page' aria_label='Distributor Page Navigation' %}
</div>
</div>
</div>
<div class="modal fade" id="add-distri-modal" tabindex="-1" aria-labelledby="Add Distributor Modal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add New Distributor</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="modal-body">
{{new_distri_form|crispy}}
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" name="submit-distri-add-new" value="Add Distributor">
</div>
</form>
</div>
</div>
</div>
{% endblock content %}
{% block custom_scripts %}
<script type="text/javascript">
{% if new_distri_form.errors %}
bootstrap.Modal.getOrCreateInstance(document.getElementById('add-distri-modal')).show()
{% endif %}
</script>
{% endblock custom_scripts %}

View File

@ -0,0 +1,94 @@
{% extends 'base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="row justify-content-center">
{% if manufacturer.image %}
<img src="{{manufacturer.image.url}}" alt="{{manufacturer.name}}" class="component-img-big btn" data-bs-toggle="modal" data-bs-target="#manufacturer-img-modal">
{% else %}
<img src="{% static 'css/icons/card-image.svg' %}" alt="{{manufacturer.name}}" class="component-img-def-big">
{% endif %}
</div>
<div class="row">
<button class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#manufacturer-delete-modal"><i class="bi bi-file-x"></i> Delete {{manufacturer.name}}</button>
</div>
</div>
<div class="col m-1">
<h2>Manufacturer {{manufacturer.name}}</h2>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{edit_form|crispy}}
<input type="submit" class="btn btn-primary" value="Save" name="submit-manufacturer-edit">
</form>
</div>
</div>
</div>
{% if manufacturer.image %}
<div class="modal fade" id="manufacturer-img-modal" tabindex="-1">
<div class="modal-dialog modal-lg modal-fullscreen-lg-down">
<div class="modal-content">
<div class="modal-header">
<h5>{{manufacturer.name}}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="text-center">
<img class="component-img-huge" src="{{manufacturer.image.url}}">
</div>
</div>
</div>
</div>
</div>
{% endif %}
<!-- Delete modal -->
<div class="modal fade" id="manufacturer-delete-modal" tabindex="-1">
<div class="modal-dialog modal-lg modal-fullscreen-lg-down">
<div class="modal-content">
<div class="modal-header">
<h5>{{manufacturer.name}}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<h4>Delete this Manufacturer?</h4>
{% if delete_error %}
<div class="alert alert-danger">
{{delete_error}}
</div>
{% if protected_components %}
<h4>Following components prevent the deletion:</h4>
<ul>
{% for comp in protected_components %}
<li><a class="text-decoration-none text-primary" href="{% url 'parts-components-detail' uuid=comp.id %}">{{comp}}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
</div>
<div class="modal-footer">
<form action="" method="post">
{% csrf_token %}
<button type="submit" class="btn btn-danger" name="submit-manufacturer-delete"><i class="bi bi-file-x"></i> Delete {{manufacturer.name}}</button>
</form>
</div>
</div>
</div>
</div>
{% endblock content %}
{% block custom_scripts %}
<script type="text/javascript">
{% if delete_error %}
bootstrap.Modal.getOrCreateInstance(document.getElementById('manufacturer-delete-modal')).show();
{% endif %}
</script>
{% endblock custom_scripts %}

View File

@ -0,0 +1,74 @@
{% extends 'base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md">
<h2>Manufacturers</h2>
<form action="" method="get">
<div class="input-group mb-3">
<input class="form-control" name="search" type="search" placeholder="Search Manufacturer..." {% if search_string %}value="{{search_string}}"{% endif %}>
<button type="submit" class="btn btn-primary">
<i class="bi bi-search"></i>
</button>
<button class="btn btn-success" type="button" data-bs-toggle="modal" data-bs-target="#add-manufacturer-modal"><i class="bi bi-plus-circle"></i> Add Manufacturer</button>
</div>
</form>
<div class="mb-3">
{% include 'paginator.html' with paginator=manufacturers get_param='page' aria_label='Manufacturer Page Navigation' %}
</div>
<div class="list-group mb-3">
{% for manufacturer in manufacturers %}
<a href="{% url 'parts-manufacturers-detail' uuid=manufacturer.id %}" class="text-decoration-none">
<li class="list-group-item list-group-item-action d-flex align-items-center">
<div class="flex-shrink-0">
{% if manufacturer.image %}
<img src="{{manufacturer.image.url}}" class="component-img-small" alt="{{ manufacturer.name }}" class="mr-3">
{% else %}
<img src="{% static 'css/icons/card-image.svg' %}" class="component-img-def-small" alt="{{ manufacturer.name }}" class="mr-3">
{% endif %}
</div>
<div class="flex-grow-1 ms-3">
<h6 class="mt-0 text-primary">{{ manufacturer.name }}</h6>
{{manufacturer.website|default_if_none:"No website specified"}}
</div>
</li>
</a>
{% endfor %}
</div>
{% include 'paginator.html' with paginator=manufacturers get_param='page' aria_label='Manufacturer Page Navigation' %}
</div>
</div>
</div>
<div class="modal fade" id="add-manufacturer-modal" tabindex="-1" aria-labelledby="Add Manufacturer Modal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add New Manufacturer</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="modal-body">
{{new_manufacturer_form|crispy}}
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" name="submit-manufacturer-add-new" value="Add Manufacturer">
</div>
</form>
</div>
</div>
</div>
{% endblock content %}
{% block custom_scripts %}
<script type="text/javascript">
{% if new_manufacturer_form.errors %}
bootstrap.Modal.getOrCreateInstance(document.getElementById('add-manufacturer-modal')).show()
{% endif %}
</script>
{% endblock custom_scripts %}