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.

application.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import six
  2. from django.conf.urls import patterns
  3. from oscar.core.loading import feature_hidden
  4. from oscar.views.decorators import permissions_required
  5. class Application(object):
  6. name = None
  7. hidable_feature_name = None
  8. #: Maps view names to a tuple or list of permissions
  9. permissions_map = {}
  10. #: Default permission for any view not in permissions_map
  11. default_permissions = None
  12. def __init__(self, app_name=None, **kwargs):
  13. self.app_name = app_name
  14. # Set all kwargs as object attributes
  15. for key, value in six.iteritems(kwargs):
  16. setattr(self, key, value)
  17. def get_urls(self):
  18. """
  19. Return the url patterns for this app, MUST be implemented in the
  20. subclass
  21. """
  22. return patterns('')
  23. def post_process_urls(self, urlpatterns):
  24. """
  25. Customise URL patterns.
  26. By default, this only allows custom decorators to be specified, but you
  27. could override this method to do anything you want.
  28. """
  29. # Test if this the URLs in the Application instance should be
  30. # available. If the feature is hidden then we don't include the URLs.
  31. if feature_hidden(self.hidable_feature_name):
  32. return patterns('')
  33. for pattern in urlpatterns:
  34. if hasattr(pattern, 'url_patterns'):
  35. self.post_process_urls(pattern.url_patterns)
  36. if not hasattr(pattern, '_callback'):
  37. continue
  38. # Look for a custom decorator
  39. decorator = self.get_url_decorator(pattern)
  40. if decorator:
  41. # Nasty way of modifying a RegexURLPattern
  42. pattern._callback = decorator(pattern._callback)
  43. return urlpatterns
  44. def get_permissions(self, url):
  45. # url namespaced?
  46. if url is not None and ':' in url:
  47. view_name = url.split(':')[1]
  48. else:
  49. view_name = url
  50. return self.permissions_map.get(view_name, self.default_permissions)
  51. def get_url_decorator(self, pattern):
  52. """
  53. Return the appropriate decorator for the view function with the passed
  54. URL name. Mainly used for access-protecting views.
  55. It's possible to specify
  56. - no permissions necessary: use None
  57. - a set of permissions: use a list
  58. - two set of permissions (`or`): use a two-tuple of lists
  59. See permissions_required decorator for details
  60. """
  61. permissions = self.get_permissions(pattern.name)
  62. return permissions_required(permissions)
  63. @property
  64. def urls(self):
  65. # We set the application and instance namespace here
  66. return self.get_urls(), self.app_name, self.name