views.py 2.6 KB

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