Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

application.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. """
  6. Base application class.
  7. This is subclassed by each app to provide a customisable container for an
  8. app's views and permissions.
  9. """
  10. #: Namespace name
  11. name = None
  12. #: A name that allows the functionality within this app to be disabled
  13. hidable_feature_name = None
  14. #: Maps view names to a tuple or list of permissions
  15. permissions_map = {}
  16. #: Default permission for any view not in permissions_map
  17. default_permissions = None
  18. def __init__(self, app_name=None, **kwargs):
  19. self.app_name = app_name
  20. # Set all kwargs as object attributes
  21. for key, value in kwargs.iteritems():
  22. setattr(self, key, value)
  23. def get_urls(self):
  24. """
  25. Return the url patterns for this app.
  26. """
  27. return patterns('')
  28. def post_process_urls(self, urlpatterns):
  29. """
  30. Customise URL patterns.
  31. This method allows decorators to be wrapped around an apps URL
  32. patterns.
  33. By default, this only allows custom decorators to be specified, but you
  34. could override this method to do anything you want.
  35. Args:
  36. urlpatterns (list): A list of URL patterns
  37. """
  38. # Test if this the URLs in the Application instance should be
  39. # available. If the feature is hidden then we don't include the URLs.
  40. if feature_hidden(self.hidable_feature_name):
  41. return patterns('')
  42. for pattern in urlpatterns:
  43. if hasattr(pattern, 'url_patterns'):
  44. self.post_process_urls(pattern.url_patterns)
  45. if not hasattr(pattern, '_callback'):
  46. continue
  47. # Look for a custom decorator
  48. decorator = self.get_url_decorator(pattern)
  49. if decorator:
  50. # Nasty way of modifying a RegexURLPattern
  51. pattern._callback = decorator(pattern._callback)
  52. return urlpatterns
  53. def get_permissions(self, url):
  54. """
  55. Return a list of permissions for a given URL name
  56. Args:
  57. url (str): A URL name (eg ``basket.basket``)
  58. Returns:
  59. list: A list of permission strings.
  60. """
  61. # url namespaced?
  62. if url is not None and ':' in url:
  63. view_name = url.split(':')[1]
  64. else:
  65. view_name = url
  66. return self.permissions_map.get(view_name, self.default_permissions)
  67. def get_url_decorator(self, pattern):
  68. """
  69. Return the appropriate decorator for the view function with the passed
  70. URL name. Mainly used for access-protecting views.
  71. It's possible to specify:
  72. - no permissions necessary: use None
  73. - a set of permissions: use a list
  74. - two set of permissions (`or`): use a two-tuple of lists
  75. See permissions_required decorator for details
  76. """
  77. permissions = self.get_permissions(pattern.name)
  78. return permissions_required(permissions)
  79. @property
  80. def urls(self):
  81. # We set the application and instance namespace here
  82. return self.get_urls(), self.app_name, self.name