Skip to content

Commit

Permalink
Transform the get sustaining members tag to a scheduled job
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpirix committed Nov 5, 2024
1 parent 62e55b1 commit a15c19b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ docker-compose.override.yml
node_modules
qgis-app/static/bundles
qgis-app/package-lock.json
qgis-app/webpack-stats.json
qgis-app/webpack-stats.json

# Sustaining members template
qgis-app/templates/flatpages/sustaining_members.html
1 change: 1 addition & 0 deletions qgis-app/plugins/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions qgis-app/plugins/tasks/get_sustaining_members.py
Original file line number Diff line number Diff line change
@@ -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}")
21 changes: 6 additions & 15 deletions qgis-app/plugins/templatetags/plugin_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
with open(template_path, 'r') as f:
return f.read()
except FileNotFoundError:
return ""
6 changes: 5 additions & 1 deletion qgis-app/settings_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit a15c19b

Please sign in to comment.