diff --git a/quotes/email_utils.py b/quotes/email_utils.py index aa23c07..b808c0f 100644 --- a/quotes/email_utils.py +++ b/quotes/email_utils.py @@ -7,6 +7,64 @@ from django.template.loader import render_to_string from django.conf import settings +def get_file_urls_with_base_url(files): + """ + Get absolute URLs for files. + Constructs full URLs using ALLOWED_HOSTS from settings. + """ + # Get base URL from ALLOWED_HOSTS + allowed_hosts = getattr(settings, "ALLOWED_HOSTS", []) + if allowed_hosts and allowed_hosts[0] != "*": + # Use first allowed host + domain = allowed_hosts[0] + # Check if it's localhost or IP for HTTP + if ( + domain.startswith("127.0.0.1") + or domain.startswith("localhost") + or domain.startswith("10.") + ): + # In DEBUG mode, add port 8000 (default Django dev server) + if settings.DEBUG: + base_url = f"http://{domain}:8000" + else: + base_url = f"http://{domain}" + else: + base_url = f"https://{domain}" + else: + # Fallback: use localhost with port in DEBUG mode + if settings.DEBUG: + base_url = "http://127.0.0.1:8000" + else: + base_url = "" + + file_urls = [] + for file in files: + # In DEBUG mode, use MEDIA_URL (WhiteNoise serves it) + # In production, use /uploads/ (nginx serves it) + if settings.DEBUG: + # Use MEDIA_URL which is "media/" + media_url = settings.MEDIA_URL.rstrip("/") + file_path = f"/{media_url}/{file.path}" + else: + # Use /uploads/ for nginx + file_path = f"/uploads/{file.path}" + + if base_url: + # Make it an absolute URL + file_url = base_url + file_path + else: + # Fallback: relative URL + file_url = file_path + + file_urls.append( + { + "file": file, + "url": file_url, + } + ) + return file_urls + + def send_submission_emails(submission): """ Send emails for a quote submission. @@ -17,6 +75,7 @@ def send_submission_emails(submission): Returns tuple (owner_sent, submitter_sent) """ files = submission.files.all() + file_urls = get_file_urls_with_base_url(files) owner_email = getattr(settings, "OWNER_EMAIL", "") owner_sent = False @@ -31,6 +90,7 @@ def send_submission_emails(submission): { "submission": submission, "files": files, + "file_urls": file_urls, }, ) @@ -47,8 +107,11 @@ Description: Files uploaded: {files.count()} """ - for file in files: + for file_url_data in file_urls: + file = file_url_data["file"] + url = file_url_data["url"] text_message += f"- {file.original_filename} ({file.file_size} bytes)\n" + text_message += f" Download: {url}\n" msg = EmailMultiAlternatives( subject=subject, @@ -70,6 +133,7 @@ Files uploaded: {files.count()} { "submission": submission, "files": files, + "file_urls": file_urls, }, ) @@ -88,8 +152,11 @@ Your Notes: Files uploaded: {files.count()} """ - for file in files: + for file_url_data in file_urls: + file = file_url_data["file"] + url = file_url_data["url"] text_message += f"- {file.original_filename} ({file.file_size} bytes)\n" + text_message += f" Download: {url}\n" text_message += f"\nWe will contact you at {submission.email} regarding your quote request.\n" text_message += f"\nThis submission will be kept on file until {submission.expiration_time.strftime('%B %d, %Y')}." diff --git a/templates/emails/submission_confirmation.html b/templates/emails/submission_confirmation.html index 94349ad..35f04e7 100644 --- a/templates/emails/submission_confirmation.html +++ b/templates/emails/submission_confirmation.html @@ -49,6 +49,16 @@ border-radius: 3px; } + .file-list li a { + color: #0066cc; + text-decoration: none; + font-weight: bold; + } + + .file-list li a:hover { + text-decoration: underline; + } + .footer { text-align: center; color: #666; @@ -84,8 +94,11 @@
Files You Uploaded ({{ files|length }}):
diff --git a/templates/emails/submission_notification_owner.html b/templates/emails/submission_notification_owner.html index 495e209..ac7be1b 100644 --- a/templates/emails/submission_notification_owner.html +++ b/templates/emails/submission_notification_owner.html @@ -49,6 +49,16 @@ border-radius: 3px; } + .file-list li a { + color: #0066cc; + text-decoration: none; + font-weight: bold; + } + + .file-list li a:hover { + text-decoration: underline; + } + .footer { text-align: center; color: #666; @@ -84,8 +94,11 @@
Uploaded Files ({{ files|length }}):