Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

decorators.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from django.http import HttpResponseRedirect
  2. from django.contrib import messages
  3. from django.core.urlresolvers import reverse
  4. from django.utils.translation import ugettext_lazy as _
  5. from oscar.core.loading import get_class
  6. ProgressChecker = get_class('checkout.utils', 'ProgressChecker')
  7. def prev_steps_must_be_complete(view_fn):
  8. """
  9. Decorator for checking that previous steps of the checkout
  10. are complete.
  11. The completed steps (identified by URL-names) are stored in the session.
  12. If this fails, then we redirect to the next uncompleted step.
  13. """
  14. def _view_wrapper(self, request, *args, **kwargs):
  15. checker = ProgressChecker()
  16. if not checker.are_previous_steps_complete(request):
  17. messages.error(request, _("You must complete this step of the checkout first"))
  18. url_name = checker.get_next_step(request)
  19. return HttpResponseRedirect(reverse(url_name))
  20. return view_fn(self, request, *args, **kwargs)
  21. return _view_wrapper
  22. def basket_required(view_fn):
  23. """
  24. Decorator for checking that the user has a non-empty basket
  25. or has a frozen one in the session
  26. """
  27. def _view_wrapper(self, request, *args, **kwargs):
  28. if request.basket.is_empty and not 'checkout_basket_id' in request.session:
  29. messages.error(request, _("You must add some products to your basket before checking out"))
  30. return HttpResponseRedirect(reverse('oscar-basket'))
  31. return view_fn(self, request, *args, **kwargs)
  32. return _view_wrapper