from pathlib import Path from uuid import uuid4 from django.conf import settings from django.core.exceptions import ValidationError from django.core.validators import FileExtensionValidator from django.db import models def profile_avatar_upload_to(instance, filename): suffix = Path(filename).suffix.lower() return f"accounts/avatars/{instance.user_id}/{uuid4().hex}{suffix}" def validate_avatar_size(image): if image.size > 5 * 1024 * 1024: raise ValidationError("Profile pictures must be 5 MB or smaller.") class ClientProfile(models.Model): user = models.OneToOneField( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="client_profile", ) company = models.CharField(max_length=200, blank=True) job_title = models.CharField(max_length=150, blank=True) phone = models.CharField(max_length=50, blank=True) bio = models.TextField(max_length=600, blank=True) avatar = models.ImageField( upload_to=profile_avatar_upload_to, blank=True, validators=[ FileExtensionValidator(["jpg", "jpeg", "png", "webp"]), validate_avatar_size, ], ) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: verbose_name = "Client Profile" verbose_name_plural = "Client Profiles" def __str__(self): return self.user.get_full_name() or self.user.email or str(self.user.pk) @property def display_name(self): full_name = self.user.get_full_name().strip() if full_name: return full_name return self.user.email