Parcourir la source

Fix #69 Display mac or ref when creating a new loan in the admin interface

SimonBoulier il y a 7 ans
Parent
commit
a7d7eb977a
2 fichiers modifiés avec 28 ajouts et 10 suppressions
  1. 16 1
      hardware_provisioning/admin.py
  2. 12 9
      hardware_provisioning/models.py

+ 16 - 1
hardware_provisioning/admin.py

@@ -5,6 +5,7 @@ from __future__ import unicode_literals
 
 from django.contrib import admin
 from django.contrib.auth import get_user_model
+from django.forms import ModelChoiceField
 from django.utils import timezone
 
 from .models import ItemType, Item, Loan, Storage
@@ -119,9 +120,15 @@ class BorrowerFilter(admin.SimpleListFilter):
             return queryset
 
 
+class ItemChoiceField(ModelChoiceField):
+    # On surcharge cette méthode pour afficher mac et n° de série dans le menu
+    # déroulant de sélection d'un objet dans la création d'un prêt.
+    def label_from_instance(self, obj):
+        return obj.designation + ' ' + obj.get_mac_and_serial()
+
 @admin.register(Loan)
 class LoanAdmin(admin.ModelAdmin):
-    list_display = ('item', 'get_mac_or_serial', 'user', 'loan_date', 'loan_date_end')
+    list_display = ('item', 'get_mac_and_serial', 'user', 'loan_date', 'loan_date_end')
     list_filter = (StatusFilter, BorrowerFilter, 'item__designation')
     search_fields = (
         'item__designation',
@@ -135,6 +142,14 @@ class LoanAdmin(admin.ModelAdmin):
     end_loan.short_description = 'Mettre fin au prêt'
 
 
+    def formfield_for_foreignkey(self, db_field, request, **kwargs):
+        if db_field.name == 'item':
+            kwargs['queryset'] = Item.objects.all()
+            return ItemChoiceField(**kwargs)
+        else:
+            return super(LoanAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
+
+
 @admin.register(Storage)
 class StorageAdmin(admin.ModelAdmin):
     list_display = ('name', 'truncated_notes', 'items_count')

+ 12 - 9
hardware_provisioning/models.py

@@ -90,6 +90,14 @@ class Item(models.Model):
     is_available.boolean = True
     is_available.short_description = 'disponible'
 
+    def get_mac_and_serial(self):
+        mac = self.mac_address
+        serial = self.serial
+        if mac and serial:
+            return "{} / {}".format(mac, serial)
+        else:
+            return mac or serial or ''
+
     class Meta:
         verbose_name = 'objet'
 
@@ -128,15 +136,10 @@ class Loan(models.Model):
         return 'prêt de {item} à {user}'.format(
             item=self.item, user=self.user)
 
-    def get_mac_or_serial(self):
-        mac = self.item.mac_address
-        serial = self.item.serial
-        if mac and serial:
-            return "{} / {}".format(mac, serial)
-        else:
-            return mac or serial or ''
-
-    get_mac_or_serial.short_description = "Adresse MAC / n° de série"
+    def get_mac_and_serial(self):
+        return self.item.get_mac_and_serial()
+    
+    get_mac_and_serial.short_description = "Adresse MAC / n° de série"
 
     def user_can_close(self, user):
         return (not self.item.is_available()) and (self.user == user)