You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from django.conf import settings
  2. from django.contrib.auth.models import User
  3. from django.core.exceptions import ImproperlyConfigured
  4. from django.utils import six
  5. from django.utils.html import conditional_escape
  6. from django.utils.safestring import mark_safe
  7. def get_user_model():
  8. """
  9. Return the User model
  10. Using this function instead of Django 1.5's get_user_model allows backwards
  11. compatibility with Django 1.4.
  12. """
  13. try:
  14. # Django 1.5+
  15. from django.contrib.auth import get_user_model
  16. except ImportError:
  17. # Django <= 1.4
  18. model = User
  19. else:
  20. model = get_user_model()
  21. # Test if user model has any custom fields and add attributes to the _meta
  22. # class
  23. core_fields = set([f.name for f in User._meta.fields])
  24. model_fields = set([f.name for f in model._meta.fields])
  25. new_fields = model_fields.difference(core_fields)
  26. model._meta.has_additional_fields = len(new_fields) > 0
  27. model._meta.additional_fields = new_fields
  28. return model
  29. # A setting that can be used in foreign key declarations
  30. AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
  31. # Two additional settings that are useful in South migrations when
  32. # specifying the user model in the FakeORM
  33. try:
  34. AUTH_USER_APP_LABEL, AUTH_USER_MODEL_NAME = AUTH_USER_MODEL.rsplit('.', 1)
  35. except ValueError:
  36. raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form"
  37. " 'app_label.model_name'")
  38. def format_html(format_string, *args, **kwargs):
  39. """
  40. Backport of format_html from Django 1.5+ to support Django 1.4
  41. """
  42. args_safe = map(conditional_escape, args)
  43. kwargs_safe = dict([(k, conditional_escape(v)) for (k, v) in
  44. six.iteritems(kwargs)])
  45. return mark_safe(format_string.format(*args_safe, **kwargs_safe))