On corrige un bug dans l'import des événements, au moment de sauver les images

This commit is contained in:
Jean-Marie Favreau 2023-12-17 16:13:32 +01:00
parent 39d8b2a8c1
commit 4515264dfc
3 changed files with 25 additions and 21 deletions

View File

@ -7,7 +7,6 @@ from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import urllib.request
from django.core.files.uploadedfile import SimpleUploadedFile
from tempfile import NamedTemporaryFile
from urllib.parse import urlparse
import os
@ -55,22 +54,6 @@ class Extractor:
return None
def guess_filename(url):
a = urlparse(url)
return os.path.basename(a.path)
def download_media(url):
# first download file
basename = Extractor.guess_filename(url)
try:
tmpfile, _ = urllib.request.urlretrieve(url)
except:
return None
# if the download is ok, then create create the corresponding file object
return SimpleUploadedFile(basename, open(tmpfile, "rb").read())
@ -210,7 +193,6 @@ class ExtractorFacebook(Extractor):
from .models import Event
image = self.get_element("image")
local_image = None if image is None else Extractor.download_media(image)
return Event(title=self.get_element("name"),
@ -221,7 +203,6 @@ class ExtractorFacebook(Extractor):
end_time=self.get_element_time("end_timestamp"),
location=self.get_element("event_place_name"),
description=self.get_element("description"),
local_image=local_image,
image=self.get_element("image"),
image_alt=self.get_element("image_alt"),
reference_urls=[url])

View File

@ -5,6 +5,10 @@ from django.template.defaultfilters import slugify
from django.urls import reverse
from colorfield.fields import ColorField
from ckeditor.fields import RichTextField
from urllib.parse import urlparse
import urllib.request
import os
from django.core.files import File
from django.template.defaultfilters import date as _date
@ -175,6 +179,25 @@ class Event(models.Model):
def nb_draft_events():
return Event.objects.filter(status=Event.STATUS.DRAFT).count()
def save(self, *args, **kwargs):
# if the image is defined but not locally downloaded
if self.image and not self.local_image:
# first download file
a = urlparse(self.image)
basename = os.path.basename(a.path)
try:
tmpfile, _ = urllib.request.urlretrieve(self.image)
except:
return None
# if the download is ok, then create the corresponding file object
self.local_image = File(name=basename, file=open(tmpfile, "rb"))
super().save(*args, **kwargs)
class ContactMessage(models.Model):

View File

@ -8,8 +8,8 @@
<script>window.CKEDITOR_BASEPATH = '/static/ckeditor/ckeditor/';</script>
<script src="/static/admin/js/vendor/jquery/jquery.js"></script>
<script src="/static/admin/js/jquery.init.js"></script>
<link href="{% static 'js/css/django_better_admin_arrayfield.min.css' %}" type="text/css" media="all" rel="stylesheet">
<script src="{% static 'js/js/django_better_admin_arrayfield.min.js' %}"></script>
<link href="{% static 'css/django_better_admin_arrayfield.min.css' %}" type="text/css" media="all" rel="stylesheet">
<script src="{% static 'js/django_better_admin_arrayfield.min.js' %}"></script>
<script src="{% static 'js/adjust_datetimes.js' %}"></script>
{% endblock %}