diff --git a/birdsong/options.py b/birdsong/options.py
index e5ee18a..3118200 100644
--- a/birdsong/options.py
+++ b/birdsong/options.py
@@ -74,6 +74,8 @@ class CampaignAdmin(ModelAdmin):
inspect_template_name = 'birdsong/editor/inspect_campaign.html'
edit_template_name = 'birdsong/editor/edit_campaign.html'
edit_view_class = editor_views.EditCampaignView
+ create_view_class = editor_views.CreateCampaignView
+ create_template_name = 'birdsong/editor/create_campaign.html'
backend_class = SMTPEmailBackend
contact_class = Contact
contact_filter_class = None
diff --git a/birdsong/static/birdsong/js/preview_campaign.js b/birdsong/static/birdsong/js/preview_campaign.js
index 01fe077..ca1641b 100644
--- a/birdsong/static/birdsong/js/preview_campaign.js
+++ b/birdsong/static/birdsong/js/preview_campaign.js
@@ -3,10 +3,21 @@ class CampaignPreviewHandler {
this.form = form;
this.button = button;
this.previewURL = button.dataset['action'];
+ this.previewFrame = document.querySelector('.campaign-admin__preview-frame');
this.setupListeners();
if (this.previewURL.includes('edit')) {
this.showPreview();
+ } else {
+ this.previewFrame.srcdoc = `
+
+
+
+
@@ -26,7 +24,7 @@
diff --git a/birdsong/views/editor.py b/birdsong/views/editor.py
index 50b2527..f664782 100644
--- a/birdsong/views/editor.py
+++ b/birdsong/views/editor.py
@@ -1,8 +1,8 @@
+from birdsong.models import Contact, Receipt
+from django.http.response import JsonResponse
from django.shortcuts import render
from django.template.loader import render_to_string
-from wagtail.contrib.modeladmin.views import IndexView, InspectView, EditView
-
-from birdsong.models import Receipt, Contact
+from wagtail.contrib.modeladmin.views import CreateView, EditView, InspectView
def preview(request, campaign, test_contact):
@@ -40,25 +40,38 @@ class InspectCampaign(InspectView):
context.update(kwargs)
return super().get_context_data(**context)
-from django.http.response import JsonResponse
+
+def ajax_preview(request, view):
+ FormClass = view.get_form_class()
+ form = FormClass(request.POST)
+ if form.is_valid():
+ campaign = form.save(commit=False)
+ # FIXME won't work with no contacts
+ test_contact = Contact.objects.first()
+ content = render_to_string(
+ campaign.get_template(request),
+ campaign.get_context(request, test_contact)
+ )
+ return JsonResponse({
+ 'success': True,
+ 'preview': content,
+ })
+ else:
+ return JsonResponse({
+ 'success': False,
+ 'errors': form.errors,
+ })
+
+
class EditCampaignView(EditView):
def post(self, request, *args, **kwargs):
if request.is_ajax():
- # Previewing mode, probably :p
- FormClass = self.get_form_class()
- form = FormClass(request.POST)
- if form.is_valid():
- campaign = form.save(commit=False)
- # FIXME won't work with no contacts
- test_contact = Contact.objects.first()
- content = render_to_string(
- campaign.get_template(request),
- campaign.get_context(request, test_contact)
- )
- return JsonResponse({
- 'success': True,
- 'preview': content,
- })
- else:
- return JsonResponse({'success': False })
- return super().post(request, *args, **kwargs)
\ No newline at end of file
+ return ajax_preview(request, self)
+ return super().post(request, *args, **kwargs)
+
+
+class CreateCampaignView(CreateView):
+ def post(self, request, *args, **kwargs):
+ if request.is_ajax():
+ return ajax_preview(request, self)
+ return super().post(request, *args, **kwargs)