Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

loading.py 3.3KB

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