Guest User

Untitled

a guest
Feb 11th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. from __future__ import unicode_literals
  2.  
  3. from django.db import models
  4. from django.core.mail import send_mail
  5. from django.contrib.auth.models import PermissionsMixin
  6. from django.contrib.auth.base_user import AbstractBaseUser
  7. from django.utils.translation import ugettext_lazy as _
  8.  
  9. from .managers import UserManager
  10.  
  11.  
  12. class User(AbstractBaseUser, PermissionsMixin):
  13. email = models.EmailField(_('email address'), unique=True)
  14. first_name = models.CharField(_('first name'), max_length=30, blank=True)
  15. last_name = models.CharField(_('last name'), max_length=30, blank=True)
  16. date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
  17. is_active = models.BooleanField(_('active'), default=True)
  18. avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)
  19.  
  20. objects = UserManager()
  21.  
  22. USERNAME_FIELD = 'email'
  23. REQUIRED_FIELDS = []
  24.  
  25. class Meta:
  26. verbose_name = _('user')
  27. verbose_name_plural = _('users')
  28.  
  29. def get_full_name(self):
  30. '''
  31. Returns the first_name plus the last_name, with a space in between.
  32. '''
  33. full_name = '%s %s' % (self.first_name, self.last_name)
  34. return full_name.strip()
  35.  
  36. def get_short_name(self):
  37. '''
  38. Returns the short name for the user.
  39. '''
  40. return self.first_name
  41.  
  42. def email_user(self, subject, message, from_email=None, **kwargs):
  43. '''
  44. Sends an email to this User.
  45. '''
  46. send_mail(subject, message, from_email, [self.email], **kwargs)
  47.  
  48. from django.contrib.auth.base_user import BaseUserManager
  49.  
  50. class UserManager(BaseUserManager):
  51. use_in_migrations = True
  52.  
  53. def _create_user(self, email, password, **extra_fields):
  54. """
  55. Creates and saves a User with the given email and password.
  56. """
  57. if not email:
  58. raise ValueError('The given email must be set')
  59. email = self.normalize_email(email)
  60. user = self.model(email=email, **extra_fields)
  61. user.set_password(password)
  62. user.save(using=self._db)
  63. return user
  64.  
  65. def create_user(self, email, password=None, **extra_fields):
  66. extra_fields.setdefault('is_superuser', False)
  67. return self._create_user(email, password, **extra_fields)
  68.  
  69. def create_superuser(self, email, password, **extra_fields):
  70. extra_fields.setdefault('is_superuser', True)
  71.  
  72. if extra_fields.get('is_superuser') is not True:
  73. raise ValueError('Superuser must have is_superuser=True.')
  74.  
  75. return self._create_user(email, password, **extra_fields)
  76.  
  77.  
  78. from django.db import models
  79. # from testapp.core.models import User
  80. from django.conf import settings
  81.  
  82.  
  83. class Course(models.Model):
  84. slug = models.SlugField(max_length=100)
  85. name = models.CharField(max_length=100)
  86. # tutor = models.ForeignKey(User, on_delete=models.CASCADE)
  87. """
  88. This is perfectly okay. But if you are creating a reusable app,
  89. that you want to make available for the public,
  90. it is strongly advised that you use the following strategy
  91. """
  92. tutor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
Add Comment
Please, Sign In to add comment