views.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.shortcuts import render, redirect
  4. from django.contrib.auth.decorators import login_required
  5. from django.contrib import messages
  6. from django.http import HttpResponseForbidden
  7. from django.core.urlresolvers import reverse
  8. from .models import Item, Loan
  9. @login_required
  10. def loan_return(request, pk):
  11. loan = Loan.objects.get(pk=pk)
  12. if not loan.user_can_close(request.user):
  13. return HttpResponseForbidden('Non autorisé')
  14. if request.method == 'POST':
  15. messages.success(
  16. request,
  17. 'Le matériel {} a été marqué comme rendu'.format(
  18. loan.item))
  19. loan.item.give_back()
  20. return redirect(reverse('hardware_provisioning:loan-list'))
  21. else:
  22. return render(request, 'hardware_provisioning/return.html', {
  23. 'loan': loan,
  24. })
  25. @login_required
  26. def loan_list(request):
  27. view = 'old' if 'old' in request.GET else ''
  28. if view == 'old':
  29. loans = request.user.loans.finished().order_by('-loan_date_end')
  30. else:
  31. loans = request.user.loans.running()
  32. return render(request, 'hardware_provisioning/list.html', {
  33. 'loans': loans,
  34. 'view': view,
  35. })
  36. @login_required
  37. def loan_detail(request, pk):
  38. return render(request, 'hardware_provisioning/detail.html', {})