adds working add variation page
This commit is contained in:
@ -19,6 +19,6 @@ urlpatterns = [
|
||||
name="refferal_rule_embed"),
|
||||
url(r'^segment/visit-count-rule/$', views.visit_count_rule_embed,
|
||||
name="visit_count_rule_embed"),
|
||||
url(r'^variations/(?P<page_pk>\d+)/add/(?P<segment_name>[^/]+)/$',
|
||||
url(r'^variations/(?P<page_pk>\d+)/add/(?P<segment_pk>[^/]+)/$',
|
||||
views.AddVariation.as_view(), name='add')
|
||||
]
|
||||
|
@ -6,7 +6,6 @@ from datetime import datetime
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.db.models.signals import pre_save
|
||||
from django.dispatch import receiver
|
||||
from django.template.defaultfilters import slugify
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
from django.utils.functional import cached_property
|
||||
@ -212,7 +211,7 @@ class AdminPersonalisablePageForm(WagtailAdminPageForm):
|
||||
segment_display = "{} - {}".format(segment_display, "canonical")
|
||||
|
||||
self.fields['segment'].widget = ReadOnlyWidget(
|
||||
text_display=segment_display if segment_display else '')
|
||||
text_display=segment_display if segment_display else _("No segment"))
|
||||
|
||||
|
||||
class PersonalisablePage(Page):
|
||||
@ -262,7 +261,7 @@ class PersonalisablePage(Page):
|
||||
)
|
||||
return variation_parent
|
||||
|
||||
def create_variation(self, segment, parent=None):
|
||||
def create_variation(self, segment, copy_fields=False, parent=None):
|
||||
slug = "{}-{}".format(self.slug, segment.encoded_name())
|
||||
|
||||
if not parent:
|
||||
@ -276,9 +275,18 @@ class PersonalisablePage(Page):
|
||||
'canonical_page': self,
|
||||
}
|
||||
|
||||
model_class = self.content_type.model_class()
|
||||
new_page = model_class(**update_attrs)
|
||||
parent.add_child()
|
||||
if copy_fields:
|
||||
kwargs = {'update_attrs': update_attrs}
|
||||
if parent != self.get_parent():
|
||||
kwargs['to'] = parent
|
||||
|
||||
new_page = self.copy(**kwargs)
|
||||
else:
|
||||
model_class = self.content_type.model_class()
|
||||
new_page = model_class(**update_attrs)
|
||||
parent.add_child(instance=new_page)
|
||||
|
||||
return new_page
|
||||
|
||||
@cached_property
|
||||
def has_variations(self):
|
||||
@ -288,6 +296,7 @@ class PersonalisablePage(Page):
|
||||
def is_canonical(self):
|
||||
return not self.canonical_page and self.has_variations
|
||||
|
||||
|
||||
@cached_classmethod
|
||||
def get_edit_handler(cls):
|
||||
tabs = []
|
||||
|
71
src/personalisation/templates/wagtailadmin/add.html
Normal file
71
src/personalisation/templates/wagtailadmin/add.html
Normal file
@ -0,0 +1,71 @@
|
||||
{% extends "wagtailadmin/generic/index.html" %}
|
||||
{% load wagtailadmin_tags %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block titletag %}{% blocktrans with page_type=content_type.model_class.get_verbose_name %}New {{ page_type }}{% endblocktrans %}{% endblock %}
|
||||
{% block bodyclass %}page-editor create-model-{{ content_type.model }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<header class="merged tab-merged nice-padding">
|
||||
{% include "wagtailadmin/shared/breadcrumb.html" with page=parent_page include_self=1 %}
|
||||
|
||||
<div class="row row-flush">
|
||||
<div class="left col9">
|
||||
<h1 class="icon icon-doc-empty-inverse">{% trans 'Add new variation for' %} <span>{{ content_type.model_class.get_verbose_name }}: {{ page.title }}</span> with segment <span>{{ segment }}</span></h1>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<form id="page-edit-form" action="{% url 'segment:add' page.pk segment.pk %}" method="POST"{% if form.is_multipart %} enctype="multipart/form-data" {% endif %}>
|
||||
{% csrf_token %}
|
||||
{{ edit_handler.render_form_content }}
|
||||
|
||||
{% page_permissions parent_page as parent_page_perms %}
|
||||
<footer>
|
||||
<ul>
|
||||
<li class="actions">
|
||||
<div class="dropdown dropup dropdown-button match-width">
|
||||
<button type="submit" class="button action-save button-longrunning" tabindex="3" data-clicked-text="{% trans 'Saving...' %}"><span class="icon icon-spinner"></span><em>{% trans 'Save draft' %}</em></button>
|
||||
<div class="dropdown-toggle icon icon-arrow-up"></div>
|
||||
<ul role="menu">
|
||||
{% if parent_page_perms.can_publish_subpage %}
|
||||
<li>
|
||||
<button type="submit" name="action-publish" value="action-publish" class="button button-longrunning" tabindex="3" data-clicked-text="{% trans 'Publishing...' %}" {% if page.locked %}disabled {% endif %}><span class="icon icon-spinner"></span><em>{% trans 'Publish' %}</em></button>
|
||||
</li>
|
||||
{% endif %}
|
||||
<li><input type="submit" name="action-submit" value="{% trans 'Submit for moderation' %}" class="button" /></li>
|
||||
</ul>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</footer>
|
||||
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
{% block extra_css %}
|
||||
{{ block.super }}
|
||||
{% include "wagtailadmin/pages/_editor_css.html" %}
|
||||
{% endblock %}
|
||||
{% block extra_js %}
|
||||
{{ block.super }}
|
||||
{% include "wagtailadmin/pages/_editor_js.html" %}
|
||||
|
||||
<script>
|
||||
$(function(){
|
||||
$('#page-edit-form .tab-content section.active input').first().focus();
|
||||
/* Make user confirm before leaving the editor if there are unsaved changes */
|
||||
{% trans "This page has unsaved changes." as confirmation_message %}
|
||||
enableDirtyFormCheck(
|
||||
'#page-edit-form',
|
||||
{
|
||||
confirmationMessage: '{{ confirmation_message|escapejs }}',
|
||||
{% if has_unsaved_changes %}
|
||||
alwaysDirty: true,
|
||||
{% endif %}
|
||||
}
|
||||
);
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
@ -73,6 +73,7 @@ class CreateSegmentView(CreateView):
|
||||
|
||||
class AddVariation(FormView):
|
||||
form_class = PersonalisationForm
|
||||
template_name = "wagtailadmin/add.html"
|
||||
|
||||
add_variation_panels = [
|
||||
FieldPanel('copy_from_canonical'),
|
||||
@ -84,9 +85,9 @@ class AddVariation(FormView):
|
||||
base_form_class=PersonalisationForm)
|
||||
])
|
||||
|
||||
def dispatch(self, request, page_pk, segment_name, *args, **kwargs):
|
||||
def dispatch(self, request, page_pk, segment_pk, *args, **kwargs):
|
||||
self.page = get_object_or_404(PersonalisablePage, pk=page_pk)
|
||||
self.segment = get_object_or_404(Segment, name=segment_name)
|
||||
self.segment = get_object_or_404(Segment, pk=segment_pk)
|
||||
|
||||
return super(AddVariation, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
|
Reference in New Issue
Block a user