From 70d1d403f65f7a9ba185f0cfa5727cc634ad3e4e Mon Sep 17 00:00:00 2001 From: Qwann Date: Wed, 8 Aug 2018 16:45:29 +0200 Subject: [PATCH] equipment attributes --- equipment/admin.py | 16 +++++++- ...ipmentattribute_equipmentattributevalue.py | 40 +++++++++++++++++++ equipment/models.py | 38 ++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 equipment/migrations/0007_equipmentattribute_equipmentattributevalue.py diff --git a/equipment/admin.py b/equipment/admin.py index 1442f72..7328176 100644 --- a/equipment/admin.py +++ b/equipment/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin from django import forms -from .models import Equipment, EquipmentDefault, EquipmentRevision, EquipmentCategory, EquipmentLost +from .models import Equipment, EquipmentDefault, EquipmentRevision, EquipmentCategory, EquipmentLost, EquipmentAttributeValue, EquipmentAttribute from .fields import IdField, IdWidget from shared.admin import admin_site @@ -57,19 +57,31 @@ class EquipmentLostExtraInline(admin.TabularInline): classes = ['collapse'] +class EquipmentAttributeValueInline(admin.TabularInline): + model = EquipmentAttributeValue + extra = 0 + + class CategoryAdmin(admin.ModelAdmin): list_display = ['name', 'parent'] ordering = ['name', 'parent'] +class EquipmentAttributeAdmin(admin.ModelAdmin): + list_display = ['name'] + ordering = ['name'] + + class EquipmentAdmin(admin.ModelAdmin): list_display = ['name', 'stock', 'owner', 'category'] ordering = ['name', 'owner', 'category'] - inlines = [EquipmentDefaultExtraInline, + inlines = [EquipmentAttributeValueInline, + EquipmentDefaultExtraInline, EquipmentLostExtraInline, EquipmentRevisionExtraInline] admin_site.register(Equipment, EquipmentAdmin) admin_site.register(EquipmentCategory, CategoryAdmin) +admin_site.register(EquipmentAttribute, EquipmentAttributeAdmin) admin.site.register(Equipment, EquipmentAdmin) diff --git a/equipment/migrations/0007_equipmentattribute_equipmentattributevalue.py b/equipment/migrations/0007_equipmentattribute_equipmentattributevalue.py new file mode 100644 index 0000000..db8a274 --- /dev/null +++ b/equipment/migrations/0007_equipmentattribute_equipmentattributevalue.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.11 on 2018-08-08 14:40 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('equipment', '0006_auto_20180808_1354'), + ] + + operations = [ + migrations.CreateModel( + name='EquipmentAttribute', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=200, unique=True, verbose_name='nom')), + ], + options={ + 'verbose_name': 'attribut', + 'verbose_name_plural': 'attributs', + }, + ), + migrations.CreateModel( + name='EquipmentAttributeValue', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('value', models.CharField(max_length=200, verbose_name='valeur')), + ('attribute', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='equipment.EquipmentAttribute')), + ('equipment', models.ForeignKey(help_text='Matériel concerné par le defaut', on_delete=django.db.models.deletion.CASCADE, related_name='attributes', to='equipment.Equipment')), + ], + options={ + 'verbose_name': 'attribut de matériel', + 'verbose_name_plural': 'attributs de matériel', + }, + ), + ] diff --git a/equipment/models.py b/equipment/models.py index aafc5e7..232ab01 100644 --- a/equipment/models.py +++ b/equipment/models.py @@ -60,6 +60,44 @@ class Equipment(EventSpecificMixin, models.Model): return self.name +class EquipmentAttribute(models.Model): + name = models.CharField( + _("nom"), + max_length=200, + unique=True, + ) + + class Meta: + verbose_name = _("attribut") + verbose_name_plural = _("attributs") + + def __str__(self): + return self.name + +class EquipmentAttributeValue(models.Model): + equipment = models.ForeignKey( + Equipment, + related_name="attributes", + help_text=_("Matériel concerné par le defaut"), + ) + attribute = models.ForeignKey( + EquipmentAttribute, + on_delete=models.CASCADE, + ) + value = models.CharField( + _("valeur"), + max_length=200, + ) + + class Meta: + verbose_name = _("attribut de matériel") + verbose_name_plural = _("attributs de matériel") + + def __str__(self): + return "{attr}={value}".format(attr=self.attribute.name, + value=self.value) + + class EquipmentAttribution(models.Model): equipment = models.ForeignKey(Equipment) activity = models.ForeignKey(Activity)