from django.db import models
from django.contrib.auth.models import User
from PIL import Image

class Profile(models.Model):
  user = models.OneToOneField(User, on_delete=models.CASCADE)
  image = models.ImageField(default="default.jpg", upload_to="profile_pics")
  def __str__(self):
    return f'{self.user.username} Profile'

  def save(self,*args,**kwargs):
    super().save(*args,**kwargs)
    img = Image.open(self.image.path)

    if img.height > 300 or img.width > 300:
      output_image = (300,300)
      img.thumbnail(output_image)
      img.save(self.image.path)


# u = User.objects.all().first() 
# p = Profile.objects.all().first()

# bu<->
# su<->bp

# bu = User.objects.filter(username="Bharath").first() 
# bp = Profile.objects.filter(user = bu).first()

# bp = Profile.objects.filter(user__username='Bharath')


# su = User.objects.filter(username="Saiteja").first() 
# bp.user = su
# bp.save()