Preview for create

This commit is contained in:
Seb 2020-05-08 17:10:52 +10:00
parent 7508eacd67
commit c7e8b7d131
5 changed files with 83 additions and 31 deletions

View File

@ -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

View File

@ -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 = `
<html>
</html >
<body>
<div style='text-align: center; width: 100%;'>
<h3>Click 'Reload preview' to load preview</h3>
</div>
</body>
`
}
}
@ -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();
})

View File

@ -0,0 +1,31 @@
{% extends "modeladmin/create.html" %}
{% block content %}
<div class='campaign-admin'>
<div class='campaign-admin__form'>
{{ block.super }}
</div>
<div class='campaign-admin__preview'>
<header role="banner">
</header>
<iframe class='campaign-admin__preview-frame'>
</iframe>
</div>
</div>
{% endblock content %}
{% block footer %}
<footer>
<ul>
<li class="actions">
{% block form_actions %}
{{ block.super }}
{% endblock %}
</li>
<li class='preview'>
<button class="button campaign-preview icon icon-view" data-action="{{ view.create_url }}">
Reload preview
</button>
</li>
</ul>
</footer>
{% endblock footer %}

View File

@ -1,8 +1,6 @@
{% extends "modeladmin/edit.html" %}
{% load i18n %}
{% block form_action %}{{ view.edit_url }}{% endblock %}
{% block content %}
<div class='campaign-admin'>
<div class='campaign-admin__form'>
@ -26,7 +24,7 @@
</li>
<li class='preview'>
<button class="button campaign-preview icon icon-view" data-action="{{ view.edit_url }}">
Preview
Reload preview
</button>
</li>
</ul>

View File

@ -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 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)