Browse Source

Allow members to return items to a storage

Jocelyn Delande 9 years ago
parent
commit
eded3baad9

+ 9 - 0
hardware_provisioning/forms.py

@@ -5,9 +5,18 @@ from django.contrib.auth import get_user_model
 from django.db.models import Q
 from django.db.models import Q
 from django import forms
 from django import forms
 
 
+from .models import Storage
+
 User = get_user_model()
 User = get_user_model()
 
 
 
 
+class LoanReturnForm(forms.Form):
+    storage = forms.ModelChoiceField(
+        label='Dans quel lieu de stockage ai-je remis le matériel ?',
+        required=False,
+        queryset=Storage.objects.all(), empty_label='Je ne sais pas')
+
+
 class LoanTransferForm(forms.Form):
 class LoanTransferForm(forms.Form):
     target_user = forms.CharField(
     target_user = forms.CharField(
         max_length=100,
         max_length=100,

+ 5 - 2
hardware_provisioning/models.py

@@ -71,8 +71,11 @@ 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())
+    def give_back(self, storage=None):
+        self.storage = storage
+        self.save()
+        self.loans.filter(loan_date_end=None).update(
+            loan_date_end=date.today())
 
 
 
 
 class LoanQuerySet(models.QuerySet):
 class LoanQuerySet(models.QuerySet):

+ 2 - 1
hardware_provisioning/templates/hardware_provisioning/return.html

@@ -3,9 +3,10 @@
 {% block content %}
 {% block content %}
 <h2>Retour de matériel</h2>
 <h2>Retour de matériel</h2>
 <p>
 <p>
-    J'ai retourné le matériel <strong>{{loan.item }} ({{ loan.item.type }}</strong>) a l'association ce jour.
+    J'ai rendu le matériel <strong>{{loan.item }} ({{ loan.item.type }}</strong>) a l'association ce jour.
 </p>
 </p>
 <form method='post'>{% csrf_token %}
 <form method='post'>{% csrf_token %}
+  {{ form.as_p }}
   <input class="success button" type="submit"
   <input class="success button" type="submit"
          value="Oui oui, c'est bien ça."/>
          value="Oui oui, c'est bien ça."/>
 </form>
 </form>

+ 16 - 12
hardware_provisioning/views.py

@@ -10,29 +10,33 @@ from django.contrib import messages
 from django.http import HttpResponseForbidden
 from django.http import HttpResponseForbidden
 from django.core.urlresolvers import reverse
 from django.core.urlresolvers import reverse
 
 
-from .forms import LoanTransferForm
+from .forms import LoanTransferForm, LoanReturnForm
 from .models import Item, Loan
 from .models import Item, Loan
 
 
 
 
 @login_required
 @login_required
 def loan_return(request, pk):
 def loan_return(request, pk):
-    loan = Loan.objects.get(pk=pk)
+    loan = get_object_or_404(Loan, pk=pk)
 
 
     if not loan.user_can_close(request.user):
     if not loan.user_can_close(request.user):
         return HttpResponseForbidden('Non autorisé')
         return HttpResponseForbidden('Non autorisé')
 
 
     if request.method == 'POST':
     if request.method == 'POST':
-        messages.success(
-            request,
-            'Le matériel {} a été marqué comme rendu'.format(
-                loan.item))
-        loan.item.give_back()
-        return redirect(reverse('hardware_provisioning:loan-list'))
-
+        form = LoanReturnForm(request.POST)
+        if form.is_valid():
+            messages.success(
+                request,
+                'Le matériel {} a été marqué comme rendu'.format(
+                    loan.item))
+            loan.item.give_back(form.cleaned_data['storage'])
+            return redirect(reverse('hardware_provisioning:loan-list'))
     else:
     else:
-        return render(request, 'hardware_provisioning/return.html', {
-            'loan': loan,
-        })
+        form = LoanReturnForm()
+
+    return render(request, 'hardware_provisioning/return.html', {
+        'loan': loan,
+        'form': form,
+    })
 
 
 
 
 @login_required
 @login_required