From dbbd8b4a9989018203e0f6b1118835eb4c8e061b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sat, 1 Jan 2022 17:53:10 +0100 Subject: [PATCH] Implement component deletion --- shimatta_kenkyusho/parts/forms.py | 114 +----------------- shimatta_kenkyusho/parts/views.py | 79 +++++++----- .../templates/parts/components-detail.html | 42 ++++++- .../templates/parts/components.html | 23 +++- 4 files changed, 109 insertions(+), 149 deletions(-) diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index c8e9db0..c30f645 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -180,123 +180,11 @@ class ComponentForm(forms.ModelForm): manufacturer = AutocompleteForeingKeyField(api_search_url='manufacturer-list', foreign_model=parts_models.Manufacturer, required=False) component_type = AutocompleteForeingKeyField(api_search_url='componenttype-list', foreign_model=parts_models.ComponentType, name_field_name='class_name', required=False, image_field_name=None) package = AutocompleteForeingKeyField(api_search_url='package-list', foreign_model=parts_models.Package, required=False) + pref_distri = AutocompleteForeingKeyField(api_search_url='distributor-list', foreign_model=parts_models.Distributor, required=False) class Meta: model = parts_models.Component fields = '__all__' -class EditComponentForm(forms.Form): - name = forms.CharField(required=True) - datasheet_link = forms.CharField(max_length=300, required=False) - description = forms.CharField(required=False, widget=forms.Textarea) - - # Look up these fields later. Will be autocompleted in UI - manufacturer = forms.CharField(required=False, initial='') - component_type = forms.CharField(required=False, label='Component Type', initial='') - pref_distri = forms.CharField(required=False, label='Preferred Distributor', initial='') - package = forms.CharField(required=False, initial='') - - image = forms.ImageField(required=False) - - def __init__(self, *args, instance=None, **kwargs): - super().__init__(*args, **kwargs) - - self.instance = instance - if instance: - self.fields['name'].initial = instance.name - self.fields['datasheet_link'].initial = instance.datasheet_link - if instance.component_type: - self.fields['component_type'].initial = instance.component_type.class_name - self.fields['description'].initial = instance.description - if instance.manufacturer: - self.fields['manufacturer'].initial = instance.manufacturer.name - if instance.package: - self.fields['package'].initial = instance.package.name - if instance.pref_distri: - self.fields['pref_distri'].initial = instance.pref_distri.name - self.fields['image'].initial = instance.image - - def clean_component_type(self): - data = self.cleaned_data['component_type'].strip() - - if len(data) == 0: - self.cleaned_data['component_type_object'] = None - return - - try: - self.cleaned_data['component_type_object'] = parts_models.ComponentType.objects.get(class_name=data) - except: - raise ValidationError('Invalid Component Type') - - def clean_manufacturer(self): - data = self.cleaned_data['manufacturer'].strip() - - if len(data) == 0: - self.cleaned_data['manufacturer_object'] = None - return - try: - self.cleaned_data['manufacturer_object'] = parts_models.Manufacturer.objects.get(name=data) - except: - raise ValidationError('Invalid Manufacturer') - - def clean_pref_distri(self): - data = self.cleaned_data['pref_distri'].strip() - - if len(data) == 0: - self.cleaned_data['pref_distri_object'] = None - return - try: - self.cleaned_data['pref_distri_object'] = parts_models.Distributor.objects.get(name=data) - except: - raise ValidationError('Invalid Distributor') - - def clean_package(self): - data = self.cleaned_data['package'].strip() - - if len(data) == 0: - self.cleaned_data['package_object'] = None - return - try: - self.cleaned_data['package_object'] = parts_models.Package.objects.get(name=data) - except: - raise ValidationError('Invalid Package') - - - def _save_new(self): - self.instance = parts_models.Component.objects.create( - name=self.cleaned_data['name'], - datasheet_link=self.cleaned_data['datasheet_link'], - description=self.cleaned_data['description'], - package=self.cleaned_data['package_object'], - component_type=self.cleaned_data['component_type_object'], - pref_distri=self.cleaned_data['pref_distri_object'], - manufacturer=self.cleaned_data['manufacturer_object'] - ) - - if bool(self.cleaned_data['image']): - self.instance.image = self.cleaned_data['image'] - self.instance.save() - - def save(self): - if self.instance is None: - self._save_new() - return - - self.instance.name = self.cleaned_data['name'] - self.instance.datasheet_link = self.cleaned_data['datasheet_link'] - self.instance.description = self.cleaned_data['description'] - - if bool(self.cleaned_data['image']) is False: - self.instance.image = None - else: - self.instance.image = self.cleaned_data['image'] - - self.instance.manufacturer = self.cleaned_data['manufacturer_object'] - self.instance.pref_distri = self.cleaned_data['pref_distri_object'] - self.instance.package = self.cleaned_data['package_object'] - self.instance.component_type = self.cleaned_data['component_type_object'] - self.instance.save() - - class PackageForm(forms.ModelForm): class Meta: model = parts_models.Package diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 17d52e6..0442aec 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -147,7 +147,25 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): comp_paginator = Paginator(Component.objects.all(), self.default_page_size) context['components'] = comp_paginator.get_page(comp_page_num) + context['comp_form'] = ComponentForm() + return context + + def handle_new_component_post(self, request, **kwargs): + cform = ComponentForm(data=request.POST, files=request.FILES) + if cform.is_valid(): + cform.save() + + context = self.get_context_data(**kwargs) + if not cform.is_valid(): + context['comp_form'] = cform + return self.render_to_response(context) + + def post(self, request, *args, **kwargs): + if 'submit-edit-component' in request.POST: + return self.handle_new_component_post(request, **kwargs) + else: + return super().post(request, *args, **kwargs) class PackageView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): template_name = 'parts/packages.html' @@ -518,55 +536,50 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): self.base_title = 'Component / '+self.object.name context = super().get_context_data(**kwargs) context['component'] = self.object - context['edit_form'] = EditComponentForm(instance=self.object) - context['stocks'] = Stock.objects.filter(component=self.object) context['comp_form'] = ComponentForm(instance=self.object) return context - def handle_submit_edit_post(self, request, **kwargs): - form_error = False - - form = EditComponentForm(instance=self.object, data=request.POST, files=request.FILES) - if form.is_valid(): - try: - form.save() - except IntegrityError as ie: - form.add_error('name', 'Component name, package, and manufacturer are not unique!') - form.add_error('package', 'Component name, package, and manufacturer are not unique!') - form.add_error('manufacturer', 'Component name, package, and manufacturer are not unique!') - form_error = True - self.object = self.get_object() - else: - form_error = True - - context = self.get_context_data(**kwargs) - if form_error: - context['edit_form'] = form - return self.render_to_response(context) - def handle_submit_edit_component_post(self, request, **kwargs): - context = self.get_context_data(**kwargs) - cform = ComponentForm(instance=self.object, data=request.POST, files=request.FILES) if cform.is_valid(): - print('valid') cform.save() - else: - print('invalid') - context['comp_form'] = cform + + context = self.get_context_data(**kwargs) + if not cform.is_valid(): + context['comp_form'] = cform return self.render_to_response(context) + 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) + def post(self, request, *args, **kwargs): self.object = self.get_object() - if 'submit-edit-comp' in request.POST: - return self.handle_submit_edit_post(request, **kwargs) if 'submit-edit-component' in request.POST: return self.handle_submit_edit_component_post(request, **kwargs) - - return super().post(request, *args, **kwargs) + elif 'submit-component-delete' in request.POST: + return self.handle_submit_delete_post(request, **kwargs) + else: + return super().post(request, *args, **kwargs) class PackageDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): template_name = 'parts/packages-detail.html' diff --git a/shimatta_kenkyusho/templates/parts/components-detail.html b/shimatta_kenkyusho/templates/parts/components-detail.html index f96bee6..6d5d4ef 100644 --- a/shimatta_kenkyusho/templates/parts/components-detail.html +++ b/shimatta_kenkyusho/templates/parts/components-detail.html @@ -22,6 +22,7 @@ Datasheet {% endif %} +
@@ -144,15 +145,54 @@
{% endif %} + + + + + {% include 'parts/modals/edit-component-modal.html' with heading="Edit "|add:component.name form=comp_form %} {% endblock content %} {% block custom_scripts %} {% endblock custom_scripts %} diff --git a/shimatta_kenkyusho/templates/parts/components.html b/shimatta_kenkyusho/templates/parts/components.html index 9e4b78e..2c07673 100644 --- a/shimatta_kenkyusho/templates/parts/components.html +++ b/shimatta_kenkyusho/templates/parts/components.html @@ -5,7 +5,15 @@

Components

-
+
+
+ + +
+
+
{% for comp in components %}
  • @@ -35,4 +43,15 @@
  • -{% endblock content %} \ No newline at end of file + +{% include 'parts/modals/edit-component-modal.html' with form=comp_form heading='New Component' %} + +{% endblock content %} +{% block custom_scripts %} + + +{% endblock custom_scripts %} \ No newline at end of file