Boilerplate yield and back and forth
This commit is contained in:
81
seduttomachineworks_project/quote_submit.py
Normal file
81
seduttomachineworks_project/quote_submit.py
Normal file
@@ -0,0 +1,81 @@
|
||||
import json
|
||||
import time
|
||||
from pathlib import Path
|
||||
from django.http import StreamingHttpResponse, JsonResponse
|
||||
from django.conf import settings
|
||||
from django.views.decorators.clickjacking import xframe_options_exempt
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
|
||||
def send_progress_update(step, message, progress):
|
||||
"""Helper to send a progress update as JSON"""
|
||||
return json.dumps({"step": step, "message": message, "progress": progress}) + "\n"
|
||||
|
||||
|
||||
@xframe_options_exempt
|
||||
@csrf_exempt
|
||||
def submit_quote(request):
|
||||
"""
|
||||
Handle quote submission with progress updates.
|
||||
Steps:
|
||||
1. Upload files
|
||||
2. Save files
|
||||
3. Send email
|
||||
4. Verify email sent
|
||||
"""
|
||||
if request.method != "POST":
|
||||
return JsonResponse({"error": "Method not allowed"}, status=405)
|
||||
|
||||
# Read form data first (files need to be read from request)
|
||||
email = request.POST.get("email", "").strip()
|
||||
notes = request.POST.get("notes", "").strip()
|
||||
files = request.FILES.getlist("files")
|
||||
|
||||
def process_quote():
|
||||
try:
|
||||
# Step 1: Upload files
|
||||
yield send_progress_update(1, "Uploading files...", 25)
|
||||
# Files are already uploaded at this point, just acknowledge
|
||||
uploaded_files = []
|
||||
for file in files:
|
||||
uploaded_files.append({"name": file.name, "size": file.size})
|
||||
|
||||
# Step 2: Save files
|
||||
yield send_progress_update(2, "Saving files...", 50)
|
||||
# Save files to disk/storage
|
||||
saved_paths = []
|
||||
for file in files:
|
||||
# Create store directory if it doesn't exist
|
||||
store_dir = Path(settings.MEDIA_ROOT) / "uploads"
|
||||
store_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Save file
|
||||
file_path = store_dir / file.name
|
||||
with open(file_path, "wb") as f:
|
||||
for chunk in file.chunks():
|
||||
f.write(chunk)
|
||||
saved_paths.append(str(file_path))
|
||||
|
||||
# Step 3: Send emails
|
||||
yield send_progress_update(3, "Sending emails 1/2...", 70)
|
||||
time.sleep(0.5)
|
||||
# TODO: Actually send emails
|
||||
yield send_progress_update(4, "Sending emails 2/2...", 75)
|
||||
time.sleep(0.5)
|
||||
# TODO: Actually send emails
|
||||
emails_sent = True # Placeholder
|
||||
|
||||
# Complete
|
||||
yield send_progress_update(
|
||||
5,
|
||||
"Quote request submitted successfully! Check your email for confirmation.",
|
||||
100,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
yield send_progress_update("error", f"An error occurred: {str(e)}", 0)
|
||||
|
||||
response = StreamingHttpResponse(process_quote(), content_type="text/event-stream")
|
||||
response["Cache-Control"] = "no-cache"
|
||||
response["X-Accel-Buffering"] = "no"
|
||||
return response
|
||||
@@ -4,11 +4,13 @@ URL configuration for seduttomotorsports project.
|
||||
|
||||
from django.urls import path
|
||||
from . import views
|
||||
from . import quote_submit
|
||||
from django.conf import settings
|
||||
from django.conf.urls.static import static
|
||||
|
||||
urlpatterns = [
|
||||
path("", views.quote_upload, name="quote_upload"),
|
||||
path("submit-quote/", quote_submit.submit_quote, name="submit_quote"),
|
||||
]
|
||||
|
||||
if settings.DEBUG:
|
||||
|
||||
Reference in New Issue
Block a user