Add package parameters

This commit is contained in:
Mario Hüttel 2022-01-10 19:22:34 +01:00
parent 2bc0f3124c
commit ea623212bb
5 changed files with 110 additions and 20 deletions

View File

@ -9,6 +9,7 @@ admin.site.register(parts_models.Manufacturer)
admin.site.register(parts_models.Storage) admin.site.register(parts_models.Storage)
admin.site.register(parts_models.Stock) admin.site.register(parts_models.Stock)
admin.site.register(parts_models.ComponentParameter) admin.site.register(parts_models.ComponentParameter)
admin.site.register(parts_models.PackageParameter)
admin.site.register(parts_models.ComponentParameterType) admin.site.register(parts_models.ComponentParameterType)
admin.site.register(parts_models.ComponentType) admin.site.register(parts_models.ComponentType)
admin.site.register(parts_models.Distributor) admin.site.register(parts_models.Distributor)

View File

@ -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')},
},
),
]

View File

@ -49,7 +49,10 @@ class ComponentType(models.Model):
class_name = models.CharField(max_length=50, unique=True) class_name = models.CharField(max_length=50, unique=True)
passive = models.BooleanField() passive = models.BooleanField()
possible_parameter = models.ManyToManyField(ComponentParameterType, blank=True) 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): def __str__(self):
return '[' + self.class_name + ']' return '[' + self.class_name + ']'
@ -144,6 +147,37 @@ class Package(models.Model):
def __str__(self): def __str__(self):
return self.name 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 Manufacturer(models.Model):
class Meta: class Meta:

View File

@ -11,7 +11,7 @@ from django.views import View
import django.forms as forms import django.forms as forms
from django.views.generic import TemplateView, DetailView from django.views.generic import TemplateView, DetailView
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin 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 .qr_parser import QrCodeValidator
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -23,7 +23,7 @@ from django.db.models.functions import Lower
from django.forms import formset_factory from django.forms import formset_factory
import uuid import uuid
ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=1) ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=0)
class QrSearchForm(forms.Form): class QrSearchForm(forms.Form):
my_qr_validator = QrCodeValidator() my_qr_validator = QrCodeValidator()
@ -211,12 +211,11 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
else: else:
paginator_queryset = self.get_component_query_set(None) paginator_queryset = self.get_component_query_set(None)
if parameter_formset.is_valid(): # Process parameters
# Process parameters for f in parameter_formset:
for f in parameter_formset: # If the form is valid and has changed compared to its initial empty state
# If the form is valid and has changed compared to its initial empty state if f.is_valid() and f.has_changed():
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'])
paginator_queryset = self.filter_queryset_with_parameters(paginator_queryset, f.cleaned_data['parameter'], f.cleaned_data['value'], f.cleaned_data['compare_method'])
else: else:
@ -243,9 +242,9 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
adv_search_form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=self.request.GET) adv_search_form = AdvancedComponentSearchForm(auto_id='adv_search_%s', data=self.request.GET)
adv_param_search_formset = ParameterSearchFormSet(data=self.request.GET) adv_param_search_formset = ParameterSearchFormSet(data=self.request.GET)
if adv_search_form.is_valid(): if adv_search_form.is_valid():
print('Advanced search valid') pass
if adv_param_search_formset.is_valid(): 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) 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: if open and new_component:
return redirect(reverse('parts-components-detail', kwargs={'uuid':new_component.id})) return redirect(reverse('parts-components-detail', kwargs={'uuid':new_component.id}))
return self.render_to_response(context) 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): def post(self, request, *args, **kwargs):
if 'submit-edit-component' in request.POST: if 'submit-edit-component' in request.POST:
@ -645,7 +636,9 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView):
context['new_param_form'] = ComponentParameterCreateForm() context['new_param_form'] = ComponentParameterCreateForm()
context['distri_nums'] = DistributorNum.objects.filter(component=self.object).order_by('distributor__name') 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') 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 return context
def handle_submit_edit_component_post(self, request, **kwargs): def handle_submit_edit_component_post(self, request, **kwargs):

View File

@ -119,6 +119,17 @@
<th scope="col"></th> <th scope="col"></th>
</thead> </thead>
<tbody> <tbody>
{% for param in package_parameters %}
<td>
<h6 {% if param.parameter_type.parameter_description %} class="accordion-header" data-bs-toggle="collapse" data-bs-target="#collapse-pkg-parameter-desc-{{forloop.counter}}"{% endif %}>
{{param.parameter_type.parameter_name}}
</h6>
</td>
<td>
{{param.resolved_value_as_string}}
</td>
<td><span class="text-info">from Package</span></td>
{% endfor %}
{% for param in parameters %} {% for param in parameters %}
<tr> <tr>
<td> <td>
@ -148,6 +159,13 @@
</div> </div>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% for param in package_parameters %}
{% if param.parameter_type.parameter_description %}
<div class="collapse accordion-collapse" id="collapse-pkg-parameter-desc-{{forloop.counter}}" data-bs-parent="#accordion-param-desc">
{{param.parameter_type.parameter_description}}
</div>
{% endif %}
{% endfor %}
</div> </div>
</div> </div>
<div class="col"> <div class="col">