# Path: lazysignup/models.py
# class LazyUser(models.Model):
#     user = models.OneToOneField(constants.LAZYSIGNUP_USER_MODEL, on_delete=models.CASCADE)
#     created = models.DateTimeField(default=now, db_index=True)
#     objects = LazyUserManager()
# 
#     @classmethod
#     def get_user_class(cls):
#         related_user_field = cls._meta.get_field('user')
#         # Django < 1.9 has rel.to
#         if hasattr(related_user_field, 'rel'):
#             rel_to = related_user_field.rel.to if related_user_field.rel else None
#         elif hasattr(related_user_field, 'remote_field'):
#             rel_to = related_user_field.remote_field.model if related_user_field.remote_field else None
#         return rel_to
# 
#     def __str__(self):
#         return '{0}:{1}'.format(self.user, self.created)


from django.contrib.auth.backends import ModelBackend
from lazysignup.models import LazyUser
from django.contrib.auth import get_user_model


class LazySignupBackend(ModelBackend):

    def authenticate(self, request=None, username=None):
        user_class = LazyUser.get_user_class()
        try:
            return user_class.objects.get(**{
                get_user_model().USERNAME_FIELD: username
            })
        except user_class.DoesNotExist:
            return None

    def get_user(self, user_id):
        # Annotate the user with our backend so it's always available,
        # not just when authenticate() has been called. This will be
        # used by the is_lazy_user filter.

# Path: lazysignup/models.py
# class LazyUser(models.Model):
#     user = models.OneToOneField(constants.LAZYSIGNUP_USER_MODEL, on_delete=models.CASCADE)
#     created = models.DateTimeField(default=now, db_index=True)
#     objects = LazyUserManager()
# 
#     @classmethod
#     def get_user_class(cls):
#         related_user_field = cls._meta.get_field('user')
#         # Django < 1.9 has rel.to
#         if hasattr(related_user_field, 'rel'):
#             rel_to = related_user_field.rel.to if related_user_field.rel else None
#         elif hasattr(related_user_field, 'remote_field'):
#             rel_to = related_user_field.remote_field.model if related_user_field.remote_field else None
#         return rel_to
# 
#     def __str__(self):
#         return '{0}:{1}'.format(self.user, self.created)


# Path: lazysignup/backends.py
from django.contrib.auth.backends import ModelBackend
from lazysignup.models import LazyUser
from django.contrib.auth import get_user_model



class LazySignupBackend(ModelBackend):

    def authenticate(self, request=None, username=None):
        user_class = LazyUser.get_user_class()
        try:
            return user_class.objects.get(**{
                get_user_model().USERNAME_FIELD: username
            })
        except user_class.DoesNotExist:
            return None

    def get_user(self, user_id):
        # Annotate the user with our backend so it's always available,
        # not just when authenticate() has been called. This will be
        # used by the is_lazy_user filter.

        user_class = LazyUser.get_user_class()