Skip to content

Commit 871239d

Browse files
author
AhmadMWaddah
committed
Feature: Add /seed/ endpoint for production data seeding
1 parent c3dab68 commit 871239d

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

config/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.urls import include, path
1414
from django.views.generic.base import RedirectView, TemplateView
1515
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
16+
import core.views as core_views
1617

1718
urlpatterns = [
1819
# Admin
@@ -34,6 +35,8 @@
3435
path("", RedirectView.as_view(url="/accounts/dashboard/", permanent=False)),
3536
# Health Check (for monitoring)
3637
path("health/", TemplateView.as_view(template_name="health.html"), name="Health"),
38+
# Seed endpoint (use token from SEED_TOKEN env var)
39+
path("seed/", core_views.seed_endpoint, name="Seed"),
3740
]
3841

3942
if settings.DEBUG:

core/views.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,34 @@ def wrapper(request, *args, **kwargs):
9191
return view_func(request, *args, **kwargs)
9292

9393
return wrapper
94+
95+
96+
def seed_endpoint(request):
97+
"""
98+
Seed endpoint for initial data setup in production.
99+
100+
Requires SEED_TOKEN env var to match ?token= query param.
101+
"""
102+
from django.conf import settings
103+
104+
provided_token = request.GET.get("token", "")
105+
expected_token = getattr(settings, "SEED_TOKEN", "")
106+
107+
if not expected_token:
108+
return JsonResponse({"error": "Seed endpoint disabled - no SEED_TOKEN configured"}, status=403)
109+
110+
if provided_token != expected_token:
111+
return JsonResponse({"error": "Invalid token"}, status=403)
112+
113+
# Run the seed command
114+
from core.management.commands.seed_erp import Command as SeedCommand
115+
from io import StringIO
116+
117+
output = StringIO()
118+
try:
119+
cmd = SeedCommand()
120+
cmd(stdout=output, stderr=StringIO(), force=True)
121+
result = output.getvalue()
122+
return JsonResponse({"status": "success", "output": result})
123+
except Exception as e:
124+
return JsonResponse({"error": str(e)}, status=500)

0 commit comments

Comments
 (0)