Parcourir la source

Merge branch 'feat-list-loan-in-MemberAdmin-view' of daimrod/coin into master

jocelyn il y a 7 ans
Parent
commit
b2c47517a6
2 fichiers modifiés avec 29 ajouts et 0 suppressions
  1. 24 0
      hardware_provisioning/admin.py
  2. 5 0
      hardware_provisioning/models.py

+ 24 - 0
hardware_provisioning/admin.py

@@ -8,6 +8,7 @@ from django.contrib.auth import get_user_model
 from django.utils import timezone
 
 from .models import ItemType, Item, Loan, Storage
+from coin.members.admin import MemberAdmin
 
 
 User = get_user_model()
@@ -146,3 +147,26 @@ class StorageAdmin(admin.ModelAdmin):
         else:
             return obj.notes
     truncated_notes.short_description = 'notes'
+
+class LoanInline(admin.TabularInline):
+    model = Loan
+    extra = 0
+    exclude = ('notes',)
+    readonly_fields = ('item', 'get_mac_or_serial', 'loan_date', 'loan_date_end', 'is_running')
+
+    show_change_link = True
+
+    def get_queryset(self, request):
+        qs = super(LoanInline, self).get_queryset(request)
+        return qs.order_by('-loan_date_end')
+
+    def has_add_permission(self, request, obj=None):
+        return False
+
+    def has_delete_permission(self, request, obj=None):
+        return False
+
+# Avoid to add LoanInline twice in case the file is loaded more than
+# once.
+if LoanInline not in MemberAdmin.inlines:
+    MemberAdmin.inlines.append(LoanInline)

+ 5 - 0
hardware_provisioning/models.py

@@ -131,6 +131,11 @@ class Loan(models.Model):
     def user_can_close(self, user):
         return (not self.item.is_available()) and (self.user == user)
 
+    def is_running(self):
+        return not self.loan_date_end or self.loan_date_end > timezone.now()
+    is_running.boolean = True
+    is_running.short_description = 'En cours ?'
+
     class Meta:
         verbose_name = 'prêt d’objet'
         verbose_name_plural = 'prêts d’objets'