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

services.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. installed_app_parts = installed_app.split(".")
  28. try:
  29. # We search the second component of the installed apps
  30. if app_name == installed_app_parts[1]:
  31. if installed_app_parts[0] == 'oscar':
  32. # Using core module explicitly
  33. return _import_classes_from_module("oscar.%s" % module_label, classes)
  34. else:
  35. # Using local override - check that requested module exists
  36. local_module_name = "%s.%s" % (installed_app_parts[0], module_label)
  37. try:
  38. imported_local_mod = __import__(local_module_name, fromlist=classes)
  39. except ImportError:
  40. # Module doesn't exist, fall back to oscar core
  41. return _import_classes_from_module("oscar.%s" % module_label, classes)
  42. module = new_module(local_module_name)
  43. imported_oscar_mod = __import__("oscar.%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):
  54. module = new_module(module_name)
  55. imported_module = __import__(module_name, fromlist=classes)
  56. for classname in classes:
  57. module.__setattr__(classname, getattr(imported_module, classname))
  58. return module