Guest User

django_abstractbaseuser

a guest
Dec 14th, 2014
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. class AbstractBaseUser(models.Model):
  2.     password = models.CharField(_('password'), max_length=128)
  3.     last_login = models.DateTimeField(_('last login'), default=timezone.now)
  4.  
  5.     is_active = True
  6.  
  7.     REQUIRED_FIELDS = []
  8.  
  9.     class Meta:
  10.         abstract = True
  11.  
  12.     def get_username(self):
  13.         "Return the identifying username for this User"
  14.         return getattr(self, self.USERNAME_FIELD)
  15.  
  16.     def __str__(self):
  17.         return self.get_username()
  18.  
  19.     def natural_key(self):
  20.         return (self.get_username(),)
  21.  
  22.     def is_anonymous(self):
  23.         """
  24.        Always returns False. This is a way of comparing User objects to
  25.        anonymous users.
  26.        """
  27.         return False
  28.  
  29.     def is_authenticated(self):
  30.         """
  31.        Always return True. This is a way to tell if the user has been
  32.        authenticated in templates.
  33.        """
  34.         return True
  35.  
  36.     def set_password(self, raw_password):
  37.         self.password = make_password(raw_password)
  38.  
  39.     def check_password(self, raw_password):
  40.         """
  41.        Returns a boolean of whether the raw_password was correct. Handles
  42.        hashing formats behind the scenes.
  43.        """
  44.         def setter(raw_password):
  45.             self.set_password(raw_password)
  46.             self.save(update_fields=["password"])
  47.         return check_password(raw_password, self.password, setter)
  48.  
  49.     def set_unusable_password(self):
  50.         # Sets a value that will never be a valid hash
  51.         self.password = make_password(None)
  52.  
  53.     def has_usable_password(self):
  54.         return is_password_usable(self.password)
  55.  
  56.     def get_full_name(self):
  57.         raise NotImplementedError('subclasses of AbstractBaseUser must provide a get_full_name() method')
  58.  
  59.     def get_short_name(self):
  60.         raise NotImplementedError('subclasses of AbstractBaseUser must provide a get_short_name() method.')
  61.  
  62.     def get_session_auth_hash(self):
  63.         """
  64.        Returns an HMAC of the password field.
  65.        """
  66.         key_salt = "django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash"
  67.         return salted_hmac(key_salt, self.password).hexdigest()
Advertisement
Add Comment
Please, Sign In to add comment