jazzband / django-authority

A Django app that provides generic per-object-permissions for Django's auth app and helpers to create custom permission checks.
http://django-authority.readthedocs.org
BSD 3-Clause "New" or "Revised" License
292 stars 58 forks source link

Django init with haystack autodiscovery fails on direct import of User model #10

Open gthb opened 11 years ago

gthb commented 11 years ago

The commit 770e696313329bac6df9675d85e9169b77442fd4 (found via git bisect) causes Django initialization to fail with a circular import if Haystack is installed, with its autodiscover init:

Traceback (most recent call last):
  File wsgi.py", line 105, in <module>
    import X.Y.sitemaps
  File X/Y/sitemaps.py", line 5, in <module>
    from X.Y.models import A, B, C
  File X/Y/models.py", line 14, in <module>
    from authority.models import Permission, Group
  File "ext/django-authority/authority/__init__.py", line 2, in <module>
    from authority.sites import site, get_check, get_choices_for, register, unregister
  File "ext/django-authority/authority/sites.py", line 7, in <module>
    from authority.permissions import BasePermission
  File "ext/django-authority/authority/permissions.py", line 9, in <module>
    from authority.models import Permission
  File "ext/django-authority/authority/models.py", line 10, in <module>
    from authority.utils import User
  File "ext/django-authority/authority/utils.py", line 11, in <module>
    User = get_user_class()
  File "ext/django-authority/authority/utils.py", line 6, in get_user_class
    return auth.get_user_model()
  File "pyenv/DJANGO15/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 127, in get_user_model
    user_model = get_model(app_label, model_name)
  File "pyenv/DJANGO15/lib/python2.7/site-packages/django/db/models/loading.py", line 230, in get_model
    self._populate()
  File "pyenv/DJANGO15/lib/python2.7/site-packages/django/db/models/loading.py", line 72, in _populate
    self.load_app(app_name, True)
  File "pyenv/DJANGO15/lib/python2.7/site-packages/django/db/models/loading.py", line 94, in load_app
    app_module = import_module(app_name)
  File "pyenv/DJANGO15/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "pyenv/DJANGO15/lib/python2.7/site-packages/haystack/__init__.py", line 144, in <module>
    handle_registrations()
  File "pyenv/DJANGO15/lib/python2.7/site-packages/haystack/__init__.py", line 140, in handle_registrations
    search_sites_conf = __import__(settings.HAYSTACK_SITECONF)
  File X/search_sites.py", line 2, in <module>
    haystack.autodiscover()
  File "pyenv/DJANGO15/lib/python2.7/site-packages/haystack/__init__.py", line 121, in autodiscover
    importlib.import_module("%s.search_indexes" % app)
  File "pyenv/DJANGO15/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File X/Y/search_indexes.py", line 2, in <module>
    from X.Y.models import A, D, E
ImportError: cannot import name A

If the change was really made just for readability, then it might be worth reverting to avoid this kind of problem.