Browse Source

feature model action : give back item from item list

Cecile 9 years ago
parent
commit
e9bf355470
2 changed files with 10 additions and 0 deletions
  1. 6 0
      hardware_provisioning/admin.py
  2. 4 0
      hardware_provisioning/models.py

+ 6 - 0
hardware_provisioning/admin.py

@@ -14,10 +14,16 @@ class ItemAdmin(admin.ModelAdmin):
                     'buy_date', 'is_available')
                     'buy_date', 'is_available')
     list_filter = ('type__name', 'buy_date')
     list_filter = ('type__name', 'buy_date')
     search_fields = ('designation', 'user_in_charge')
     search_fields = ('designation', 'user_in_charge')
+    actions = ['give_back']
 
 
+    def give_back(self, request, queryset):
+        for item in queryset.filter(loans__loan_date_end=None):
+            item.give_back()
+    give_back.short_description = 'Rendre le matériel'
 
 
 @admin.register(Loan)
 @admin.register(Loan)
 class LoanAdmin(admin.ModelAdmin):
 class LoanAdmin(admin.ModelAdmin):
     list_display = ('item', 'user', 'loan_date', 'loan_date_end', 'location')
     list_display = ('item', 'user', 'loan_date', 'loan_date_end', 'location')
     list_filter = ('item__designation', 'user__username')
     list_filter = ('item__designation', 'user__username')
     search_fields = ('item', 'user')
     search_fields = ('item', 'user')
+

+ 4 - 0
hardware_provisioning/models.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
 from __future__ import unicode_literals
 from __future__ import unicode_literals
+from datetime import date
 from django.db import models
 from django.db import models
 from django.conf import settings
 from django.conf import settings
 from .fields import MACAddressField
 from .fields import MACAddressField
@@ -56,6 +57,9 @@ class Item(models.Model):
     class Meta:
     class Meta:
         verbose_name = 'objet'
         verbose_name = 'objet'
 
 
+    def give_back(self):
+        self.loans.filter(loan_date_end=None).update(loan_date_end=date.today())
+
 
 
 class Loan(models.Model):
 class Loan(models.Model):
     item = models.ForeignKey(Item, verbose_name='objet', related_name='loans')
     item = models.ForeignKey(Item, verbose_name='objet', related_name='loans')