Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

loading.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from imp import new_module
  2. from django.conf import settings
  3. class AppNotFoundError(Exception):
  4. pass
  5. def get_class(module_label, classname):
  6. return get_classes(module_label, [classname])[0]
  7. def get_classes(module_label, classnames):
  8. """
  9. For dynamically importing classes from a module.
  10. Eg. calling get_classes('catalogue.models', ['Product']) will search
  11. INSTALLED_APPS for the relevant product app (default is
  12. 'oscar.apps.catalogue') and then import the classes from there. If the
  13. class can't be found in the overriding module, then we attempt to import it
  14. from within oscar.
  15. This is very similar to django.db.models.get_model although that is only
  16. for loading models while this method will load any class.
  17. """
  18. app_module_path = _get_app_module_path(module_label)
  19. if not app_module_path:
  20. raise AppNotFoundError("No app found matching '%s'" % module_label)
  21. # Check if app is in oscar
  22. if app_module_path.split('.')[0] == 'oscar':
  23. # Using core oscar class
  24. module_path = 'oscar.apps.%s' % module_label
  25. imported_module = __import__(module_path, fromlist=classnames)
  26. return _pluck_classes([imported_module], classnames)
  27. # App must be local - check if module is in local app (it could be in
  28. # oscar's)
  29. base_package = app_module_path.split(".")[0]
  30. local_app = "%s.%s" % (base_package, module_label)
  31. try:
  32. imported_local_module = __import__(local_app, fromlist=classnames)
  33. except ImportError:
  34. # Module not in local app
  35. imported_local_module = {}
  36. oscar_app = "oscar.apps.%s" % module_label
  37. imported_oscar_module = __import__(oscar_app, fromlist=classnames)
  38. return _pluck_classes([imported_local_module, imported_oscar_module], classnames)
  39. def _pluck_classes(modules, classnames):
  40. klasses = []
  41. for classname in classnames:
  42. for module in modules:
  43. if hasattr(module, classname):
  44. klasses.append(getattr(module, classname))
  45. break
  46. return klasses
  47. def _get_app_module_path(module_label):
  48. app_name = module_label.rsplit(".", 1)[0]
  49. for installed_app in settings.INSTALLED_APPS:
  50. if installed_app.endswith(app_name):
  51. return installed_app
  52. return None
  53. def import_module(module_label, classes, namespace=None):
  54. """
  55. This is the deprecated old way of loading modules
  56. """
  57. klasses = get_classes(module_label, classes)
  58. if namespace:
  59. for classname, klass in zip(classes, klasses):
  60. namespace[classname] = klass
  61. else:
  62. module = new_module("oscar.apps.%s" % module_label)
  63. for classname, klass in zip(classes, klasses):
  64. setattr(module, classname, klass)
  65. return module