Advertisement
robertvari

Custom User Model

Dec 17th, 2019
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. from django.contrib.auth.models import (
  2.     AbstractBaseUser,
  3.     BaseUserManager
  4. )
  5.  
  6. from django.utils.text import slugify
  7. from django.db import models
  8. import time
  9. from PIL import Image
  10.  
  11.  
  12. class CustomUserManager(BaseUserManager):
  13.     def create_user(self, email, full_name, password=None):
  14.         if not email:
  15.             raise ValueError("Users must have an email address")
  16.  
  17.         if not full_name:
  18.             raise ValueError("Users must have full name")
  19.  
  20.         user = self.model(
  21.             email=self.normalize_email(email),
  22.             full_name=full_name
  23.         )
  24.  
  25.         user.set_password(password)
  26.         user.save(using=self._db)
  27.         return user
  28.  
  29.     def create_superuser(self, email, full_name, password):
  30.         user = self.create_user(
  31.             email=self.normalize_email(email),
  32.             full_name=full_name,
  33.             password=password,
  34.         )
  35.  
  36.         user.is_admin = True
  37.         user.is_staff = True
  38.         user.is_superuser = True
  39.         user.save(using=self._db)
  40.         return user
  41.  
  42.  
  43. class CustomUser(AbstractBaseUser):
  44.     email = models.EmailField(max_length=200, unique=True)
  45.     full_name = models.CharField(max_length=200)
  46.     slug = models.SlugField(unique=True)
  47.  
  48.     # login with email by default
  49.     USERNAME_FIELD = 'email'
  50.  
  51.     # full_name must be provided for registration
  52.     REQUIRED_FIELDS = ("full_name",)
  53.  
  54.     # personal fields
  55.     location = models.CharField(max_length=200, blank=True)
  56.     birth_date = models.DateTimeField(blank=True, null=True)
  57.     profile_pic = models.ImageField(upload_to="profile_pictures", blank=True)
  58.     bio = models.TextField(blank=True)
  59.     linkedin = models.URLField(blank=True)
  60.     facebook = models.URLField(blank=True)
  61.     twitter = models.URLField(blank=True)
  62.     instagram = models.URLField(blank=True)
  63.  
  64.     # social features
  65.     follows = models.ManyToManyField('self', related_name="followers", blank=True)
  66.     # likes = models.ManyToManyField(Post, related_name="likes", blank=True)
  67.  
  68.     # required for AbstractBaseUser
  69.     date_joined = models.DateTimeField(auto_now_add=True)
  70.     last_login = models.DateTimeField(auto_now=True)
  71.  
  72.     is_admin = models.BooleanField(default=False)
  73.     is_active = models.BooleanField(default=True)
  74.     is_staff = models.BooleanField(default=False)
  75.     is_superuser = models.BooleanField(default=False)
  76.  
  77.     objects = CustomUserManager()
  78.  
  79.     class Meta:
  80.         verbose_name_plural = "Users"
  81.  
  82.     def __str__(self):
  83.         return self.email
  84.  
  85.     def delete(self, *args, **kwargs):
  86.         # delete profile picture if any
  87.         if self.profile_pic:
  88.             self.profile_pic.delete(save=False)
  89.         super(CustomUser, self).delete(*args, **kwargs)
  90.  
  91.     def create_slug(self):
  92.         if not self.id:
  93.             slug = slugify(self.full_name)
  94.  
  95.             user = CustomUser.objects.filter(slug=slug).exists()
  96.             if user:
  97.                 slug = f'{slug}-{int(time.time())}'
  98.  
  99.             self.slug = slug
  100.  
  101.     def save(self, *args, **kwargs):
  102.         # delete old image
  103.         try:
  104.             user = CustomUser.objects.get(id=self.id)
  105.             if user:
  106.                 if user.profile_pic.name != self.profile_pic.name:
  107.                     user.profile_pic.delete(save=False)
  108.         except:
  109.             pass
  110.  
  111.         self.create_slug()
  112.  
  113.         super(CustomUser, self).save(*args, **kwargs)
  114.  
  115.         # resize profile picture
  116.         if self.profile_pic:
  117.             image_path = self.profile_pic.path
  118.             img = Image.open(image_path)
  119.  
  120.             if img.size[0] > 300 or img.size[1] > 300:
  121.                 img.thumbnail((300, 300))
  122.                 img.save(image_path)
  123.  
  124.     def has_perm(self, perm, obj=None):
  125.         return self.is_admin
  126.  
  127.     def has_module_perms(self, app_label):
  128.         return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement