|
@@ -4,17 +4,61 @@ from __future__ import unicode_literals
|
|
|
|
|
|
import datetime
|
|
import datetime
|
|
|
|
|
|
-from django.shortcuts import render, redirect
|
|
|
|
|
|
+from django.shortcuts import get_object_or_404, render, redirect
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.contrib import messages
|
|
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, LoanReturnForm
|
|
|
|
|
|
+from .forms import LoanDeclareForm, LoanTransferForm, LoanReturnForm
|
|
from .models import Item, Loan
|
|
from .models import Item, Loan
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
@login_required
|
|
|
|
+def item_list(request):
|
|
|
|
+ items = Item.objects.all().order_by('storage', 'type', 'designation')
|
|
|
|
+
|
|
|
|
+ # FIXME: suboptimal
|
|
|
|
+ items = [i for i in items.filter() if i.is_available()]
|
|
|
|
+ return render(request, 'hardware_provisioning/item_list.html', {
|
|
|
|
+ 'items': items,
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@login_required
|
|
|
|
+def item_borrow(request, pk):
|
|
|
|
+ item = get_object_or_404(Item, pk=pk)
|
|
|
|
+
|
|
|
|
+ if not item.is_available():
|
|
|
|
+ return HttpResponseForbidden('Item non disponible')
|
|
|
|
+
|
|
|
|
+ if request.method == 'POST':
|
|
|
|
+ form = LoanDeclareForm(request.POST)
|
|
|
|
+ if form.is_valid():
|
|
|
|
+ loan = Loan.objects.create(
|
|
|
|
+ item=item,
|
|
|
|
+ loan_date=datetime.date.today(),
|
|
|
|
+ loan_date_end=form.cleaned_data['loan_date_end'],
|
|
|
|
+ user=request.user,
|
|
|
|
+ )
|
|
|
|
+ messages.success(
|
|
|
|
+ request, "Emprunt de {} ({}) enregistré".format(
|
|
|
|
+ item.designation, item.type))
|
|
|
|
+ if not loan.loan_date_end:
|
|
|
|
+ messages.warning(
|
|
|
|
+ request,
|
|
|
|
+ "N'oubliez pas de notifier le retour de l'objet le temps venu")
|
|
|
|
+ return redirect(reverse('hardware_provisioning:loan-list'))
|
|
|
|
+ else:
|
|
|
|
+ form = LoanDeclareForm()
|
|
|
|
+
|
|
|
|
+ return render(request, 'hardware_provisioning/item_borrow.html', {
|
|
|
|
+ 'item': item,
|
|
|
|
+ 'form': form,
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@login_required
|
|
def loan_return(request, pk):
|
|
def loan_return(request, pk):
|
|
loan = get_object_or_404(Loan, pk=pk)
|
|
loan = get_object_or_404(Loan, pk=pk)
|
|
|
|
|