Test coverage report: auction.utils.loader

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