Advertisement
robertvari

User profile model

Apr 13th, 2020
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. from django.db import models
  2.  
  3. class Profile(models.Model):
  4.     user = models.OneToOneField(CreativeUser, on_delete=models.CASCADE, related_name='profile')
  5.     slug = models.SlugField(unique=True, blank=True)
  6.     full_name = models.CharField('Full name', max_length=200, blank=True)
  7.     image = models.ImageField(upload_to=photo_directory_path, blank=True)
  8.     job = models.CharField(max_length=200, blank=True)
  9.     location = models.CharField(max_length=200, blank=True)
  10.     about = models.TextField(max_length=5000, blank=True)
  11.  
  12.     def save(self, *args, **kwargs):
  13.         try:
  14.             profile = Profile.objects.get(id=self.id)
  15.             if profile:
  16.                 if profile.image.name != self.image.name:
  17.                     profile.image.delete(save=False)
  18.         except Exception as e:
  19.             print(e)
  20.             pass
  21.  
  22.         super().save(*args, **kwargs)
  23.  
  24.         if self.image:
  25.             image_path = self.image.path
  26.             img = Image.open(image_path)
  27.  
  28.             if img.size[0] > 300 or img.size[1] > 300:
  29.                 img.thumbnail((300, 300))
  30.                 img.save(image_path)
  31.  
  32.     def __str__(self):
  33.         return self.user.email
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement