From 494aded95b7b8d5e30aec16b909af420804d3458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 2 Jan 2022 16:23:49 +0100 Subject: [PATCH] Implement distributor numbers --- shimatta_kenkyusho/parts/forms.py | 20 ++++++++- shimatta_kenkyusho/parts/views.py | 43 +++++++++++++------ .../templates/parts/components-detail.html | 29 ++++++++++++- .../modals/new-distributor-number-modal.html | 26 +++++++++++ 4 files changed, 102 insertions(+), 16 deletions(-) create mode 100644 shimatta_kenkyusho/templates/parts/modals/new-distributor-number-modal.html diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index c30f645..6e72706 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -197,4 +197,22 @@ class DistributorForm(forms.ModelForm): class ManufacturerForm(forms.ModelForm): class Meta: model = parts_models.Manufacturer - fields = '__all__' \ No newline at end of file + fields = '__all__' + +class DistributorNumberCreateForm(forms.ModelForm): + distributor = AutocompleteForeingKeyField(api_search_url='distributor-list', foreign_model=parts_models.Distributor, required=True) + + class Meta: + model = parts_models.DistributorNum + fields = ['distributor', 'distributor_part_number'] + +class DistributorNumberDeleteForm(forms.Form): + distributor_num = forms.UUIDField(required=True) + + def clean_distributor_num(self): + my_uuid = self.cleaned_data['distributor_num'] + try: + distributor_number = parts_models.DistributorNum.objects.get(id=my_uuid) + except: + raise ValidationError('distributor number invalid') + return distributor_number \ No newline at end of file diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 9935779..7774c74 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -525,25 +525,15 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): pk_url_kwarg = 'uuid' base_title = '' navbar_selected = 'Components' - - def prepare_initial_param_formset_data(self): - parameters = ComponentParameter.objects.filter(component=self.object) - initdata = [] - for param in parameters: - param_type = param.parameter_type.parameter_type - if param_type == 'F': - value = param.text_value - else: - value = param.value - initdata.append({'parameter_type': param.parameter_type, 'value': value}) - return initdata - + def get_context_data(self, **kwargs): self.base_title = 'Component / '+self.object.name context = super().get_context_data(**kwargs) context['component'] = self.object context['stocks'] = Stock.objects.filter(component=self.object) context['comp_form'] = ComponentForm(instance=self.object) + context['new_distri_num_form'] = DistributorNumberCreateForm() + context['distri_nums'] = DistributorNum.objects.filter(component=self.object) return context @@ -578,12 +568,39 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): context['protected_stuff'] = protected_stuff return self.render_to_response(context) + 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) + def post(self, request, *args, **kwargs): self.object = self.get_object() if 'submit-edit-component' in request.POST: return self.handle_submit_edit_component_post(request, **kwargs) elif 'submit-component-delete' in request.POST: return self.handle_submit_delete_post(request, **kwargs) + 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) else: return super().post(request, *args, **kwargs) diff --git a/shimatta_kenkyusho/templates/parts/components-detail.html b/shimatta_kenkyusho/templates/parts/components-detail.html index 6d5d4ef..5d758b2 100644 --- a/shimatta_kenkyusho/templates/parts/components-detail.html +++ b/shimatta_kenkyusho/templates/parts/components-detail.html @@ -98,8 +98,29 @@

Parameters

-

Distributor Part Numbers

- +

Distributor Part Numbers

+ + + + + + + + {% for num in distri_nums %} + + + + + + {% endfor %} + +
DistributorPart Number
{{num.distributor.name}}{{num.distributor_part_number}} +
+ {% csrf_token %} + + +
+
@@ -181,6 +202,7 @@
+{% include 'parts/modals/new-distributor-number-modal.html' with component_name=component.name form=new_distri_num_form %} {% include 'parts/modals/edit-component-modal.html' with heading="Edit "|add:component.name form=comp_form %} {% endblock content %} @@ -193,6 +215,9 @@ bootstrap.Modal.getOrCreateInstance(document.getElementById('comp-edit-modal')). {% if delete_error %} bootstrap.Modal.getOrCreateInstance(document.getElementById('component-delete-modal')).show(); {% endif %} +{% if new_distri_num_form.errors %} +bootstrap.Modal.getOrCreateInstance(document.getElementById('new-distributor-number-modal')).show(); +{% endif %} {% endblock custom_scripts %} diff --git a/shimatta_kenkyusho/templates/parts/modals/new-distributor-number-modal.html b/shimatta_kenkyusho/templates/parts/modals/new-distributor-number-modal.html new file mode 100644 index 0000000..9848750 --- /dev/null +++ b/shimatta_kenkyusho/templates/parts/modals/new-distributor-number-modal.html @@ -0,0 +1,26 @@ +{% comment "" %} +Needs: +- form : DistributorNuberCreateForm +- component_name: Component's name + +{% endcomment %} +{% load crispy_forms_tags %} + \ No newline at end of file -- 2.47.0