si l'image est donnée par url, on la récupère sur notre serveur

This commit is contained in:
Jean-Marie Favreau 2023-11-11 21:25:26 +01:00
parent fd9afa14e9
commit 165c997838
7 changed files with 57 additions and 14 deletions

View File

@ -59,6 +59,8 @@ services:
volumes:
- ./src:/usr/src/app/
- ./deployment/scripts:/app/deployment/scripts/
- static_files:/usr/src/app/static
- media_files:/usr/src/app/media
env_file: .env
depends_on:
- db

View File

@ -1,10 +1,17 @@
from abc import ABC, abstractmethod
from django.db import models
from selenium import webdriver
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
from bs4 import BeautifulSoup
import json
@ -40,6 +47,26 @@ class Extractor:
logger.error(e)
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())
class ExtractorFacebook(Extractor):
class SimpleFacebookEvent:
@ -164,6 +191,10 @@ class ExtractorFacebook(Extractor):
def build_event(self, url):
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"),
status=Event.STATUS.DRAFT,
start_day=self.get_element_datetime("start_timestamp"),
@ -172,6 +203,7 @@ class ExtractorFacebook(Extractor):
end_time=self.get_element_datetime("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

@ -218,3 +218,9 @@ msgstr "Text tel que présenté aux visiteureuses"
msgid "Content"
msgstr "Contenu"
msgid "Illustration (local image)"
msgstr "Illustration (image locale)"
msgid "Illustration image stored in the agenda server"
msgstr "Image d'illustration stockée sur le serveur de l'agenda"

View File

@ -107,6 +107,8 @@ class Event(models.Model):
description = models.TextField(verbose_name=_('Description'), help_text=_('General description of the event'), blank=True, null=True)
local_image = models.ImageField(verbose_name=_('Illustration (local image)'), help_text=_("Illustration image stored in the agenda server"), max_length=1024, blank=True, null=True)
image = models.URLField(verbose_name=_('Illustration'), help_text=_("URL of the illustration image"), max_length=1024, blank=True, null=True)
image_alt = models.CharField(verbose_name=_('Illustration description'), help_text=_('Alternative text used by screen readers for the image'), blank=True, null=True, max_length=1024)
@ -145,6 +147,7 @@ class Event(models.Model):
return self.status == Event.STATUS.TRASH
class EventSubmissionForm(models.Model):
url = models.URLField(max_length=512, verbose_name=_('URL'), help_text=_("URL where this event can be found."))

View File

@ -35,9 +35,9 @@
</em></p>
{% endif %}
{% if event.image %}
{% if event.image or event.local_image %}
<article class='illustration-small'>
<img src="{{ event.image }}" alt="{{ event.image_alt }}" />
<img src="{% if event.local_image %}{{ event.local_image.url }}{% else %}{{ event.image }}{% endif %}" alt="{{ event.image_alt }}" />
</article>
{% endif %}

View File

@ -29,9 +29,9 @@
</em></p>
{% endif %}
{% if event.image %}
<article class='illustration{% if display in "in list by day" %}-small{% endif %}'>
<img src="{{ event.image }}" alt="{{ event.image_alt }}" />
{% if event.image or event.local_image %}
<article class='illustration'>
<img src="{% if event.local_image %}{{ event.local_image.url }}{% else %}{{ event.image }}{% endif %}" alt="{{ event.image_alt }}" />
</article>
{% endif %}

View File

@ -19,9 +19,9 @@
</p>
</header>
{% if event.image %}
<article class='illustration{% if display in "in list by day" %}-small{% endif %}'>
<img src="{{ event.image }}" alt="{{ event.image_alt }}" />
{% if event.image or event.local_image %}
<article class='illustration'>
<img src="{% if event.local_image %}{{ event.local_image.url }}{% else %}{{ event.image }}{% endif %}" alt="{{ event.image_alt }}" />
</article>
{% endif %}