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

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