Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

application.py 3.2KB

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