views.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime
  4. from django.shortcuts import render, redirect
  5. from django.contrib.auth.decorators import login_required
  6. from django.contrib import messages
  7. from django.http import HttpResponseForbidden
  8. from django.core.urlresolvers import reverse
  9. from .forms import LoanTransferForm
  10. from .models import Item, Loan
  11. @login_required
  12. def loan_return(request, pk):
  13. loan = Loan.objects.get(pk=pk)
  14. if not loan.user_can_close(request.user):
  15. return HttpResponseForbidden('Non autorisé')
  16. if request.method == 'POST':
  17. messages.success(
  18. request,
  19. 'Le matériel {} a été marqué comme rendu'.format(
  20. loan.item))
  21. loan.item.give_back()
  22. return redirect(reverse('hardware_provisioning:loan-list'))
  23. else:
  24. return render(request, 'hardware_provisioning/return.html', {
  25. 'loan': loan,
  26. })
  27. @login_required
  28. def loan_transfer(request, pk):
  29. """ Transfer something loaned to another member
  30. """
  31. old_loan = get_object_or_404(Loan, pk=pk)
  32. if not old_loan.user_can_close(request.user):
  33. return HttpResponseForbidden()
  34. if request.method == 'POST':
  35. form = LoanTransferForm(request.POST)
  36. if form.is_valid():
  37. old_loan.item.give_back()
  38. Loan.objects.create(
  39. user=form.cleaned_data['target_user'],
  40. loan_date=datetime.date.today(),
  41. item=old_loan.item)
  42. messages.success(
  43. request,
  44. "Le matériel {} a été transféré à l'adhérent \"{}\"".format(
  45. old_loan.item,
  46. form.data['target_user']))
  47. return redirect(reverse('hardware_provisioning:loan-list'))
  48. else:
  49. form = LoanTransferForm()
  50. return render(request, 'hardware_provisioning/transfer.html', {
  51. 'form': form,
  52. 'loan': old_loan,
  53. })
  54. @login_required
  55. def loan_list(request):
  56. view = 'old' if 'old' in request.GET else ''
  57. if view == 'old':
  58. loans = request.user.loans.finished().order_by('-loan_date_end')
  59. else:
  60. loans = request.user.loans.running()
  61. return render(request, 'hardware_provisioning/list.html', {
  62. 'loans': loans,
  63. 'view': view,
  64. })
  65. @login_required
  66. def loan_detail(request, pk):
  67. return render(request, 'hardware_provisioning/detail.html', {})