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.

loading.py 3.1KB

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