Skip to content

Commit

Permalink
Merge branch 'chrisglass-circular-import-legutierr-2'
Browse files Browse the repository at this point in the history
Conflicts:
	shop/util/loader.py
	shop/views/cart.py
  • Loading branch information
chrisglass committed Jan 25, 2012
2 parents 6a34036 + 0f121b7 commit 82ef773
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions shop/util/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@
from django.core import exceptions
from django.utils.importlib import import_module

CLASS_PATH_ERROR = '''django-shop is unable to interpret settings value for %s. %s should ' \
'be in ther form of a tuple: (\'path.to.models.Class\',
\'app_label\').'''

def load_class(class_path, setting_name=None):
"""
Loads a class given a class_path.
The setting_name parameter is only there for pretty error output, and
Loads a class given a class_path. The setting value may be a string or a tuple.
The setting_name parameter is only there for pretty error output, and
therefore is optional
"""
if isinstance(class_path, basestring):
pass
else:
try:
class_path, app_label = class_path
except:
if setting_name:
raise exceptions.ImproperlyConfigured(CLASS_PATH_ERROR % (setting_name, setting_name))
else:
raise exceptions.ImproperlyConfigured(CLASS_PATH_ERROR % ("this setting", "It"))

try:
class_module, class_name = class_path.rsplit('.', 1)
except ValueError:
Expand Down Expand Up @@ -52,10 +65,24 @@ def get_model_string(model_name):
This is needed to allow our crazy custom model usage.
"""
class_path = getattr(settings,
'SHOP_%s_MODEL' % model_name.upper().replace('_', ''), None)
setting_name = 'SHOP_%s_MODEL' % model_name.upper().replace('_', '')
class_path = getattr(settings, setting_name, None)

if not class_path:
return 'shop.%s' % model_name
elif isinstance(class_path, basestring):
parts = class_path.split('.')
try:
index = parts.index('models') - 1
except ValueError, e:
raise exceptions.ImproperlyConfigured(CLASS_PATH_ERROR % (setting_name, setting_name))
app_label, model_name = parts[index], parts[-1]
else:
klass = load_class(class_path)
return '%s.%s' % (klass._meta.app_label, klass.__name__)
try:
class_path, app_label = class_path
model_name = class_path.split('.')[-1]
except:
raise exceptions.ImproperlyConfigured(CLASS_PATH_ERROR % (setting_name, setting_name))

return "%s.%s" % (app_label, model_name)

1 comment on commit 82ef773

@zeus
Copy link
Contributor

@zeus zeus commented on 82ef773 Jan 26, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing:

 '%s.%s' % (klass._meta.app_label, klass.__name__)

with:

 app_label, model_name = parts[index], parts[-1]

is wrong

For example if we have Order model for app 'foo' stored in 'bar' module (that is valid code), we will 'bar.Order' as model string which is invalid.

Please sign in to comment.