From a566e198b806e91382241b3be793c7c434280511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 11 Jan 2022 19:35:09 +0100 Subject: [PATCH] Implement key parameter rendering for components. In Stock viewer still missing --- shimatta_kenkyusho/parts/models.py | 35 +++++++++++++++++-- shimatta_kenkyusho/parts/views.py | 10 ++++-- .../templates/parts/components-detail.html | 4 +-- .../templates/parts/components.html | 7 +++- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/shimatta_kenkyusho/parts/models.py b/shimatta_kenkyusho/parts/models.py index bf23f77..d8d8d60 100644 --- a/shimatta_kenkyusho/parts/models.py +++ b/shimatta_kenkyusho/parts/models.py @@ -240,7 +240,34 @@ class Component(models.Model): sum = 0 return sum - + def get_key_parameters(self): + """ + Get the key parameters of a component defined by its component type. + Returns a tuple of 3 elements. All three might be None + """ + p1 = None + p2 = None + p3 = None + + if self.component_type: + t = (self.component_type.key_parameter1, self.component_type.key_parameter2, self.component_type.key_parameter3) + if t[0]: + p1 = ComponentParameter.objects.filter(component=self, parameter_type=t[0]).first() + if t[1]: + p2 = ComponentParameter.objects.filter(component=self, parameter_type=t[1]).first() + if t[2]: + p3 = ComponentParameter.objects.filter(component=self, parameter_type=t[2]).first() + + return (p1, p2, p3) + + def get_key_parameters_as_text(self): + params = self.get_key_parameters() + ret_strings = [] + for p in params: + if p: + ret_strings.append(p.resolved_value_as_string()) + return ret_strings + class ComponentParameter(models.Model): class Meta: unique_together = ('component', 'parameter_type') @@ -265,10 +292,12 @@ class ComponentParameter(models.Model): 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}' + num = round(num, 3) + return f'{num} {prefix}{self.parameter_type.unit}' elif my_type == 'N': # Standard float number - return f'{self.value:.3f} {self.parameter_type.unit}' + num = round(self.value, 3) + return f'{num} {self.parameter_type.unit}' elif my_type == 'F': return self.text_value diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index e4183b4..bc7ab0d 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -19,6 +19,7 @@ from django.db import IntegrityError from django.db.models import ProtectedError from .forms import * from django.db.models import Q +from django.db.models import Prefetch from django.db.models.functions import Lower from django.forms import formset_factory import uuid @@ -149,7 +150,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): default_page_size = 25 def get_component_query_set(self, search_string): - queryset = Component.objects.select_related('package', 'manufacturer').all() + queryset = Component.objects.select_related('package', 'manufacturer', 'component_type').prefetch_related('componentparameter_set').all() if search_string is None or search_string == '': return queryset @@ -621,7 +622,7 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): template_name = 'parts/components-detail.html' - model = Component + queryset = Component.objects.select_related('component_type', 'package', 'manufacturer').prefetch_related('componentparameter_set') pk_url_kwarg = 'uuid' base_title = '' navbar_selected = 'Components' @@ -638,7 +639,10 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): 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') - + + parameter_texts = self.object.get_key_parameters_as_text() + context['key_parameter_string'] = ', '.join(parameter_texts) + 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 343be45..c4fe01b 100644 --- a/shimatta_kenkyusho/templates/parts/components-detail.html +++ b/shimatta_kenkyusho/templates/parts/components-detail.html @@ -39,7 +39,7 @@ - {{component.name}} + {{component.name}}{% if key_parameter_string %}
{{key_parameter_string}}{% endif %} {% if component.package %} @@ -128,7 +128,7 @@ {{param.resolved_value_as_string}} - from Package + from Package {% endfor %} {% for param in parameters %} diff --git a/shimatta_kenkyusho/templates/parts/components.html b/shimatta_kenkyusho/templates/parts/components.html index 43a8459..12bfa07 100644 --- a/shimatta_kenkyusho/templates/parts/components.html +++ b/shimatta_kenkyusho/templates/parts/components.html @@ -48,7 +48,12 @@ {% endif %}
-
{{ comp.name }}
+
+ {{ comp.name }} + {% for key_param in comp.get_key_parameters_as_text %} + {{key_param}} + {% endfor %} +
{% if comp.package %} Package: {{comp.package}}
{% endif %}