From 29a257241cb4cb907daaff45f840a86b7d140975 Mon Sep 17 00:00:00 2001 From: Wassilios Lytras Date: Wed, 18 Sep 2024 10:45:57 +0200 Subject: [PATCH 1/2] In Django 4.2.18, the filepath for storing the files is not allowed to be an absolute path. --- pyas2/models.py | 28 ++++++++++++++++++++-------- pyas2/settings.py | 9 +++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/pyas2/models.py b/pyas2/models.py index 7463cdf..20dd338 100644 --- a/pyas2/models.py +++ b/pyas2/models.py @@ -6,6 +6,7 @@ from email.parser import HeaderParser from uuid import uuid4 +import django import requests from django.core.files.base import ContentFile from django.core.files.storage import default_storage @@ -24,6 +25,21 @@ from pyas2 import settings from pyas2.utils import run_post_send +# Check if running Django >= 4.2 +if django.VERSION >= (4, 2): + try: + from django.core.files.storage import storages + + as2files_storage = storages[ + "as2files" + ] # Use 'as2files' storage if defined in Django 4.2+ + except KeyError: + # If 'as2files' is not configured, fallback to default storage + as2files_storage = default_storage +else: + # In Django 4.1 or lower, fallback to default storage + as2files_storage = default_storage + logger = logging.getLogger("pyas2") @@ -349,18 +365,14 @@ def create_from_as2message( # Save the payload to the inbox folder full_filename = None if direction == "IN" and status == "S": - if settings.DATA_DIR: - dirname = os.path.join( - settings.DATA_DIR, "messages", organization, "inbox", partner - ) - else: - dirname = os.path.join("messages", organization, "inbox", partner) + dirname = os.path.join("messages", organization, "inbox", partner) if not message.partner.keep_filename or not filename: filename = f"{message.message_id}.msg" - full_filename = default_storage.generate_filename( + + full_filename = as2files_storage.generate_filename( posixpath.join(dirname, filename) ) - default_storage.save(name=full_filename, content=ContentFile(payload)) + as2files_storage.save(name=full_filename, content=ContentFile(payload)) return message, full_filename diff --git a/pyas2/settings.py b/pyas2/settings.py index 1977b56..33c9ae2 100644 --- a/pyas2/settings.py +++ b/pyas2/settings.py @@ -1,5 +1,6 @@ import os +import django from django.conf import settings APP_SETTINGS = getattr(settings, "PYAS2", {}) @@ -20,3 +21,11 @@ # Max number of days worth of messages to be saved in archive MAX_ARCH_DAYS = APP_SETTINGS.get("MAX_ARCH_DAYS", 30) + +if django.VERSION >= (4, 2) and "as2files" not in settings.STORAGES: + settings.STORAGES["as2files"] = { + "BACKEND": "django.core.files.storage.FileSystemStorage", + "OPTIONS": { + "location": DATA_DIR, + }, + } From 8b2ff9af9aacb5506ccc7e18a4d4383dfbef97f1 Mon Sep 17 00:00:00 2001 From: Wassilios Lytras Date: Wed, 18 Sep 2024 12:01:41 +0200 Subject: [PATCH 2/2] sorting imports --- pyas2/models.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pyas2/models.py b/pyas2/models.py index 20dd338..cf2a8a9 100644 --- a/pyas2/models.py +++ b/pyas2/models.py @@ -13,13 +13,10 @@ from django.db import models from django.utils import timezone from django.utils.translation import gettext as _ - -from pyas2lib import ( - Mdn as As2Mdn, - Message as As2Message, - Organization as As2Organization, - Partner as As2Partner, -) +from pyas2lib import Mdn as As2Mdn +from pyas2lib import Message as As2Message +from pyas2lib import Organization as As2Organization +from pyas2lib import Partner as As2Partner from pyas2lib.utils import extract_certificate_info from pyas2 import settings @@ -28,7 +25,7 @@ # Check if running Django >= 4.2 if django.VERSION >= (4, 2): try: - from django.core.files.storage import storages + from django.core.files.storage import storages # noqa: E0611 as2files_storage = storages[ "as2files"