Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. from django.conf import settings
  2. from django.contrib.auth.models import (
  3. AbstractBaseUser, BaseUserManager, PermissionsMixin
  4. )
  5. from django.db import models
  6. from phone.models import TimestampedModel
  7. import pyotp
  8.  
  9. class UserManager(BaseUserManager):
  10. """
  11. Django requires that custom users define their own Manager class. By
  12. inheriting from `BaseUserManager`, we get a lot of the same code used by
  13. Django to create a `User` for free.
  14. All we have to do is override the `create_user` function which we will use
  15. to create `User` objects.
  16. """
  17.  
  18. def create_user(self, email, password=None, **extra_fields):
  19. """Create and return a `User` with an email and password."""
  20.  
  21. if email is None:
  22. raise TypeError('Users must have an email address.')
  23.  
  24. user = self.model(email=self.normalize_email(email), **extra_fields)
  25.  
  26. user.set_password(password)
  27.  
  28. user.save(using=self._db)
  29.  
  30. return user
  31.  
  32. def create_superuser(self, email, password, **extra_fields):
  33.  
  34. """
  35. Create and return a `User` with superuser powers.
  36. Superuser powers means that this use is an admin that can do anything
  37. they want.
  38. """
  39. if password is None:
  40. raise TypeError('Superusers must have a password.')
  41.  
  42. user = self.create_user(email,password=password, **extra_fields)
  43. user.is_superuser = True
  44. user.is_staff = True
  45. user.save()
  46.  
  47. return user
  48.  
  49.  
  50. class User(AbstractBaseUser, PermissionsMixin, TimestampedModel):
  51. email = models.EmailField(db_index=True, unique=True)
  52. is_active = models.BooleanField(default = True)
  53.  
  54. is_staff = models.BooleanField(default = False)
  55.  
  56. USERNAME_FIELD = 'email'
  57.  
  58. key = models.CharField(max_length=100, unique=True, blank=True)
  59.  
  60. enable_authenticator = models.BooleanField(default=False) #We can use this to enable 2fa for users
  61. objects = UserManager()
  62.  
  63. class Meta:
  64. verbose_name ='user'
  65. verbose_name_plural = 'users'
  66.  
  67. def __str__(self):
  68. """
  69. Returns a string representation of this `User`.
  70. This string is used when a `User` is printed in the console.
  71. """
  72. return self.email
  73.  
  74. def get_short_name(self):
  75.  
  76. return self.first_name
  77.  
  78. def authenticate(self, otp):
  79. """ This method authenticates the given otp"""
  80. provided_otp = 0
  81. try:
  82. provided_otp = int(otp)
  83. except:
  84. return False
  85. #Here we are using Time Based OTP. The interval is 60 seconds.
  86. #otp must be provided within this interval or it's invalid
  87. t = pyotp.TOTP(self.key, interval=300)
  88. return t.verify(provided_otp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement