# Path: micro_admin/models.py
# class User(AbstractBaseUser, PermissionsMixin):
#     username = models.CharField(max_length=30, unique=True)
#     email = models.EmailField(verbose_name='email address', max_length=255, unique=True, db_index=True,)
#     user_roles = models.CharField(choices=USER_ROLES, max_length=10)
#     is_employee = models.BooleanField(default=False)
#     date_of_birth = models.DateField(default='1970-01-01')
#     is_active = models.BooleanField(default=True)
#     is_admin = models.BooleanField(default=False)
#     is_staff = models.BooleanField(_('staff status'), default=False)
#     date_joined = models.DateTimeField(default=timezone.now)
#     is_special = models.BooleanField(default=False)
#     first_name = models.CharField(max_length=100, default='')
#     last_name = models.CharField(max_length=100, default='')
#     gender = models.CharField(choices=GENDER_TYPES, max_length=10)
#     fb_profile = models.URLField(default='')
#     tw_profile = models.URLField(default='')
#     ln_profile = models.URLField(default='')
#     google_plus_url = models.URLField(default='')
#     about = models.CharField(max_length=2000, default='', null=True, blank=True)
#     state = models.CharField(max_length=50)
#     city = models.CharField(max_length=50)
#     area = models.CharField(max_length=150)
#     address = models.TextField(max_length=1000, default='')
#     mobile = models.BigIntegerField(default='0')
#     website = models.URLField(default='', null=True)
#     phones = models.TextField(max_length=100, default='', null=True)
#     pincode = models.TextField(max_length=50, default='', null=True)
#     min_published_blogs = models.IntegerField(default=0)
#     max_published_blogs = models.IntegerField(default=0)
# 
#     objects = UserManager()
# 
#     USERNAME_FIELD = 'email'
#     REQUIRED_FIELDS = ['username']
# 
#     def get_full_name(self):
#         # The user is identified by their email address
#         return self.first_name + ' ' + self.last_name
# 
#     def get_short_name(self):
#         # The user is identified by their email address
#         return self.first_name
# 
#     def __unicode__(self):
#         return self.first_name + ' (' + self.email + ')'
# 
#     def total_posts(self):
#         return Post.objects.filter(user=self).count()
# 
#     def drafted_posts(self):
#         return Post.objects.filter(user=self, status="D").count()
# 
#     def is_site_admin(self):
#         if self.user_roles == "Admin" or self.is_superuser:
#             return True
#         return False
# 
#     class Meta:
#         permissions = (
#             ("blog_moderator", "Can enable or disable blog posts"),
#             ("blogger", "Can write blog posts"),
#         )
# 
#     def get_week_posts(self, value):
#         if value:
#             value = value.split('-')
#             previous_date = datetime.strptime(value[0].strip(), "%Y/%m/%d").strftime("%Y-%m-%d")
#             current_date = datetime.strptime(value[1].strip(), "%Y/%m/%d").strftime("%Y-%m-%d")
#         else:
#             current_date = datetime.strptime(str(datetime.now().date()), "%Y-%m-%d").strftime("%Y-%m-%d")
#             previous_date = datetime.strptime(str(datetime.now().date() - timedelta(days=7)), "%Y-%m-%d").strftime("%Y-%m-%d")
#         post = Post.objects.filter(user=self, created_on__range=(previous_date, current_date))
#         return post.filter(status='P').count(), post.filter(status='D').count(), post.filter(status='R').count(), post.filter(status='T').count()


from django.contrib.auth.backends import ModelBackend
from micro_admin.models import User
"""Log in to Django without providing a password."""



class PasswordlessAuthBackend(ModelBackend):

    """Custom authentication in djnago without providing a password."""

    def authenticate(self, username=None):
        try:
            return User.objects.get(email=username)
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:

# Path: micro_admin/models.py
# class User(AbstractBaseUser, PermissionsMixin):
#     username = models.CharField(max_length=30, unique=True)
#     email = models.EmailField(verbose_name='email address', max_length=255, unique=True, db_index=True,)
#     user_roles = models.CharField(choices=USER_ROLES, max_length=10)
#     is_employee = models.BooleanField(default=False)
#     date_of_birth = models.DateField(default='1970-01-01')
#     is_active = models.BooleanField(default=True)
#     is_admin = models.BooleanField(default=False)
#     is_staff = models.BooleanField(_('staff status'), default=False)
#     date_joined = models.DateTimeField(default=timezone.now)
#     is_special = models.BooleanField(default=False)
#     first_name = models.CharField(max_length=100, default='')
#     last_name = models.CharField(max_length=100, default='')
#     gender = models.CharField(choices=GENDER_TYPES, max_length=10)
#     fb_profile = models.URLField(default='')
#     tw_profile = models.URLField(default='')
#     ln_profile = models.URLField(default='')
#     google_plus_url = models.URLField(default='')
#     about = models.CharField(max_length=2000, default='', null=True, blank=True)
#     state = models.CharField(max_length=50)
#     city = models.CharField(max_length=50)
#     area = models.CharField(max_length=150)
#     address = models.TextField(max_length=1000, default='')
#     mobile = models.BigIntegerField(default='0')
#     website = models.URLField(default='', null=True)
#     phones = models.TextField(max_length=100, default='', null=True)
#     pincode = models.TextField(max_length=50, default='', null=True)
#     min_published_blogs = models.IntegerField(default=0)
#     max_published_blogs = models.IntegerField(default=0)
# 
#     objects = UserManager()
# 
#     USERNAME_FIELD = 'email'
#     REQUIRED_FIELDS = ['username']
# 
#     def get_full_name(self):
#         # The user is identified by their email address
#         return self.first_name + ' ' + self.last_name
# 
#     def get_short_name(self):
#         # The user is identified by their email address
#         return self.first_name
# 
#     def __unicode__(self):
#         return self.first_name + ' (' + self.email + ')'
# 
#     def total_posts(self):
#         return Post.objects.filter(user=self).count()
# 
#     def drafted_posts(self):
#         return Post.objects.filter(user=self, status="D").count()
# 
#     def is_site_admin(self):
#         if self.user_roles == "Admin" or self.is_superuser:
#             return True
#         return False
# 
#     class Meta:
#         permissions = (
#             ("blog_moderator", "Can enable or disable blog posts"),
#             ("blogger", "Can write blog posts"),
#         )
# 
#     def get_week_posts(self, value):
#         if value:
#             value = value.split('-')
#             previous_date = datetime.strptime(value[0].strip(), "%Y/%m/%d").strftime("%Y-%m-%d")
#             current_date = datetime.strptime(value[1].strip(), "%Y/%m/%d").strftime("%Y-%m-%d")
#         else:
#             current_date = datetime.strptime(str(datetime.now().date()), "%Y-%m-%d").strftime("%Y-%m-%d")
#             previous_date = datetime.strptime(str(datetime.now().date() - timedelta(days=7)), "%Y-%m-%d").strftime("%Y-%m-%d")
#         post = Post.objects.filter(user=self, created_on__range=(previous_date, current_date))
#         return post.filter(status='P').count(), post.filter(status='D').count(), post.filter(status='R').count(), post.filter(status='T').count()


# Path: micro_admin/auth_backend.py
from django.contrib.auth.backends import ModelBackend
from micro_admin.models import User

"""Log in to Django without providing a password."""



class PasswordlessAuthBackend(ModelBackend):

    """Custom authentication in djnago without providing a password."""

    def authenticate(self, username=None):
        try:
            return User.objects.get(email=username)
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:

            return User.objects.get(pk=user_id)