From 14728c851337e8b5a840b3e0d2397e1db3365677 Mon Sep 17 00:00:00 2001 From: Qwann Date: Thu, 9 Aug 2018 16:06:24 +0200 Subject: [PATCH] category full name --- equipment/admin.py | 4 +-- .../migrations/0011_auto_20180809_1405.py | 20 +++++++++++++ equipment/models.py | 30 ++++++++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 equipment/migrations/0011_auto_20180809_1405.py diff --git a/equipment/admin.py b/equipment/admin.py index ac9ef94..391715d 100644 --- a/equipment/admin.py +++ b/equipment/admin.py @@ -63,7 +63,7 @@ class EquipmentAttributeValueInline(admin.TabularInline): class CategoryAdmin(admin.ModelAdmin): - list_display = ['name', 'parent'] + list_display = ['name', 'parent', "full_name"] ordering = ['name', 'parent'] @@ -73,7 +73,7 @@ class EquipmentAttributeAdmin(admin.ModelAdmin): class EquipmentAdmin(admin.ModelAdmin): - readonly_fields = ['added_at', 'modified_at'] + readonly_fields = ['full_category', 'added_at', 'modified_at',] list_display = ['name', 'stock', 'owner', 'category', 'modified_at'] ordering = ['name', 'owner', 'category'] inlines = [EquipmentAttributeValueInline, diff --git a/equipment/migrations/0011_auto_20180809_1405.py b/equipment/migrations/0011_auto_20180809_1405.py new file mode 100644 index 0000000..d99cf4c --- /dev/null +++ b/equipment/migrations/0011_auto_20180809_1405.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.11 on 2018-08-09 14:05 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('equipment', '0010_auto_20180809_1211'), + ] + + operations = [ + migrations.AlterField( + model_name='equipment', + name='description', + field=models.TextField(blank=True, verbose_name='description'), + ), + ] diff --git a/equipment/models.py b/equipment/models.py index 86f0c28..58145f7 100644 --- a/equipment/models.py +++ b/equipment/models.py @@ -23,6 +23,24 @@ class EquipmentCategory(models.Model): help_text=_("merci de ne pas faire de référence cyclique"), ) + def full_name_property(self): + if self.parent is None: + return "-" + current = self + res = "" + for k in range(100): + if current.parent is None: + break + res = "/{current}{old}".format( + current=current.name, + old=res) + current = current.parent + return res + + full_name_property.short_description = _("Chemin complet") + full_name = property(full_name_property) + + class Meta: verbose_name = _("catégories") verbose_name_plural = _("catégories") @@ -51,7 +69,10 @@ class Equipment(EventSpecificMixin, models.Model): max_length=200, ) stock = models.PositiveSmallIntegerField(_("quantité totale")) - description = models.TextField(_("description")) + description = models.TextField( + _("description"), + blank=True, + ) activities = models.ManyToManyField( Activity, related_name="equipment", @@ -76,6 +97,13 @@ class Equipment(EventSpecificMixin, models.Model): auto_now=True, ) + def full_category_property(self): + return self.category.full_name_property() + + full_category_property.short_description = _("Chemin complet") + full_category= property(full_category_property) + + class Meta: verbose_name = _("matériel") verbose_name_plural = _("matériels")