Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.contrib.auth.models import (
- AbstractBaseUser,
- BaseUserManager
- )
- from django.utils.text import slugify
- from django.db import models
- import time
- from PIL import Image
- class CustomUserManager(BaseUserManager):
- def create_user(self, email, full_name, password=None):
- if not email:
- raise ValueError("Users must have an email address")
- if not full_name:
- raise ValueError("Users must have full name")
- user = self.model(
- email=self.normalize_email(email),
- full_name=full_name
- )
- user.set_password(password)
- user.save(using=self._db)
- return user
- def create_superuser(self, email, full_name, password):
- user = self.create_user(
- email=self.normalize_email(email),
- full_name=full_name,
- password=password,
- )
- user.is_admin = True
- user.is_staff = True
- user.is_superuser = True
- user.save(using=self._db)
- return user
- class CustomUser(AbstractBaseUser):
- email = models.EmailField(max_length=200, unique=True)
- full_name = models.CharField(max_length=200)
- slug = models.SlugField(unique=True)
- # login with email by default
- USERNAME_FIELD = 'email'
- # full_name must be provided for registration
- REQUIRED_FIELDS = ("full_name",)
- # personal fields
- location = models.CharField(max_length=200, blank=True)
- birth_date = models.DateTimeField(blank=True, null=True)
- profile_pic = models.ImageField(upload_to="profile_pictures", blank=True)
- bio = models.TextField(blank=True)
- linkedin = models.URLField(blank=True)
- facebook = models.URLField(blank=True)
- twitter = models.URLField(blank=True)
- instagram = models.URLField(blank=True)
- # social features
- follows = models.ManyToManyField('self', related_name="followers", blank=True)
- # likes = models.ManyToManyField(Post, related_name="likes", blank=True)
- # required for AbstractBaseUser
- date_joined = models.DateTimeField(auto_now_add=True)
- last_login = models.DateTimeField(auto_now=True)
- is_admin = models.BooleanField(default=False)
- is_active = models.BooleanField(default=True)
- is_staff = models.BooleanField(default=False)
- is_superuser = models.BooleanField(default=False)
- objects = CustomUserManager()
- class Meta:
- verbose_name_plural = "Users"
- def __str__(self):
- return self.email
- def delete(self, *args, **kwargs):
- # delete profile picture if any
- if self.profile_pic:
- self.profile_pic.delete(save=False)
- super(CustomUser, self).delete(*args, **kwargs)
- def create_slug(self):
- if not self.id:
- slug = slugify(self.full_name)
- user = CustomUser.objects.filter(slug=slug).exists()
- if user:
- slug = f'{slug}-{int(time.time())}'
- self.slug = slug
- def save(self, *args, **kwargs):
- # delete old image
- try:
- user = CustomUser.objects.get(id=self.id)
- if user:
- if user.profile_pic.name != self.profile_pic.name:
- user.profile_pic.delete(save=False)
- except:
- pass
- self.create_slug()
- super(CustomUser, self).save(*args, **kwargs)
- # resize profile picture
- if self.profile_pic:
- image_path = self.profile_pic.path
- img = Image.open(image_path)
- if img.size[0] > 300 or img.size[1] > 300:
- img.thumbnail((300, 300))
- img.save(image_path)
- def has_perm(self, perm, obj=None):
- return self.is_admin
- def has_module_perms(self, app_label):
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement