from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from PIL import Image

# Create your models here.

class Profile(models.Model):
	user = models.OneToOneField(User, on_delete=models.CASCADE)
	image = models.ImageField(default='profile_logo1.jpg', upload_to='profile_pics')

	def __str__(self):
		return f'{self.user.username} Profile'

	def save(self):
		super().save()
		img = Image.open(self.image.path)
		if img.height > 200 or img.width > 200:
			output_size = (200,200)
			img.thumbnail(output_size)
			img.save(self.image.path)


# @receiver(post_save, sender=User)
# def create_profile(sender, instance, created, **kwargs):
# 	if created:
# 		Profile.objects.create(user=instance)

# @receiver(post_save, sender=User)
# def save_profile(sender, instance, **kwargs):
# 	instance.profile.save()

def post_save_profile_receiver(sender, instance,created, *args, **kwargs):
	if created:
		profile_obj = Profile.objects.create(user=instance)
		profile_obj.save()
post_save.connect(post_save_profile_receiver, sender=User)
