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 = ` + + + +
+

Click 'Reload preview' to load preview

+
+ + ` } } @@ -21,7 +32,6 @@ class CampaignPreviewHandler { } showPreview() { - // alert(this.previewURL); const formData = new FormData(this.form); fetch(this.previewURL, { method: 'POST', @@ -34,12 +44,10 @@ class CampaignPreviewHandler { .then(response => response.json()) .then(responseJSON => { if (responseJSON.success) { - let previewContainer = document.querySelector('.campaign-admin__preview-frame'); - previewContainer.srcdoc = responseJSON.preview; - + this.previewFrame.srcdoc = responseJSON.preview; } else { - alert('Your form has missing/incorrect data'); - // Try to submit the form to show errors? + // Submit form so user can see errors + this.form.submit(); } this.resetButton(); }) diff --git a/birdsong/templates/birdsong/editor/create_campaign.html b/birdsong/templates/birdsong/editor/create_campaign.html new file mode 100644 index 0000000..0e39f5a --- /dev/null +++ b/birdsong/templates/birdsong/editor/create_campaign.html @@ -0,0 +1,31 @@ +{% extends "modeladmin/create.html" %} + +{% block content %} +
+
+ {{ block.super }} +
+
+
+
+ +
+
+{% endblock content %} +{% block footer %} + +{% endblock footer %} \ No newline at end of file diff --git a/birdsong/templates/birdsong/editor/edit_campaign.html b/birdsong/templates/birdsong/editor/edit_campaign.html index 38af2c0..18333d5 100644 --- a/birdsong/templates/birdsong/editor/edit_campaign.html +++ b/birdsong/templates/birdsong/editor/edit_campaign.html @@ -1,8 +1,6 @@ {% extends "modeladmin/edit.html" %} {% load i18n %} -{% block form_action %}{{ view.edit_url }}{% endblock %} - {% block content %}
@@ -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)