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 @@
Distributor | +Part Number | ++ + + {% for num in distri_nums %} + |
---|---|---|
{{num.distributor.name}} | +{{num.distributor_part_number}} | ++ + | +