Add get_user_document and improve mongo_auth module
* Added a get_user_document() methot to access the actual Document class
    used for authentication.
  * Clarified the docstring on MongoUser to prevent its use when the user
    Document class should be used.
  * Removed the masking of exceptions when loading the user document class.
			
			
This commit is contained in:
		| @@ -6,10 +6,29 @@ from django.utils.importlib import import_module | |||||||
| from django.utils.translation import ugettext_lazy as _ | from django.utils.translation import ugettext_lazy as _ | ||||||
|  |  | ||||||
|  |  | ||||||
|  | __all__ = ( | ||||||
|  |     'get_user_document', | ||||||
|  | ) | ||||||
|  |  | ||||||
|  |  | ||||||
| MONGOENGINE_USER_DOCUMENT = getattr( | MONGOENGINE_USER_DOCUMENT = getattr( | ||||||
|     settings, 'MONGOENGINE_USER_DOCUMENT', 'mongoengine.django.auth.User') |     settings, 'MONGOENGINE_USER_DOCUMENT', 'mongoengine.django.auth.User') | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def get_user_document(self): | ||||||
|  |     """Get the user docuemnt class user for authentcation. | ||||||
|  |  | ||||||
|  |     This is the class defined in settings.MONGOENGINE_USER_DOCUMENT, which | ||||||
|  |     defaults to `mongoengine.django.auth.User`. | ||||||
|  |  | ||||||
|  |     """ | ||||||
|  |  | ||||||
|  |     name = MONGOENGINE_USER_DOCUMENT | ||||||
|  |     dot = name.rindex('.') | ||||||
|  |     module = import_module(name[:dot]) | ||||||
|  |     return getattr(module, name[dot + 1:]) | ||||||
|  |  | ||||||
|  |  | ||||||
| class MongoUserManager(UserManager): | class MongoUserManager(UserManager): | ||||||
|     """A User manager wich allows the use of MongoEngine documents in Django. |     """A User manager wich allows the use of MongoEngine documents in Django. | ||||||
|  |  | ||||||
| @@ -44,7 +63,7 @@ class MongoUserManager(UserManager): | |||||||
|     def contribute_to_class(self, model, name): |     def contribute_to_class(self, model, name): | ||||||
|         super(MongoUserManager, self).contribute_to_class(model, name) |         super(MongoUserManager, self).contribute_to_class(model, name) | ||||||
|         self.dj_model = self.model |         self.dj_model = self.model | ||||||
|         self.model = self._get_user_document() |         self.model = get_user_document() | ||||||
|  |  | ||||||
|         self.dj_model.USERNAME_FIELD = self.model.USERNAME_FIELD |         self.dj_model.USERNAME_FIELD = self.model.USERNAME_FIELD | ||||||
|         username = models.CharField(_('username'), max_length=30, unique=True) |         username = models.CharField(_('username'), max_length=30, unique=True) | ||||||
| @@ -55,16 +74,6 @@ class MongoUserManager(UserManager): | |||||||
|             field = models.CharField(_(name), max_length=30) |             field = models.CharField(_(name), max_length=30) | ||||||
|             field.contribute_to_class(self.dj_model, name) |             field.contribute_to_class(self.dj_model, name) | ||||||
|  |  | ||||||
|     def _get_user_document(self): |  | ||||||
|         try: |  | ||||||
|             name = MONGOENGINE_USER_DOCUMENT |  | ||||||
|             dot = name.rindex('.') |  | ||||||
|             module = import_module(name[:dot]) |  | ||||||
|             return getattr(module, name[dot + 1:]) |  | ||||||
|         except ImportError: |  | ||||||
|             raise ImproperlyConfigured("Error importing %s, please check " |  | ||||||
|                                        "settings.MONGOENGINE_USER_DOCUMENT" |  | ||||||
|                                        % name) |  | ||||||
|  |  | ||||||
|     def get(self, *args, **kwargs): |     def get(self, *args, **kwargs): | ||||||
|         try: |         try: | ||||||
| @@ -85,5 +94,14 @@ class MongoUserManager(UserManager): | |||||||
|  |  | ||||||
|  |  | ||||||
| class MongoUser(models.Model): | class MongoUser(models.Model): | ||||||
|     objects = MongoUserManager() |     """"Dummy user model for Django. | ||||||
|  |  | ||||||
|  |     MongoUser is used to replace Django's UserManager with MongoUserManager. | ||||||
|  |     The actual user document class is mongoengine.django.auth.User or any | ||||||
|  |     other document class specified in MONGOENGINE_USER_DOCUMENT. | ||||||
|  |  | ||||||
|  |     To get the user document class, use `get_user_document()`. | ||||||
|  |  | ||||||
|  |     """ | ||||||
|  |  | ||||||
|  |     objects = MongoUserManager() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user