38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
import os
|
|
from pathlib import Path
|
|
from django.shortcuts import render
|
|
from django.http import HttpResponse
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
|
|
|
@staff_member_required
|
|
def setup_env(request):
|
|
# BASE_DIR is usually workspace, so its parent is where .env is.
|
|
env_path = Path(os.getcwd()).parent / ".env"
|
|
|
|
if request.method == "POST":
|
|
# Extract secrets from form
|
|
secret_key = request.POST.get("DJANGO_SECRET_KEY", "").strip()
|
|
email_user = request.POST.get("EMAIL_HOST_USER", "").strip()
|
|
email_pass = request.POST.get("EMAIL_HOST_PASSWORD", "").strip()
|
|
|
|
existing_content = ""
|
|
if env_path.exists():
|
|
existing_content = env_path.read_text()
|
|
|
|
with open(env_path, "a") as f:
|
|
# Add a newline if the file doesn't end with one
|
|
if existing_content and not existing_content.endswith("\n"):
|
|
f.write("\n")
|
|
|
|
# Append only non-empty values
|
|
if secret_key:
|
|
f.write(f"DJANGO_SECRET_KEY={secret_key}\n")
|
|
if email_user:
|
|
f.write(f"EMAIL_HOST_USER={email_user}\n")
|
|
if email_pass:
|
|
f.write(f"EMAIL_HOST_PASSWORD={email_pass}\n")
|
|
|
|
return HttpResponse("<h3>Success! Secrets safely appended to .env.</h3><p>Please return to the AI chat, ask me to restart the app, and then I will delete this temporary page.</p>")
|
|
|
|
return render(request, "core/temp_env_setup.html")
|