Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. from django.db import models
  2. from django.contrib.auth.models import AbstractBaseUser
  3. from django.contrib.auth.models import PermissionsMixin
  4. from django.contrib.auth.models import BaseUserManager
  5.  
  6. # Create your models here.
  7.  
  8. class UserProfileManager(BaseUserManager):
  9. """Helps Django work with our custom user model."""
  10.  
  11. def create_user(self, email, name, password=None):
  12. """Creates a new user profile object."""
  13.  
  14. if not email:
  15. raise ValueError('Users must have an email address.')
  16.  
  17. email = self.normalize_email(email)
  18. user = self.model(email=email, name=name)
  19.  
  20. user.set_password(password)
  21. user.save(using=self._db)
  22.  
  23. return user
  24.  
  25. def create_superuser(self, email, name, password):
  26. """Creates and saves a new superuser with given details."""
  27.  
  28. user = self.create_user(email, name, password)
  29.  
  30. user.is_superuser = True
  31. user.is_staff = True
  32.  
  33. user.save(using=self._db)
  34.  
  35. return user
  36.  
  37.  
  38. class UserProfile(AbstractBaseUser, PermissionsMixin):
  39. """Represents a "user profile" inside our system."""
  40.  
  41. email = models.EmailField(max_length=255, unique=True)
  42. name = models.CharField(max_length=255)
  43. is_active = models.BooleanField(default=True)
  44. is_staff = models.BooleanField(default=False)
  45.  
  46. objects = UserProfileManager()
  47.  
  48. USERNAME_FIELD = 'email'
  49. REQUIRED_FIELDS = ['name']
  50.  
  51. def get_full_name(self):
  52. """Used to get a user's full name."""
  53.  
  54. return self.name
  55.  
  56. def get_short_name(self):
  57. """Used to get a user's short name."""
  58.  
  59. return self.name
  60.  
  61. def __str__(self):
  62. """Django uses it when it needs to convert the object to a string."""
  63.  
  64. return self.email
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement