admin.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. from django.contrib.admin.sites import AdminSite
  5. class BrowserIDAdminSite(AdminSite):
  6. """Support logging in to the admin interface via BrowserID."""
  7. login_template = 'browserid/admin_login.html'
  8. #: If True, include the normal username and password form as well as
  9. #: the BrowserID button.
  10. include_password_form = False
  11. def copy_registry(self, site):
  12. """
  13. Copy the ModelAdmins that have been registered on another site
  14. so that they are available on this site as well.
  15. Useful when used with :func:`django.contrib.admin.autocomplete`,
  16. allowing you to copy the ModelAdmin entries registered with the
  17. default site, such as the User ModelAdmin. For example, in
  18. ``urls.py``:
  19. .. code-block:: python
  20. from django.contrib import admin
  21. admin.autodiscover()
  22. from django_browserid.admin import site as browserid_admin
  23. browserid_admin.copy_registry(admin.site)
  24. # To include: url(r'^admin/', include(browserid_admin.urls))
  25. :param site:
  26. Site to copy registry entries from.
  27. """
  28. for model, modeladmin in site._registry.items():
  29. self.register(model, modeladmin.__class__)
  30. def login(self, request, extra_context=None):
  31. # Add extra context variables to login view.
  32. extra_context = extra_context or {}
  33. extra_context['include_password_form'] = self.include_password_form
  34. return super(BrowserIDAdminSite, self).login(request, extra_context)
  35. #: Global object for the common case. You can import this in
  36. #: ``admin.py`` and ``urls.py`` instead of
  37. #: :data:`django.contrib.admin.site`.
  38. site = BrowserIDAdminSite()