diff --git a/src/agenda_culturel/extractors.py b/src/agenda_culturel/extractors.py
index 1b1230b..de4c123 100644
--- a/src/agenda_culturel/extractors.py
+++ b/src/agenda_culturel/extractors.py
@@ -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])
diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py
index 8d590b9..68ca3cf 100644
--- a/src/agenda_culturel/models.py
+++ b/src/agenda_culturel/models.py
@@ -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):
diff --git a/src/agenda_culturel/templates/agenda_culturel/event_form.html b/src/agenda_culturel/templates/agenda_culturel/event_form.html
index 0473da3..b4c687e 100644
--- a/src/agenda_culturel/templates/agenda_culturel/event_form.html
+++ b/src/agenda_culturel/templates/agenda_culturel/event_form.html
@@ -8,8 +8,8 @@
-
-
+
+
{% endblock %}