views.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.shortcuts import get_object_or_404, 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 django.utils import timezone
  9. from .forms import LoanDeclareForm, LoanTransferForm, LoanReturnForm
  10. from .models import Item, Loan
  11. @login_required
  12. def item_list(request):
  13. items = Item.objects.all().order_by('storage', 'type', 'designation')
  14. # FIXME: suboptimal
  15. items = [i for i in items.filter() if i.is_available()]
  16. return render(request, 'hardware_provisioning/item_list.html', {
  17. 'items': items,
  18. })
  19. @login_required
  20. def item_borrow(request, pk):
  21. item = get_object_or_404(Item, pk=pk)
  22. if not item.is_available():
  23. return HttpResponseForbidden('Item non disponible')
  24. if request.method == 'POST':
  25. form = LoanDeclareForm(request.POST)
  26. if form.is_valid():
  27. loan = Loan.objects.create(
  28. item=item,
  29. loan_date=timezone.now(),
  30. loan_date_end=form.cleaned_data['loan_date_end'],
  31. user=request.user,
  32. )
  33. messages.success(
  34. request, "Emprunt de {} ({}) enregistré".format(
  35. item.designation, item.type))
  36. if not loan.loan_date_end:
  37. messages.warning(
  38. request,
  39. "N'oubliez pas de notifier le retour de l'objet le temps venu")
  40. return redirect(reverse('hardware_provisioning:loan-list'))
  41. else:
  42. form = LoanDeclareForm()
  43. return render(request, 'hardware_provisioning/item_borrow.html', {
  44. 'item': item,
  45. 'form': form,
  46. })
  47. @login_required
  48. def loan_return(request, pk):
  49. loan = get_object_or_404(Loan, pk=pk)
  50. if not loan.user_can_close(request.user):
  51. return HttpResponseForbidden('Non autorisé')
  52. if request.method == 'POST':
  53. form = LoanReturnForm(request.POST)
  54. if form.is_valid():
  55. messages.success(
  56. request,
  57. 'Le matériel {} a été marqué comme rendu'.format(
  58. loan.item))
  59. loan.item.give_back(form.cleaned_data['storage'])
  60. return redirect(reverse('hardware_provisioning:loan-list'))
  61. else:
  62. form = LoanReturnForm()
  63. return render(request, 'hardware_provisioning/return.html', {
  64. 'loan': loan,
  65. 'form': form,
  66. })
  67. @login_required
  68. def loan_transfer(request, pk):
  69. """ Transfer something loaned to another member
  70. """
  71. old_loan = get_object_or_404(Loan, pk=pk)
  72. if not old_loan.user_can_close(request.user):
  73. return HttpResponseForbidden()
  74. if request.method == 'POST':
  75. form = LoanTransferForm(request.POST)
  76. if form.is_valid():
  77. old_loan.item.give_back()
  78. Loan.objects.create(
  79. user=form.cleaned_data['target_user'],
  80. loan_date=timezone.now(),
  81. item=old_loan.item)
  82. messages.success(
  83. request,
  84. "Le matériel {} a été transféré à l'adhérent \"{}\"".format(
  85. old_loan.item,
  86. form.data['target_user']))
  87. return redirect(reverse('hardware_provisioning:loan-list'))
  88. else:
  89. form = LoanTransferForm()
  90. return render(request, 'hardware_provisioning/transfer.html', {
  91. 'form': form,
  92. 'loan': old_loan,
  93. })
  94. @login_required
  95. def loan_list(request):
  96. view = 'old' if 'old' in request.GET else ''
  97. if view == 'old':
  98. loans = request.user.loans.finished().order_by('-loan_date_end')
  99. else:
  100. loans = request.user.loans.running()
  101. return render(request, 'hardware_provisioning/list.html', {
  102. 'loans': loans,
  103. 'view': view,
  104. })
  105. @login_required
  106. def loan_detail(request, pk):
  107. return render(request, 'hardware_provisioning/detail.html', {})