您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

loading.py 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from imp import new_module
  2. from django.conf import settings
  3. class AppNotFoundError(Exception):
  4. pass
  5. def import_module(module_label, classes, namespace=None):
  6. u"""
  7. For dynamically importing classes from a module.
  8. Eg. calling import_module('product.models') will search INSTALLED_APPS for
  9. the relevant product app (default is 'oscar.product') and then import the
  10. classes from there. If the class can't be found in the overriding module,
  11. then we attempt to import it from within oscar.
  12. We search the INSTALLED_APPS list to find the appropriate app string and
  13. import that.
  14. This is very similar to django.db.models.get_model although that is only
  15. for loading models while this method will load any class.
  16. """
  17. # Classes must be specified in order for __import__ to work correctly. It's
  18. # also a good practice
  19. if not classes:
  20. raise ValueError("You must specify the classes to import")
  21. # Arguments will be things like 'product.models' and so we
  22. # we take the first component to find within the INSTALLED_APPS list.
  23. app_name = module_label.rsplit(".", 1)[0]
  24. for installed_app in settings.INSTALLED_APPS:
  25. base_package = installed_app.split(".")[0]
  26. module_name = installed_app.split(".", 2).pop() # strip oscar.apps
  27. try:
  28. # We search the second component of the installed apps
  29. if app_name == module_name:
  30. if base_package == 'oscar':
  31. # Using core module explicitly
  32. return _import_classes_from_module("oscar.apps.%s" % module_label, classes, namespace)
  33. else:
  34. # Using local override - check that requested module exists
  35. local_app = "%s.%s" % (base_package, module_label)
  36. try:
  37. imported_local_mod = __import__(local_app, fromlist=classes)
  38. except ImportError, e:
  39. # Module doesn't exist, fall back to oscar core. This can be tricky
  40. # as if the overriding module has an import error, it will get picked up
  41. # here.
  42. if str(e).startswith("No module named"):
  43. return _import_classes_from_module("oscar.apps.%s" % module_label, classes, namespace)
  44. raise e
  45. # Found overriding module, merging custom classes with core
  46. module = new_module(local_app)
  47. imported_oscar_mod = __import__("oscar.apps.%s" % module_label, fromlist=classes)
  48. for classname in classes:
  49. if hasattr(imported_local_mod, classname):
  50. if namespace:
  51. namespace[classname] = getattr(imported_local_mod, classname)
  52. else:
  53. module.__setattr__(classname, getattr(imported_local_mod, classname))
  54. else:
  55. if namespace:
  56. namespace[classname] = getattr(imported_oscar_mod, classname)
  57. else:
  58. module.__setattr__(classname, getattr(imported_oscar_mod, classname))
  59. return module
  60. except IndexError:
  61. pass
  62. raise AppNotFoundError("Unable to find an app matching %s in INSTALLED_APPS" % (app_name,))
  63. def _import_classes_from_module(module_name, classes, namespace):
  64. imported_module = __import__(module_name, fromlist=classes)
  65. if namespace:
  66. for classname in classes:
  67. namespace[classname] = getattr(imported_module, classname)
  68. return
  69. module = new_module(module_name)
  70. for classname in classes:
  71. setattr(module, classname, getattr(imported_module, classname))
  72. return module