From 486d4e23e541ea4cd9fbb2e817b996c5772300f2 Mon Sep 17 00:00:00 2001 From: afabiani Date: Mon, 10 Feb 2020 12:26:17 +0100 Subject: [PATCH] [Fixes #7] Users Avatar and Groups Logo appear as broken img --- avatar/models.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/avatar/models.py b/avatar/models.py index 90bafaa..afaf396 100644 --- a/avatar/models.py +++ b/avatar/models.py @@ -2,6 +2,7 @@ import os import io import hashlib +from shutil import copyfile from django.db import models from django.conf import settings @@ -111,6 +112,16 @@ def create_thumbnail(self, size, quality=None): thumb = self.avatar.storage.save(self.avatar_name(size), thumb_file) def avatar_url(self, size): + try: + _base_path = os.path.split(self.avatar_name(size))[0] + _upload_path = os.path.split( + self.avatar.storage.url(self.avatar_name(size)))[1] + _upload_path = os.path.join(_base_path, _upload_path) + if not self.avatar.storage.exists(_upload_path): + copyfile(self.avatar.storage.path(self.avatar_name(size)), + self.avatar.storage.path(_upload_path)) + except BaseException: + pass return self.avatar.storage.url(self.avatar_name(size)) def avatar_name(self, size):