From 503ede44e3d5fdc149f45cfa185449289da58ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 14 Nov 2021 16:48:34 +0100 Subject: [PATCH] Implement package-detail view --- shimatta_kenkyusho/parts/forms.py | 7 +- .../migrations/0006_auto_20211114_1524.py | 29 ++++++ shimatta_kenkyusho/parts/models.py | 6 +- shimatta_kenkyusho/parts/views.py | 46 +++++++++- .../templates/parts/packages-detail.html | 92 ++++++++++++++++++- 5 files changed, 172 insertions(+), 8 deletions(-) create mode 100644 shimatta_kenkyusho/parts/migrations/0006_auto_20211114_1524.py diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index 01cded1..149e771 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -291,4 +291,9 @@ class DistributorNumberFormSet(forms.BaseModelFormSet): instance.component = component if commit: instance.save() - return instances \ No newline at end of file + return instances + +class PackageForm(forms.ModelForm): + class Meta: + model = parts_models.Package + fields = '__all__' \ No newline at end of file diff --git a/shimatta_kenkyusho/parts/migrations/0006_auto_20211114_1524.py b/shimatta_kenkyusho/parts/migrations/0006_auto_20211114_1524.py new file mode 100644 index 0000000..8cc2833 --- /dev/null +++ b/shimatta_kenkyusho/parts/migrations/0006_auto_20211114_1524.py @@ -0,0 +1,29 @@ +# Generated by Django 3.2 on 2021-11-14 15:24 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('parts', '0005_auto_20211113_1912'), + ] + + operations = [ + migrations.AlterField( + model_name='component', + name='manufacturer', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='parts.manufacturer'), + ), + migrations.AlterField( + model_name='component', + name='package', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='parts.package'), + ), + migrations.AlterField( + model_name='component', + name='pref_distri', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='parts.distributor'), + ), + ] diff --git a/shimatta_kenkyusho/parts/models.py b/shimatta_kenkyusho/parts/models.py index ad5db2e..12ecce6 100644 --- a/shimatta_kenkyusho/parts/models.py +++ b/shimatta_kenkyusho/parts/models.py @@ -157,12 +157,12 @@ class Component(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True) name = models.CharField(max_length=100) - manufacturer = models.ForeignKey(Manufacturer, on_delete=models.SET_NULL, blank=True, null=True) + manufacturer = models.ForeignKey(Manufacturer, on_delete=models.PROTECT, blank=True, null=True) component_type = models.ForeignKey(ComponentType, on_delete=models.SET_NULL, blank=True, null=True) - pref_distri = models.ForeignKey(Distributor, on_delete=models.SET_NULL, blank=True, null=True) + pref_distri = models.ForeignKey(Distributor, on_delete=models.PROTECT, blank=True, null=True) description = models.TextField(null=True, blank=True) datasheet_link = models.CharField(max_length=300, null=True, blank=True) - package = models.ForeignKey(Package, on_delete=models.SET_NULL, blank=True, null=True) + package = models.ForeignKey(Package, on_delete=models.PROTECT, blank=True, null=True) image = models.ImageField(upload_to=RandomFileName.RandomFileName('component-images'), blank=True, null=True) def __str__(self): diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 7733aad..d1d8788 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -16,7 +16,8 @@ from .qr_parser import QrCodeValidator from django.core.paginator import Paginator from django.core.exceptions import ValidationError from django.db import IntegrityError -from .forms import MyTestForm, AddSubStorageForm, DeleteStockForm, EditWatermarkForm, EditStockAmountForm, AddStockForm, EditComponentForm, EditComponentParameterForm, DistributorNumberForm, DistributorNumberFormSet +from django.db.models import ProtectedError +from .forms import * from django.db.models import Q from django.db.models.functions import Lower import uuid @@ -527,5 +528,48 @@ class PackageDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): self.base_title = 'Package / '+self.object.name context = super().get_context_data(**kwargs) context['package'] = self.object + context['edit_form'] = PackageForm(instance=self.object) return context + + 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: + return redirect('parts-main') + + 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) + + return super().post(request, *args, **kwargs) \ No newline at end of file diff --git a/shimatta_kenkyusho/templates/parts/packages-detail.html b/shimatta_kenkyusho/templates/parts/packages-detail.html index ed21f33..ca00bfb 100644 --- a/shimatta_kenkyusho/templates/parts/packages-detail.html +++ b/shimatta_kenkyusho/templates/parts/packages-detail.html @@ -1,8 +1,94 @@ {% extends 'base.html' %} +{% load static %} +{% load crispy_forms_tags %} + {% block content %}
- Meow
- {{package}} +
+
+
+ {% if package.image %} + {{package.name}} + {% else %} + {{package.name}} + {% endif %} +
+
+ +
+
+
+

Package {{package.name}}

+
+ {% csrf_token %} + {{edit_form|crispy}} + +
+
+
-{% endblock content %} \ No newline at end of file + +{% if package.image %} + +{% endif %} + + + + + +{% endblock content %} + +{% block custom_scripts %} + + +{% endblock custom_scripts %} \ No newline at end of file