auction.utils.loader: 53 total statements, 32.0% covered

Generated: Mon 2013-04-29 18:13 CST

Source file: /home/slam/workspace/django-auction/auction/utils/loader.py

Stats: 16 executed, 34 missed, 3 excluded, 44 ignored

  1. #-*- coding: utf-8 -*-
  2. """
  3. These utils are ripped directly from Django-Shop.
  4. """
  5. from django.conf import settings
  6. from django.core import exceptions
  7. from django.utils.importlib import import_module
  8. CLASS_PATH_ERROR = 'django-auction is unable to interpret settings value for %s. '\
  9. '%s should be in the form of a tupple: '\
  10. '(\'path.to.models.Class\', \'app_label\').'
  11. def load_class(class_path, setting_name=None):
  12. """
  13. Loads a class given a class_path. The setting value may be a string or a
  14. tuple.
  15. The setting_name parameter is only there for pretty error output, and
  16. therefore is optional
  17. """
  18. if not isinstance(class_path, basestring):
  19. try:
  20. class_path, app_label = class_path
  21. except:
  22. if setting_name:
  23. raise exceptions.ImproperlyConfigured(CLASS_PATH_ERROR % (
  24. setting_name, setting_name))
  25. else:
  26. raise exceptions.ImproperlyConfigured(CLASS_PATH_ERROR % (
  27. 'this setting', 'It'))
  28. try:
  29. class_module, class_name = class_path.rsplit('.', 1)
  30. except ValueError:
  31. if setting_name:
  32. txt = '%s isn\'t a valid module. Check your %s setting' % (
  33. class_path, setting_name)
  34. else:
  35. txt = '%s isn\'t a valid module.' % class_path
  36. raise exceptions.ImproperlyConfigured(txt)
  37. try:
  38. mod = import_module(class_module)
  39. except ImportError, e:
  40. if setting_name:
  41. txt = 'Error importing backend %s: "%s". Check your %s setting' % (
  42. class_module, e, setting_name)
  43. else:
  44. txt = 'Error importing backend %s: "%s".' % (class_module, e)
  45. raise exceptions.ImproperlyConfigured(txt)
  46. try:
  47. clazz = getattr(mod, class_name)
  48. except AttributeError:
  49. if setting_name:
  50. txt = ('Backend module "%s" does not define a "%s" class. Check'
  51. ' your %s setting' % (class_module, class_name,
  52. setting_name))
  53. else:
  54. txt = 'Backend module "%s" does not define a "%s" class.' % (
  55. class_module, class_name)
  56. raise exceptions.ImproperlyConfigured(txt)
  57. return clazz
  58. def get_model_string(model_name):
  59. """
  60. Returns the model string notation Django uses for lazily loaded ForeignKeys
  61. (eg 'auth.User') to prevent circular imports.
  62. This is needed to allow our crazy custom model usage.
  63. """
  64. setting_name = 'AUCTION_%s_MODEL' % model_name.upper().replace('_', '')
  65. class_path = getattr(settings, setting_name, None)
  66. if not class_path:
  67. return 'auction.%s' % model_name
  68. elif isinstance(class_path, basestring):
  69. parts = class_path.split('.')
  70. try:
  71. index = parts.index('models') - 1
  72. except ValueError, e:
  73. raise exceptions.ImproperlyConfigured(CLASS_PATH_ERROR % (
  74. setting_name, setting_name))
  75. app_label, model_name = parts[index], parts[-1]
  76. else:
  77. try:
  78. class_path, app_label = class_path
  79. model_name = class_path.split('.')[-1]
  80. except:
  81. raise exceptions.ImproperlyConfigured(CLASS_PATH_ERROR % (
  82. setting_name, setting_name))
  83. return '%s.%s' % (app_label, model_name)