From 165c997838e6f5c28eab51b3b5a25350cfde49b7 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Sat, 11 Nov 2023 21:25:26 +0100 Subject: [PATCH] =?UTF-8?q?si=20l'image=20est=20donn=C3=A9e=20par=20url,?= =?UTF-8?q?=20on=20la=20r=C3=A9cup=C3=A8re=20sur=20notre=20serveur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.prod.yml | 2 ++ src/agenda_culturel/extractors.py | 32 +++++++++++++++++++ .../locale/fr/LC_MESSAGES/django.po | 8 ++++- src/agenda_culturel/models.py | 3 ++ .../event-in-list-by-day-inc.html | 10 +++--- .../single-event/event-in-list-inc.html | 10 +++--- .../single-event/event-single-inc.html | 6 ++-- 7 files changed, 57 insertions(+), 14 deletions(-) diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 00c0384..d6b1e1d 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -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 diff --git a/src/agenda_culturel/extractors.py b/src/agenda_culturel/extractors.py index 2a92722..1f0e9ef 100644 --- a/src/agenda_culturel/extractors.py +++ b/src/agenda_culturel/extractors.py @@ -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]) diff --git a/src/agenda_culturel/locale/fr/LC_MESSAGES/django.po b/src/agenda_culturel/locale/fr/LC_MESSAGES/django.po index 7a97c7b..d74b079 100644 --- a/src/agenda_culturel/locale/fr/LC_MESSAGES/django.po +++ b/src/agenda_culturel/locale/fr/LC_MESSAGES/django.po @@ -217,4 +217,10 @@ msgid "Text as shown to the visitors" msgstr "Text tel que présenté aux visiteureuses" msgid "Content" -msgstr "Contenu" \ No newline at end of file +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" \ No newline at end of file diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 3759510..059eae5 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -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.")) diff --git a/src/agenda_culturel/templates/agenda_culturel/single-event/event-in-list-by-day-inc.html b/src/agenda_culturel/templates/agenda_culturel/single-event/event-in-list-by-day-inc.html index 2a8b780..03bfe72 100644 --- a/src/agenda_culturel/templates/agenda_culturel/single-event/event-in-list-by-day-inc.html +++ b/src/agenda_culturel/templates/agenda_culturel/single-event/event-in-list-by-day-inc.html @@ -35,11 +35,11 @@

{% endif %} - {% if event.image %} -
- {{ event.image_alt }} -
- {% endif %} + {% if event.image or event.local_image %} +
+ {{ event.image_alt }} +
+ {% endif %}

{{ event.description |truncatewords:20 |linebreaks }}

diff --git a/src/agenda_culturel/templates/agenda_culturel/single-event/event-in-list-inc.html b/src/agenda_culturel/templates/agenda_culturel/single-event/event-in-list-inc.html index 8cb7ed7..bb20d80 100644 --- a/src/agenda_culturel/templates/agenda_culturel/single-event/event-in-list-inc.html +++ b/src/agenda_culturel/templates/agenda_culturel/single-event/event-in-list-inc.html @@ -29,11 +29,11 @@

{% endif %} - {% if event.image %} -
- {{ event.image_alt }} -
- {% endif %} + {% if event.image or event.local_image %} +
+ {{ event.image_alt }} +
+ {% endif %}

{{ event.description |truncatewords:20 |linebreaks }}

diff --git a/src/agenda_culturel/templates/agenda_culturel/single-event/event-single-inc.html b/src/agenda_culturel/templates/agenda_culturel/single-event/event-single-inc.html index eb55d01..b0fd3c7 100644 --- a/src/agenda_culturel/templates/agenda_culturel/single-event/event-single-inc.html +++ b/src/agenda_culturel/templates/agenda_culturel/single-event/event-single-inc.html @@ -19,9 +19,9 @@

- {% if event.image %} -
- {{ event.image_alt }} + {% if event.image or event.local_image %} +
+ {{ event.image_alt }}
{% endif %}