You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decorators.py 1.5KB

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