Browse Source

Add transfer button/action to loans list

Jocelyn Delande 9 years ago
parent
commit
5f65ad8beb

+ 33 - 0
hardware_provisioning/forms.py

@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+
+from django.core.exceptions import ValidationError
+from django.contrib.auth import get_user_model
+from django.db.models import Q
+from django import forms
+
+User = get_user_model()
+
+
+class LoanTransferForm(forms.Form):
+    target_user = forms.CharField(
+        max_length=100,
+        label='Adhérent',
+        help_text='email, pseudonyme ou numéro de l\'adhérent',
+    )
+
+    def clean_target_user(self):
+        value = self.cleaned_data['target_user']
+        result = User.objects.filter(
+            Q(email__iexact=value)
+            | Q(pk__iexact=value)
+            | Q(nickname__iexact=value)
+            | Q(username__iexact=value)
+        )
+        if result.count() > 1:
+            raise ValidationError(
+                "La recherche retourne plus d'un adhérent")
+        elif result.count() < 1:
+            raise ValidationError(
+                "Aucun adhérent ne correspond à cette recherche")
+
+        return result.first()

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

@@ -37,6 +37,9 @@
                   <a href="{% url 'hardware_provisioning:loan-return' pk=loan.pk %}" class="small button">
                      <i class="fa fa-times"></i>&nbsp;Rendre
                  </a>
+                <a href="{% url 'hardware_provisioning:loan-transfer' pk=loan.pk %}" class="small button">
+                   <i class="fa fa-exchange"></i> Transférer
+                </a>
               </div>
             </td>
             {% endif %}

+ 18 - 0
hardware_provisioning/templates/hardware_provisioning/transfer.html

@@ -0,0 +1,18 @@
+{% extends "base.html" %}
+
+{% block content %}
+<h2>Transfert de matériel</h2>
+<p>
+    J'ai passé aujourd'hui le matériel <strong>{{  loan.item }}
+    ({{ loan.item.type }}</strong>) a un autre adhérent :
+</p>
+<form method="post">{% csrf_token %}
+  <p>
+    <input id="id_target_user"
+           name="target_user"
+           type="text" placeholder="{{ form.target_user.help_text }}"/>
+    {{ form.target_user.errors }}
+  <input class="success button" type="submit"
+         value="Oui oui, c'est bien ça."/>
+</form>
+{% endblock %}

+ 1 - 0
hardware_provisioning/urls.py

@@ -8,5 +8,6 @@ from . import views
 urlpatterns = [
     url(r'^$', views.loan_list, name='loan-list'),
     url(r'^(?P<pk>[0-9]+)/return$', views.loan_return, name='loan-return'),
+    url(r'^(?P<pk>[0-9]+)/transfer$', views.loan_transfer, name='loan-transfer'),
     url(r'^(?P<pk>[0-9]+)$', views.loan_detail, name='loan-detail'),
 ]

+ 36 - 1
hardware_provisioning/views.py

@@ -2,13 +2,15 @@
 
 from __future__ import unicode_literals
 
+import datetime
+
 from django.shortcuts import render, redirect
 from django.contrib.auth.decorators import login_required
 from django.contrib import messages
 from django.http import HttpResponseForbidden
 from django.core.urlresolvers import reverse
 
-
+from .forms import LoanTransferForm
 from .models import Item, Loan
 
 
@@ -34,6 +36,39 @@ def loan_return(request, pk):
 
 
 @login_required
+def loan_transfer(request, pk):
+    """ Transfer something loaned to another member
+    """
+    old_loan = get_object_or_404(Loan, pk=pk)
+
+    if not old_loan.user_can_close(request.user):
+        return HttpResponseForbidden()
+
+    if request.method == 'POST':
+        form = LoanTransferForm(request.POST)
+        if form.is_valid():
+            old_loan.item.give_back()
+            Loan.objects.create(
+                user=form.cleaned_data['target_user'],
+                loan_date=datetime.date.today(),
+                item=old_loan.item)
+            messages.success(
+                request,
+                "Le matériel {} a été transféré à l'adhérent \"{}\"".format(
+                    old_loan.item,
+                    form.data['target_user']))
+            return redirect(reverse('hardware_provisioning:loan-list'))
+
+    else:
+        form = LoanTransferForm()
+
+    return render(request, 'hardware_provisioning/transfer.html', {
+        'form': form,
+        'loan': old_loan,
+    })
+
+
+@login_required
 def loan_list(request):
     view = 'old' if 'old' in request.GET else ''