From e2aba765d4e5736ce70a413c5640a71cd9115dfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Wed, 10 Nov 2021 19:38:39 +0100 Subject: [PATCH] Add a lot of stuff.. Can't remember --- shimatta_kenkyusho/parts/forms.py | 97 ++++++++++++++++++- shimatta_kenkyusho/parts/views.py | 32 +++++- .../shimatta_kenkyusho/settings.py | 2 +- .../templates/parts/components-detail.html | 67 +++++++++++-- .../parts/modals/edit-component-modal.html | 27 ++++++ .../templates/parts/stocks-detail.html | 6 +- 6 files changed, 213 insertions(+), 18 deletions(-) create mode 100644 shimatta_kenkyusho/templates/parts/modals/edit-component-modal.html diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index cdc264a..0e726ce 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -104,4 +104,99 @@ class AddStockForm(forms.Form): new_stock = parts_models.Stock.objects.create(storage=storage, component=component, watermark=watermark, amount=amount) new_stock.save() - \ No newline at end of file + +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) + component_type = forms.CharField(required=False, label='Component Type') + pref_distri = forms.CharField(required=False, label='Preferred Distributor') + package = forms.CharField(required=False) + + 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(self): + if self.instance is None: + self.instance = parts_models.Component.objects.create() + + 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() diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 561bbb9..9ad92dd 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -15,7 +15,7 @@ from .models import Storage, Stock, Component, Distributor, Manufacturer, Packag from .qr_parser import QrCodeValidator from django.core.paginator import Paginator from django.core.exceptions import ValidationError -from .forms import MyTestForm, AddSubStorageForm, DeleteStockForm, EditWatermarkForm, EditStockAmountForm, AddStockForm +from .forms import MyTestForm, AddSubStorageForm, DeleteStockForm, EditWatermarkForm, EditStockAmountForm, AddStockForm, EditComponentForm from django.db.models import Q from django.db.models.functions import Lower import uuid @@ -233,6 +233,10 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): if search is None or search == '': return stocks_in_storage + if search.startswith('[comp_uuid]'): + search = search.replace('[comp_uuid]', '') + + # Check if the searhc equals a UUID test_uuid = None try: @@ -391,11 +395,31 @@ 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) 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(): + form.save() + else: + print("Error") + form_error = True + + context = self.get_context_data(**kwargs) + if form_error: + context['edit_form'] = form + 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) + + super().post(request, *args, **kwargs) + class PackageDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): template_name = 'parts/packages-detail.html' model = Package diff --git a/shimatta_kenkyusho/shimatta_kenkyusho/settings.py b/shimatta_kenkyusho/shimatta_kenkyusho/settings.py index 8aa2312..4ea2bcd 100644 --- a/shimatta_kenkyusho/shimatta_kenkyusho/settings.py +++ b/shimatta_kenkyusho/shimatta_kenkyusho/settings.py @@ -26,7 +26,7 @@ SECRET_KEY = 'django-insecure-vq_@ue3ul@&4bz7wkcpf3pjrwf8o$7g!z-rw$ftr-$)7l3*m=^ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['localhost'] # Application definition diff --git a/shimatta_kenkyusho/templates/parts/components-detail.html b/shimatta_kenkyusho/templates/parts/components-detail.html index e21a2eb..44d2a3d 100644 --- a/shimatta_kenkyusho/templates/parts/components-detail.html +++ b/shimatta_kenkyusho/templates/parts/components-detail.html @@ -4,16 +4,55 @@ {% block content %}
-
-
- {% if component.get_resolved_image %} - {{component.name}} - {% else %} - {{component.name}} +
+
+
+ {% if component.get_resolved_image %} + {{component.name}} + {% else %} + {{component.name}} + {% endif %} +
+
+ {% qr_from_text component.get_qr_code size="m" image_format="svg" %} +
+
+ {% if component.datasheet_link %} + Datasheet {% endif %} + +
-
- {% qr_from_text component.get_qr_code size="m" image_format="svg" %} +
+ + + + + + + + + + + + + + + + + + +
NamePackageManufacturerTypeTotal #
{{component.name}}{% if component.package %}{{component.package.name}}{% endif %}{% if component.manufacturer %}{{component.manufacturer.name}}{% endif %}{% if component.component_type %}{{component.component_type.class_name}}{% endif %} + {{component.get_total_amount}}
+

Description

+ {% if component.description %} + {{component.description}} + {% else %} + + {% endif %} +
@@ -41,5 +80,15 @@
{% endif %} +{% include 'parts/modals/edit-component-modal.html' with heading="Edit "|add:component.name edit_form=edit_form %} +{% endblock content %} + +{% block custom_scripts %} + + +{% endblock custom_scripts %} -{% endblock content %} \ No newline at end of file diff --git a/shimatta_kenkyusho/templates/parts/modals/edit-component-modal.html b/shimatta_kenkyusho/templates/parts/modals/edit-component-modal.html new file mode 100644 index 0000000..e5fab77 --- /dev/null +++ b/shimatta_kenkyusho/templates/parts/modals/edit-component-modal.html @@ -0,0 +1,27 @@ +{% comment "" %} +Needs following context: +- heading +- edit_form EditComponentForm +{% endcomment %} + +{% load crispy_forms_tags %} + + \ No newline at end of file diff --git a/shimatta_kenkyusho/templates/parts/stocks-detail.html b/shimatta_kenkyusho/templates/parts/stocks-detail.html index dd3e47d..3b81a22 100644 --- a/shimatta_kenkyusho/templates/parts/stocks-detail.html +++ b/shimatta_kenkyusho/templates/parts/stocks-detail.html @@ -47,15 +47,15 @@

Stocked Components

-
+
-
{% for stock in stocks %}