def save(self, commit=True): data = self.cleaned_data if data.get("photo") and not isinstance(data["photo"], basestring): if self.instance.photo and self.instance.image_handler and self.instance.image_handler.storage: self.instance.image_handler.delete() filename = generate_filename(data["photo"].name) handler = ImageHandlerUserPhoto() handler.load(filename, data["photo"]) handler.save_thumbnails("PNG") self.instance.photo = filename self.instance.friendly_name = data.get("friendly_name") self.instance.birth_date = data["birth_date"] self.instance.save()
class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) friendly_name = models.CharField(_('Friendly Name'), max_length=64, null=True, blank=True) address = models.ForeignKey(Location, verbose_name=_("Location"), null=True, blank=True) photo = models.CharField(_("Photo"), max_length=255, null=True, blank=True) birth_date = models.DateField(_("Birth Date"), null=True, blank=True) def __init__(self, *args, **kwargs): super(UserProfile, self).__init__(*args, **kwargs) self.image_handler = ImageHandlerUserPhoto() if self.photo: self.image_handler.load(self.photo) @property def unread_notifications(self): return self.user.notifications.filter(read=False) @property def huge_url(self): if self.photo: return self.image_handler.url('huge') else: return "/media/img/user.huge.png" @property def thumb_url(self): if self.photo: return self.image_handler.url('thumb') else: return "/media/img/user.thumb.png" @property def icon_url(self): if self.photo: return self.image_handler.url('icon') else: return "/media/img/user.icon.png" @classmethod def pre_delete(cls, instance, **kwargs): instance.image_handler.delete()
def __init__(self, *args, **kwargs): super(UserProfile, self).__init__(*args, **kwargs) self.image_handler = ImageHandlerUserPhoto() if self.photo: self.image_handler.load(self.photo)