diff --git a/shimatta_kenkyusho/parts/migrations/0012_componenttype_parent_class.py b/shimatta_kenkyusho/parts/migrations/0012_componenttype_parent_class.py new file mode 100644 index 0000000..9558e45 --- /dev/null +++ b/shimatta_kenkyusho/parts/migrations/0012_componenttype_parent_class.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.5 on 2022-11-11 20:18 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('parts', '0011_auto_20220110_1812'), + ] + + operations = [ + migrations.AddField( + model_name='componenttype', + name='parent_class', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='child_classes', to='parts.componenttype'), + ), + ] diff --git a/shimatta_kenkyusho/parts/models.py b/shimatta_kenkyusho/parts/models.py index d8d8d60..6766920 100644 --- a/shimatta_kenkyusho/parts/models.py +++ b/shimatta_kenkyusho/parts/models.py @@ -47,6 +47,7 @@ class ComponentType(models.Model): class Meta: ordering = ['class_name'] class_name = models.CharField(max_length=50, unique=True) + parent_class = models.ForeignKey('self', on_delete=models.PROTECT, related_name='child_classes', null=True, blank=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") @@ -54,7 +55,27 @@ class ComponentType(models.Model): key_parameter3 = models.ForeignKey(ComponentParameterType, on_delete=models.CASCADE, blank=True, null=True, related_name="type_param3") def __str__(self): - return '[' + self.class_name + ']' + return self.get_full_path() + + + def get_path_components(self): + chain = [] + iterator = self + chain.append(self) + while iterator.parent_class is not None: + chain.append(iterator.parent_class) + iterator = iterator.parent_class + + return chain + + def get_full_path(self): + output = '' + + chain = self.get_path_components() + + for i in range(len(chain) - 1, -1, -1): + output = output + ' / ' + chain[i].class_name + return output class Storage(models.Model): class Meta: diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index df15c71..db29e8b 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -186,8 +186,15 @@ class ComponentTypeDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView) pk_url_kwarg = 'uuid' template_name = 'parts/component-types-detail.html' + def get_breadcrumbs(self): + crumbs = self.object.get_path_components() + # Reverse list and drop the last element of the reversed list + crumbs = crumbs[::-1][:-1] + return crumbs + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) + context['breadcrumbs'] = self.get_breadcrumbs() return context diff --git a/shimatta_kenkyusho/templates/parts/component-types-detail.html b/shimatta_kenkyusho/templates/parts/component-types-detail.html index 0bbbdcd..b354876 100644 --- a/shimatta_kenkyusho/templates/parts/component-types-detail.html +++ b/shimatta_kenkyusho/templates/parts/component-types-detail.html @@ -3,10 +3,18 @@ {% block content %}
+

Component Type: {{object.class_name}}

-
diff --git a/shimatta_kenkyusho/templates/parts/component-types.html b/shimatta_kenkyusho/templates/parts/component-types.html index d8010c3..40dabee 100644 --- a/shimatta_kenkyusho/templates/parts/component-types.html +++ b/shimatta_kenkyusho/templates/parts/component-types.html @@ -22,7 +22,7 @@
  • - {{t.class_name}} + {{t}}
    {% if t.passive %}