From ea623212bb57cdf4c4142636d7438487f84e5d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 10 Jan 2022 19:22:34 +0100 Subject: [PATCH] Add package parameters --- shimatta_kenkyusho/parts/admin.py | 1 + .../migrations/0011_auto_20220110_1812.py | 44 +++++++++++++++++++ shimatta_kenkyusho/parts/models.py | 36 ++++++++++++++- shimatta_kenkyusho/parts/views.py | 31 +++++-------- .../templates/parts/components-detail.html | 18 ++++++++ 5 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 shimatta_kenkyusho/parts/migrations/0011_auto_20220110_1812.py diff --git a/shimatta_kenkyusho/parts/admin.py b/shimatta_kenkyusho/parts/admin.py index 046e01b..95de51e 100644 --- a/shimatta_kenkyusho/parts/admin.py +++ b/shimatta_kenkyusho/parts/admin.py @@ -9,6 +9,7 @@ admin.site.register(parts_models.Manufacturer) admin.site.register(parts_models.Storage) admin.site.register(parts_models.Stock) admin.site.register(parts_models.ComponentParameter) +admin.site.register(parts_models.PackageParameter) admin.site.register(parts_models.ComponentParameterType) admin.site.register(parts_models.ComponentType) admin.site.register(parts_models.Distributor) diff --git a/shimatta_kenkyusho/parts/migrations/0011_auto_20220110_1812.py b/shimatta_kenkyusho/parts/migrations/0011_auto_20220110_1812.py new file mode 100644 index 0000000..acea38a --- /dev/null +++ b/shimatta_kenkyusho/parts/migrations/0011_auto_20220110_1812.py @@ -0,0 +1,44 @@ +# Generated by Django 3.2.5 on 2022-01-10 18:12 + +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('parts', '0010_auto_20220103_1606'), + ] + + operations = [ + migrations.AddField( + model_name='componenttype', + name='key_parameter1', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='type_param1', to='parts.componentparametertype'), + ), + migrations.AddField( + model_name='componenttype', + name='key_parameter2', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='type_param2', to='parts.componentparametertype'), + ), + migrations.AddField( + model_name='componenttype', + name='key_parameter3', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='type_param3', to='parts.componentparametertype'), + ), + migrations.CreateModel( + name='PackageParameter', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)), + ('value', models.FloatField(default=0)), + ('text_value', models.TextField(blank=True)), + ('package', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='parts.package')), + ('parameter_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='parts.componentparametertype')), + ], + options={ + 'ordering': ['id'], + 'unique_together': {('package', 'parameter_type')}, + }, + ), + ] diff --git a/shimatta_kenkyusho/parts/models.py b/shimatta_kenkyusho/parts/models.py index 075003c..bf23f77 100644 --- a/shimatta_kenkyusho/parts/models.py +++ b/shimatta_kenkyusho/parts/models.py @@ -49,7 +49,10 @@ class ComponentType(models.Model): class_name = models.CharField(max_length=50, unique=True) passive = models.BooleanField() possible_parameter = models.ManyToManyField(ComponentParameterType, blank=True) - + key_parameter1 = models.ForeignKey(ComponentParameterType, on_delete=models.CASCADE, blank=True, null=True, related_name="type_param1") + key_parameter2 = models.ForeignKey(ComponentParameterType, on_delete=models.CASCADE, blank=True, null=True, related_name="type_param2") + key_parameter3 = models.ForeignKey(ComponentParameterType, on_delete=models.CASCADE, blank=True, null=True, related_name="type_param3") + def __str__(self): return '[' + self.class_name + ']' @@ -144,6 +147,37 @@ class Package(models.Model): def __str__(self): return self.name +class PackageParameter(models.Model): + class Meta: + unique_together = ('package', 'parameter_type') + ordering = ['id'] + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True) + package = models.ForeignKey(Package, on_delete=models.CASCADE) # A target package is required! + parameter_type = models.ForeignKey(ComponentParameterType, on_delete=models.CASCADE) + value = models.FloatField(default=0) + text_value = models.TextField(null=False, blank=True) + + def __str__(self): + if self.parameter_type.parameter_type == 'F': + value = self.text_value + else: + value = str(self.value) + + return str(self.package)+ ': '+ str(self.parameter_type) + ': ' + value + + def resolved_value_as_string(self): + my_type = self.parameter_type.parameter_type + + if my_type == 'E' or my_type == 'I': + # Engineering float number + (num, prefix) = NumConv.number_to_engineering(self.value, it_unit=(True if my_type=='I' else False)) + return f'{num:.3f} {prefix}{self.parameter_type.unit}' + elif my_type == 'N': + # Standard float number + return f'{self.value:.3f} {self.parameter_type.unit}' + elif my_type == 'F': + return self.text_value + class Manufacturer(models.Model): class Meta: diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 7c71ace..e4183b4 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -11,7 +11,7 @@ from django.views import View import django.forms as forms from django.views.generic import TemplateView, DetailView from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin -from .models import Storage, Stock, Component, Distributor, Manufacturer, Package, ComponentParameter, ComponentParameterType, DistributorNum +from .models import Storage, Stock, Component, Distributor, Manufacturer, Package, ComponentParameter, ComponentParameterType, DistributorNum, PackageParameter from .qr_parser import QrCodeValidator from django.core.paginator import Paginator from django.core.exceptions import ValidationError @@ -23,7 +23,7 @@ from django.db.models.functions import Lower from django.forms import formset_factory import uuid -ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=1) +ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=0) class QrSearchForm(forms.Form): my_qr_validator = QrCodeValidator() @@ -211,12 +211,11 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): else: paginator_queryset = self.get_component_query_set(None) - if parameter_formset.is_valid(): - # Process parameters - for f in parameter_formset: - # If the form is valid and has changed compared to its initial empty state - if f.is_valid() and f.has_changed(): - paginator_queryset = self.filter_queryset_with_parameters(paginator_queryset, f.cleaned_data['parameter'], f.cleaned_data['value'], f.cleaned_data['compare_method']) + # Process parameters + for f in parameter_formset: + # If the form is valid and has changed compared to its initial empty state + if f.is_valid() and f.has_changed(): + paginator_queryset = self.filter_queryset_with_parameters(paginator_queryset, f.cleaned_data['parameter'], f.cleaned_data['value'], f.cleaned_data['compare_method']) else: @@ -243,9 +242,9 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): adv_search_form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=self.request.GET) adv_param_search_formset = ParameterSearchFormSet(data=self.request.GET) if adv_search_form.is_valid(): - print('Advanced search valid') + pass if adv_param_search_formset.is_valid(): - print('Parameter search valid') + pass return self.get_context_data_int(advanced_search = adv_search_form, parameter_formset=adv_param_search_formset, **kwargs) @@ -264,14 +263,6 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): if open and new_component: return redirect(reverse('parts-components-detail', kwargs={'uuid':new_component.id})) return self.render_to_response(context) - - def handle_advanced_search_get(self, request, **kwargs): - - form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=request.POST) - param_formset = ParameterSearchFormSet(data=request.POST) - - context = self.get_context_data_int(form, param_formset, **kwargs) - return self.render_to_response(context) def post(self, request, *args, **kwargs): if 'submit-edit-component' in request.POST: @@ -645,7 +636,9 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): context['new_param_form'] = ComponentParameterCreateForm() context['distri_nums'] = DistributorNum.objects.filter(component=self.object).order_by('distributor__name') context['parameters'] = ComponentParameter.objects.filter(component=self.object).order_by('parameter_type__parameter_name') - + if self.object.package: + context['package_parameters'] = PackageParameter.objects.filter(package=self.object.package).order_by('parameter_type__parameter_name') + return context def handle_submit_edit_component_post(self, request, **kwargs): diff --git a/shimatta_kenkyusho/templates/parts/components-detail.html b/shimatta_kenkyusho/templates/parts/components-detail.html index a8d2163..343be45 100644 --- a/shimatta_kenkyusho/templates/parts/components-detail.html +++ b/shimatta_kenkyusho/templates/parts/components-detail.html @@ -119,6 +119,17 @@ + {% for param in package_parameters %} + +
+ {{param.parameter_type.parameter_name}} +
+ + + {{param.resolved_value_as_string}} + + from Package + {% endfor %} {% for param in parameters %} @@ -148,6 +159,13 @@ {% endif %} {% endfor %} + {% for param in package_parameters %} + {% if param.parameter_type.parameter_description %} +
+ {{param.parameter_type.parameter_description}} +
+ {% endif %} + {% endfor %}