Browse Source

Fix hardware availability selector in admin

- fix available queryset method
- add queryset methods to get borrowed/unavailable/deployed hardware
- offer those filtering options in admin
Jocelyn Delalande 7 years ago
parent
commit
d4392cb9f7
3 changed files with 65 additions and 12 deletions
  1. 6 0
      hardware_provisioning/admin.py
  2. 13 1
      hardware_provisioning/models.py
  3. 46 11
      hardware_provisioning/tests.py

+ 6 - 0
hardware_provisioning/admin.py

@@ -42,14 +42,20 @@ class AvailabilityFilter(admin.SimpleListFilter):
     def lookups(self, request, model_admin):
         return [
             ('available', 'Disponible'),
+            ('unavailable', 'Indispo (emprunté ou déployé)'),
             ('borrowed', 'Emprunté'),
+            ('deployed', 'Déployé'),
         ]
 
     def queryset(self, request, queryset):
         if self.value() == 'available':
             return queryset.available()
+        elif self.value() == 'unavailable':
+            return queryset.unavailable()
         elif self.value() == 'borrowed':
             return queryset.borrowed()
+        elif self.value() == 'deployed':
+            return queryset.deployed()
         else:
             return queryset
 

+ 13 - 1
hardware_provisioning/models.py

@@ -2,6 +2,7 @@
 
 from __future__ import unicode_literals
 from django.db import models
+from django.db.models import Q
 from django.conf import settings
 from django.utils import timezone
 
@@ -24,11 +25,22 @@ class ItemQuerySet(models.QuerySet):
         return Loan.objects.running().values_list('item', flat=True)
 
     def available(self):
-        return self.exclude(pk__in=self._get_borrowed_pks())
+        return self.exclude(
+            pk__in=self._get_borrowed_pks()).exclude(deployed=True)
 
     def borrowed(self):
         return self.filter(pk__in=self._get_borrowed_pks())
 
+    def deployed(self):
+        return self.filter(deployed=True)
+
+    def unavailable(self):
+        """ deployed or borrowed
+        """
+        return self.filter(
+            Q(pk__in=self._get_borrowed_pks()) |
+            Q(deployed=True))
+
 
 class Item(models.Model):
     type = models.ForeignKey(ItemType, verbose_name='type de matériel',

+ 46 - 11
hardware_provisioning/tests.py

@@ -14,20 +14,30 @@ def localize(naive_dt):
         return timezone.make_aware(naive_dt, timezone.get_current_timezone())
 
 
-class HardwareLoaningTestCase(TestCase):
+class HardwareModelsFactoryMixin:
+    def get_item_type(self, **kwargs):
+        params = {'name': 'Foos'}
+        params.update(**kwargs)
+        item_type, _ = ItemType.objects.get_or_create(**kwargs)
+        return item_type
+
+    def get_item(self, **kwargs):
+        params = {
+            'type': self.get_item_type(),
+            'designation': 'Test item',
+        }
+        params.update(**kwargs)
+        item, _ = Item.objects.get_or_create(**params)
+        return item
+
+
+class HardwareLoaningTestCase(HardwareModelsFactoryMixin, TestCase):
     def setUp(self):
-        self.member = Member.objects.create(
-            first_name='John',
-            last_name='Doe',
-            username='jdoe')
-        self.item_type = ItemType.objects.create(name='Foos')
-        self.item = Item.objects.create(
-            type=self.item_type,
-            designation='Bar Wheel',
-            buy_date=date(2012,12,5))
+        self.member = Member.objects.create(username='jdoe')
+        self.item = self.get_item()
 
     def test_running_(self):
-        loan_start_date = localize(datetime(2011,1,14,12,0,0))
+        loan_start_date = localize(datetime(2011, 1, 14, 12, 0, 0))
         loan = Loan.objects.create(
             item=self.item, user=self.member,
             loan_date=loan_start_date)
@@ -37,3 +47,28 @@ class HardwareLoaningTestCase(TestCase):
         loan.item.give_back()
         self.assertEqual(Loan.objects.running().count(), 0)
         self.assertEqual(Loan.objects.finished().count(), 1)
+
+
+class ItemTestCase(HardwareModelsFactoryMixin, TestCase):
+    def setUp(self):
+        self.member = Member.objects.create(username='jdoe')
+
+        self.free_item = self.get_item(designation='free')
+        self.deployed_item = self.get_item(
+            designation='deployed', deployed=True)
+        self.borrowed_item = self.get_item(designation='borrowed')
+
+    def test_queryset_methods(self):
+        self.assertEqual(Item.objects.borrowed().count(), 0)
+        self.assertEqual(Item.objects.deployed().count(), 1)
+        self.assertEqual(Item.objects.available().count(), 2)
+        self.assertEqual(Item.objects.unavailable().count(), 1)
+
+        Loan.objects.create(
+            item=self.borrowed_item, user=self.member,
+            loan_date=localize(datetime(2011, 1, 14, 12, 0, 0)))
+
+        self.assertEqual(Item.objects.borrowed().count(), 1)
+        self.assertEqual(Item.objects.deployed().count(), 1)
+        self.assertEqual(Item.objects.available().count(), 1)
+        self.assertEqual(Item.objects.unavailable().count(), 2)