models.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from django.conf import settings
  2. from django.contrib.auth.hashers import make_password
  3. from django.contrib.auth.models import UserManager
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.db import models
  6. from django.utils.importlib import import_module
  7. from django.utils.translation import ugettext_lazy as _
  8. __all__ = (
  9. 'get_user_document',
  10. )
  11. MONGOENGINE_USER_DOCUMENT = getattr(
  12. settings, 'MONGOENGINE_USER_DOCUMENT', 'mongoengine.django.auth.User')
  13. def get_user_document():
  14. """Get the user document class used for authentication.
  15. This is the class defined in settings.MONGOENGINE_USER_DOCUMENT, which
  16. defaults to `mongoengine.django.auth.User`.
  17. """
  18. name = MONGOENGINE_USER_DOCUMENT
  19. dot = name.rindex('.')
  20. module = import_module(name[:dot])
  21. return getattr(module, name[dot + 1:])
  22. class MongoUserManager(UserManager):
  23. """A User manager wich allows the use of MongoEngine documents in Django.
  24. To use the manager, you must tell django.contrib.auth to use MongoUser as
  25. the user model. In you settings.py, you need:
  26. INSTALLED_APPS = (
  27. ...
  28. 'django.contrib.auth',
  29. 'mongoengine.django.mongo_auth',
  30. ...
  31. )
  32. AUTH_USER_MODEL = 'mongo_auth.MongoUser'
  33. Django will use the model object to access the custom Manager, which will
  34. replace the original queryset with MongoEngine querysets.
  35. By default, mongoengine.django.auth.User will be used to store users. You
  36. can specify another document class in MONGOENGINE_USER_DOCUMENT in your
  37. settings.py.
  38. The User Document class has the same requirements as a standard custom user
  39. model: https://docs.djangoproject.com/en/dev/topics/auth/customizing/
  40. In particular, the User Document class must define USERNAME_FIELD and
  41. REQUIRED_FIELDS.
  42. `AUTH_USER_MODEL` has been added in Django 1.5.
  43. """
  44. def contribute_to_class(self, model, name):
  45. super(MongoUserManager, self).contribute_to_class(model, name)
  46. self.dj_model = self.model
  47. self.model = get_user_document()
  48. self.dj_model.USERNAME_FIELD = self.model.USERNAME_FIELD
  49. username = models.CharField(_('username'), max_length=30, unique=True)
  50. username.contribute_to_class(self.dj_model, self.dj_model.USERNAME_FIELD)
  51. self.dj_model.REQUIRED_FIELDS = self.model.REQUIRED_FIELDS
  52. for name in self.dj_model.REQUIRED_FIELDS:
  53. field = models.CharField(_(name), max_length=30)
  54. field.contribute_to_class(self.dj_model, name)
  55. def get(self, *args, **kwargs):
  56. try:
  57. return self.get_query_set().get(*args, **kwargs)
  58. except self.model.DoesNotExist:
  59. # ModelBackend expects this exception
  60. raise self.dj_model.DoesNotExist
  61. @property
  62. def db(self):
  63. raise NotImplementedError
  64. def get_empty_query_set(self):
  65. return self.model.objects.none()
  66. def get_query_set(self):
  67. return self.model.objects
  68. class MongoUser(models.Model):
  69. """"Dummy user model for Django.
  70. MongoUser is used to replace Django's UserManager with MongoUserManager.
  71. The actual user document class is mongoengine.django.auth.User or any
  72. other document class specified in MONGOENGINE_USER_DOCUMENT.
  73. To get the user document class, use `get_user_document()`.
  74. """
  75. objects = MongoUserManager()
  76. class Meta:
  77. app_label = 'mongo_auth'
  78. def set_password(self, password):
  79. """Doesn't do anything, but works around the issue with Django 1.6."""
  80. make_password(password)