From a15c19bdabdec9a7ec99c96132c63ce6fe463256 Mon Sep 17 00:00:00 2001 From: Lova Andriarimalala <43842786+Xpirix@users.noreply.github.com> Date: Tue, 5 Nov 2024 09:27:41 +0300 Subject: [PATCH] Transform the get sustaining members tag to a scheduled job --- .gitignore | 5 ++- qgis-app/plugins/tasks/__init__.py | 1 + .../plugins/tasks/get_sustaining_members.py | 32 +++++++++++++++++++ qgis-app/plugins/templatetags/plugin_utils.py | 21 ++++-------- qgis-app/settings_docker.py | 6 +++- 5 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 qgis-app/plugins/tasks/get_sustaining_members.py diff --git a/.gitignore b/.gitignore index a6725166..e002008d 100644 --- a/.gitignore +++ b/.gitignore @@ -60,4 +60,7 @@ docker-compose.override.yml node_modules qgis-app/static/bundles qgis-app/package-lock.json -qgis-app/webpack-stats.json \ No newline at end of file +qgis-app/webpack-stats.json + +# Sustaining members template +qgis-app/templates/flatpages/sustaining_members.html \ No newline at end of file diff --git a/qgis-app/plugins/tasks/__init__.py b/qgis-app/plugins/tasks/__init__.py index 371b383f..a3a8ec25 100644 --- a/qgis-app/plugins/tasks/__init__.py +++ b/qgis-app/plugins/tasks/__init__.py @@ -1,3 +1,4 @@ from plugins.tasks.generate_plugins_xml import * # noqa from plugins.tasks.update_qgis_versions import * # noqa from plugins.tasks.rebuild_search_index import * # noqa +from plugins.tasks.get_sustaining_members import * # noqa diff --git a/qgis-app/plugins/tasks/get_sustaining_members.py b/qgis-app/plugins/tasks/get_sustaining_members.py new file mode 100644 index 00000000..95f2e43b --- /dev/null +++ b/qgis-app/plugins/tasks/get_sustaining_members.py @@ -0,0 +1,32 @@ +from celery import shared_task +from celery.utils.log import get_task_logger +import requests +from bs4 import BeautifulSoup +from django.conf import settings +import os + +logger = get_task_logger(__name__) + +@shared_task +def get_sustaining_members(): + """ + Get the Sustaining members HTML section from the new website + """ + try: + url = 'https://qgis.org' + response = requests.get(url) + response.raise_for_status() + soup = BeautifulSoup(response.text, 'html.parser') + + # Extract the section by the specified class name + section = soup.select_one('section.section') + + if section: + template_path = os.path.join(settings.SITE_ROOT, 'templates/flatpages/sustaining_members.html') + with open(template_path, 'w') as f: + f.write(section.prettify()) + logger.info(f"get_sustaining_members: Section saved to {template_path}") + else: + logger.info("get_sustaining_members: Section not found") + except requests.RequestException as e: + logger.info(f"get_sustaining_members: {e}") \ No newline at end of file diff --git a/qgis-app/plugins/templatetags/plugin_utils.py b/qgis-app/plugins/templatetags/plugin_utils.py index 2ab7bd45..532a0da9 100755 --- a/qgis-app/plugins/templatetags/plugin_utils.py +++ b/qgis-app/plugins/templatetags/plugin_utils.py @@ -101,20 +101,11 @@ def version_tag(context): @register.simple_tag def get_sustaining_members_section(): """ - Get the Sustaining members HTML section from the new website + Get the Sustaining members HTML from the template file """ + template_path = os.path.join(settings.SITE_ROOT, 'templates/flatpages/sustaining_members.html') try: - url = 'https://qgis.org' - response = requests.get(url) - response.raise_for_status() - soup = BeautifulSoup(response.text, 'html.parser') - - # Extract the section by the specified class name - section = soup.select_one('section.section') - - if section: - return section.prettify() # Returning HTML content - else: - return "Section not found" - except requests.RequestException as e: - return f"Error: {e}" \ No newline at end of file + with open(template_path, 'r') as f: + return f.read() + except FileNotFoundError: + return "" diff --git a/qgis-app/settings_docker.py b/qgis-app/settings_docker.py index 7b87c55e..73db6038 100644 --- a/qgis-app/settings_docker.py +++ b/qgis-app/settings_docker.py @@ -150,7 +150,11 @@ 'rebuild_search_index': { 'task': 'plugins.tasks.rebuild_search_index.rebuild_search_index', 'schedule': crontab(minute=0, hour=3), # Execute every day at 3 AM. - } + }, + 'get_sustaining_members': { + 'task': 'plugins.tasks.get_sustaining_members.get_sustaining_members', + 'schedule': crontab(minute='*/30'), # Execute every 30 minutes. + }, } # Set plugin token access and refresh validity to a very long duration SIMPLE_JWT = {