From b5fa7bed09ad2479b31f664f5f0e9804302d65f2 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 26 Oct 2024 00:27:05 +0000 Subject: [PATCH] Ensure links are prepended with base URL on receipt --- src/backend/InvenTree/InvenTree/settings.py | 3 +- .../0010_migrate_currency_setting.py | 2 +- .../migrations/0031_auto_20241026_0024.py | 39 +++++++++++++++++++ src/backend/InvenTree/common/tasks.py | 4 ++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/backend/InvenTree/common/migrations/0031_auto_20241026_0024.py diff --git a/src/backend/InvenTree/InvenTree/settings.py b/src/backend/InvenTree/InvenTree/settings.py index 984a2027b5af..b7f7b97db371 100644 --- a/src/backend/InvenTree/InvenTree/settings.py +++ b/src/backend/InvenTree/InvenTree/settings.py @@ -32,7 +32,8 @@ checkMinPythonVersion() -INVENTREE_NEWS_URL = 'https://inventree.org/news/feed.atom' +INVENTREE_BASE_URL = 'https://inventree.org' +INVENTREE_NEWS_URL = f'{INVENTREE_BASE_URL}/news/feed.atom' # Determine if we are running in "test" mode e.g. "manage.py test" TESTING = 'test' in sys.argv or 'TESTING' in os.environ diff --git a/src/backend/InvenTree/common/migrations/0010_migrate_currency_setting.py b/src/backend/InvenTree/common/migrations/0010_migrate_currency_setting.py index 116d139be884..8d84505f876f 100644 --- a/src/backend/InvenTree/common/migrations/0010_migrate_currency_setting.py +++ b/src/backend/InvenTree/common/migrations/0010_migrate_currency_setting.py @@ -29,5 +29,5 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunPython(set_default_currency), + migrations.RunPython(set_default_currency, reverse_code=migrations.RunPython.noop), ] diff --git a/src/backend/InvenTree/common/migrations/0031_auto_20241026_0024.py b/src/backend/InvenTree/common/migrations/0031_auto_20241026_0024.py new file mode 100644 index 000000000000..75b6c86a672d --- /dev/null +++ b/src/backend/InvenTree/common/migrations/0031_auto_20241026_0024.py @@ -0,0 +1,39 @@ +# Generated by Django 4.2.16 on 2024-10-26 00:24 + +from django.conf import settings +from django.db import migrations + +import logging + +logger = logging.getLogger('inventree') + + +def update_news_feed_urls(apps, schema_editor): + """Update and validate the news feed URLs.""" + + from common.models import NewsFeedEntry + + n = 0 + + for entry in NewsFeedEntry.objects.all(): + if entry.link and entry.link.startswith('/'): + entry.link = settings.INVENTREE_BASE_URL + entry.link + entry.save() + n += 1 + + if n > 0: + logger.info("Updated link for %s NewsFeedEntry objects", n) + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0030_barcodescanresult'), + ] + + operations = [ + migrations.RunPython( + update_news_feed_urls, + reverse_code=migrations.RunPython.noop + ) + ] diff --git a/src/backend/InvenTree/common/tasks.py b/src/backend/InvenTree/common/tasks.py index ffb67311b9f5..fb989ed7f918 100644 --- a/src/backend/InvenTree/common/tasks.py +++ b/src/backend/InvenTree/common/tasks.py @@ -70,6 +70,10 @@ def update_news_feed(): if entry.id in id_list: continue + # Enforce proper links for the entries + if entry.link.startswith('/'): + entry.link = settings.INVENTREE_BASE_URL + entry + # Create entry try: NewsFeedEntry.objects.create(