Amélioration de la gestion des images:
- téléchargement des images si elles sont manquantes - utilisation d'un nom de fichier local pour éviter les collisions - mise à jour des images lors de la mise à jour d'un événement
This commit is contained in:
parent
0d62660c2b
commit
84ce4b0d7d
@ -13,6 +13,8 @@ from django.core.cache import cache
|
|||||||
from django.core.cache.utils import make_template_fragment_key
|
from django.core.cache.utils import make_template_fragment_key
|
||||||
from django.contrib.auth.models import User, AnonymousUser
|
from django.contrib.auth.models import User, AnonymousUser
|
||||||
import emoji
|
import emoji
|
||||||
|
from django.core.files.storage import default_storage
|
||||||
|
import uuid
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import urllib.request
|
import urllib.request
|
||||||
@ -935,19 +937,28 @@ class Event(models.Model):
|
|||||||
def is_representative(self):
|
def is_representative(self):
|
||||||
return self.other_versions is None or self.other_versions.representative == self
|
return self.other_versions is None or self.other_versions.representative == self
|
||||||
|
|
||||||
|
def download_missing_image(self):
|
||||||
|
if self.local_image and not default_storage.exists(self.local_image.name):
|
||||||
|
logger.warning("on dl")
|
||||||
|
self.download_image()
|
||||||
|
self.save(update_fields=["local_image"])
|
||||||
|
|
||||||
def download_image(self):
|
def download_image(self):
|
||||||
# first download file
|
# first download file
|
||||||
|
|
||||||
a = urlparse(self.image)
|
a = urlparse(self.image)
|
||||||
basename = os.path.basename(a.path)
|
basename = os.path.basename(a.path)
|
||||||
|
|
||||||
|
ext = basename.split('.')[-1]
|
||||||
|
filename = "%s.%s" % (uuid.uuid4(), ext)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tmpfile, _ = urllib.request.urlretrieve(self.image)
|
tmpfile, _ = urllib.request.urlretrieve(self.image)
|
||||||
except:
|
except:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# if the download is ok, then create the corresponding file object
|
# if the download is ok, then create the corresponding file object
|
||||||
self.local_image = File(name=basename, file=open(tmpfile, "rb"))
|
self.local_image = File(name=filename, file=open(tmpfile, "rb"))
|
||||||
|
|
||||||
def add_pending_organisers(self, organisers):
|
def add_pending_organisers(self, organisers):
|
||||||
self.pending_organisers = organisers
|
self.pending_organisers = organisers
|
||||||
@ -1114,7 +1125,7 @@ class Event(models.Model):
|
|||||||
self.update_recurrence_dtstartend()
|
self.update_recurrence_dtstartend()
|
||||||
|
|
||||||
# if the image is defined but not locally downloaded
|
# if the image is defined but not locally downloaded
|
||||||
if self.image and not self.local_image:
|
if self.image and (not self.local_image or not default_storage.exists(self.local_image.name)):
|
||||||
self.download_image()
|
self.download_image()
|
||||||
|
|
||||||
# remove "/" from tags
|
# remove "/" from tags
|
||||||
@ -1170,7 +1181,6 @@ class Event(models.Model):
|
|||||||
for is_auth in [False, True]:
|
for is_auth in [False, True]:
|
||||||
key = make_template_fragment_key("event_body", [is_auth, self])
|
key = make_template_fragment_key("event_body", [is_auth, self])
|
||||||
cache.delete(key)
|
cache.delete(key)
|
||||||
logger.warning("on passe par le save")
|
|
||||||
|
|
||||||
# then if its a clone, update the representative
|
# then if its a clone, update the representative
|
||||||
if clone:
|
if clone:
|
||||||
@ -1564,9 +1574,14 @@ class Event(models.Model):
|
|||||||
same_imported.other_versions.representative = None
|
same_imported.other_versions.representative = None
|
||||||
same_imported.other_versions.save()
|
same_imported.other_versions.save()
|
||||||
# we only update local information if it's a pure import and has no moderated_date
|
# we only update local information if it's a pure import and has no moderated_date
|
||||||
|
new_image = same_imported.image != event.image
|
||||||
same_imported.update(event, pure and same_imported.moderated_date is None)
|
same_imported.update(event, pure and same_imported.moderated_date is None)
|
||||||
same_imported.set_in_importation_process()
|
same_imported.set_in_importation_process()
|
||||||
same_imported.prepare_save()
|
same_imported.prepare_save()
|
||||||
|
# fix missing or updated files
|
||||||
|
if same_imported.local_image and (not default_storage.exists(same_imported.local_image.name) or new_image):
|
||||||
|
same_imported.download_image()
|
||||||
|
same_imported.save(update_fields=["local_image"])
|
||||||
to_update.append(same_imported)
|
to_update.append(same_imported)
|
||||||
else:
|
else:
|
||||||
# otherwise, the new event possibly a duplication of the remaining others.
|
# otherwise, the new event possibly a duplication of the remaining others.
|
||||||
|
@ -496,6 +496,8 @@ class EventDetailView(UserPassesTestMixin, DetailView, ModelFormMixin):
|
|||||||
|
|
||||||
def get_object(self):
|
def get_object(self):
|
||||||
o = super().get_object()
|
o = super().get_object()
|
||||||
|
logger.warning(">>>> details")
|
||||||
|
o.download_missing_image()
|
||||||
y = self.kwargs["year"]
|
y = self.kwargs["year"]
|
||||||
m = self.kwargs["month"]
|
m = self.kwargs["month"]
|
||||||
d = self.kwargs["day"]
|
d = self.kwargs["day"]
|
||||||
@ -516,7 +518,6 @@ class EventDetailView(UserPassesTestMixin, DetailView, ModelFormMixin):
|
|||||||
return self.form_invalid(form)
|
return self.form_invalid(form)
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
logger.warning("on form valide")
|
|
||||||
message = form.save(commit=False)
|
message = form.save(commit=False)
|
||||||
message.user = self.request.user
|
message.user = self.request.user
|
||||||
message.related_event = self.get_object()
|
message.related_event = self.get_object()
|
||||||
@ -524,7 +525,6 @@ class EventDetailView(UserPassesTestMixin, DetailView, ModelFormMixin):
|
|||||||
message.spam = False
|
message.spam = False
|
||||||
message.closed = True
|
message.closed = True
|
||||||
message.save()
|
message.save()
|
||||||
logger.warning("on save " + str(message))
|
|
||||||
|
|
||||||
|
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user