Browse Source

Allow to show old/current loans in loans list

Jocelyn Delande 9 years ago
parent
commit
012a92c7ae

+ 7 - 1
coin/static/css/local.css

@@ -394,4 +394,10 @@ tr.inactive {
     overflow: hidden;
     overflow: hidden;
     text-overflow: ellipsis;
     text-overflow: ellipsis;
     white-space: nowrap;
     white-space: nowrap;
-}
+}
+
+/* List filters links */
+
+.list-filter {
+	text-align: right;
+}

+ 14 - 0
hardware_provisioning/models.py

@@ -61,6 +61,18 @@ class Item(models.Model):
         self.loans.filter(loan_date_end=None).update(loan_date_end=date.today())
         self.loans.filter(loan_date_end=None).update(loan_date_end=date.today())
 
 
 
 
+class LoanQuerySet(models.QuerySet):
+    running_filter = (
+        models.Q(loan_date_end__gt=date.today()) |
+        models.Q(loan_date_end__isnull=True))
+
+    def running(self):
+        return self.filter(self.running_filter)
+
+    def finished(self):
+        return self.exclude(self.running_filter)
+
+
 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')
     user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='membre',
     user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='membre',
@@ -78,3 +90,5 @@ class Loan(models.Model):
     class Meta:
     class Meta:
         verbose_name = 'prêt d’objet'
         verbose_name = 'prêt d’objet'
         verbose_name_plural = 'prêts d’objets'
         verbose_name_plural = 'prêts d’objets'
+
+    objects = LoanQuerySet().as_manager()

+ 13 - 0
hardware_provisioning/templates/hardware_provisioning/list.html

@@ -1,7 +1,11 @@
 {% extends "base.html" %}
 {% extends "base.html" %}
 
 
 {% block content %}
 {% block content %}
+{% if view  == 'old' %}
+<h2>L'association m'a prêté…</h2>
+{% else %}
 <h2>L'association me prête…</h2>
 <h2>L'association me prête…</h2>
+{% endif %}
 
 
 <table id="member_loans" class="full-width">
 <table id="member_loans" class="full-width">
     <thead>
     <thead>
@@ -9,6 +13,7 @@
             <th>Type de matériel</th>
             <th>Type de matériel</th>
             <th>Matériel prêté</th>
             <th>Matériel prêté</th>
             <th>Date de prêt</th>
             <th>Date de prêt</th>
+            {% if view == 'old' %}<th>Date retour</th>{% endif %}
             <th>Addr. MAC</th>
             <th>Addr. MAC</th>
             <th>Num. de série</th>
             <th>Num. de série</th>
         </tr>
         </tr>
@@ -19,6 +24,7 @@
             <td>{{ loan.item.type }}</td>
             <td>{{ loan.item.type }}</td>
             <td>{{ loan.item }}</a></td>
             <td>{{ loan.item }}</a></td>
             <td>{{ loan.loan_date }}</td>
             <td>{{ loan.loan_date }}</td>
+            {% if view == 'old' %}<td>{{ loan.loan_date_end }}</td>{% endif %}
             <td>{{ loan.item.mac_address|default:"n/a" }}</td>
             <td>{{ loan.item.mac_address|default:"n/a" }}</td>
             <td>{{ loan.item.serial|default:"n/a" }}</td>
             <td>{{ loan.item.serial|default:"n/a" }}</td>
         </tr>
         </tr>
@@ -26,4 +32,11 @@
     </tbody>
     </tbody>
 </table>
 </table>
 
 
+<p class="list-filter">
+    {% if view == 'old' %}
+    <a href="./">Afficher les prêts en cours</a>
+    {% else %}
+    <a href="?old">Afficher l'historique de prêt</a>
+    {% endif %}
+</p>
 {% endblock %}
 {% endblock %}

+ 11 - 2
hardware_provisioning/views.py

@@ -8,8 +8,17 @@ from .models import Item, Loan
 
 
 @login_required
 @login_required
 def loan_list(request):
 def loan_list(request):
-    loans = request.user.loans.all()
-    return render(request, 'hardware_provisioning/list.html', {'loans': loans})
+    view = 'old' if 'old' in request.GET else ''
+
+    if view == 'old':
+        loans = request.user.loans.finished().order_by('-loan_date_end')
+    else:
+        loans = request.user.loans.running()
+
+    return render(request, 'hardware_provisioning/list.html', {
+        'loans': loans,
+        'view': view,
+    })
 
 
 
 
 @login_required
 @login_required