7

small cleanup and impersonation code

This commit is contained in:
Boris Besemer
2016-11-28 14:08:02 +01:00
parent 7c9ac4fa6e
commit b824428b36
4 changed files with 14 additions and 67 deletions

View File

@ -12,6 +12,4 @@ urlpatterns = [
name='enable'),
url(r'^segment/(?P<segment_id>[0-9]+)/disable/$', views.disable,
name='disable'),
url(r'^variations/(?P<page_pk>\d+)/add/(?P<segment_pk>[^/]+)/$',
views.AddVariation.as_view(), name='add')
]

View File

@ -0,0 +1,5 @@
def impersonate_other_page(page, other_page):
page.path = other_page.path
page.depth = other_page.depth
page.url_path = other_page.url_path
page.title = other_page.title

View File

@ -1,19 +1,13 @@
from __future__ import absolute_import, unicode_literals
from django.forms import ModelForm
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render, redirect
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.utils.translation import ugettext_lazy as _
from django.views.generic.edit import FormView
from wagtail.contrib.modeladmin.views import CreateView
from wagtail.wagtailadmin.edit_handlers import (
FieldPanel, ObjectList, PageChooserPanel, TabbedInterface)
from personalisation.forms import (
PersonalisationForm, ReferralRuleForm, SegmentForm, TimeRuleForm,
VisitCountRuleForm)
from personalisation.models import PersonalisablePage, Segment
from personalisation.forms import SegmentForm
from personalisation.models import Segment
def overview(request):
@ -47,55 +41,3 @@ class CreateSegmentView(CreateView):
header_icon = 'folder-open-1'
class AddVariation(FormView):
form_class = PersonalisationForm
template_name = "wagtailadmin/add.html"
add_variation_panels = [
FieldPanel('copy_from_canonical'),
PageChooserPanel('parent_page'),
]
edit_handler = TabbedInterface([
ObjectList(add_variation_panels, heading='Variation',
base_form_class=PersonalisationForm)
])
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, pk=segment_pk)
return super(AddVariation, self).dispatch(request, *args, **kwargs)
def get_form_kwargs(self, *args, **kwargs):
form_kwargs = super(AddVariation, self).get_form_kwargs(*args, **kwargs)
form_kwargs.update({
'page': self.page,
'segment': self.segment,
})
return form_kwargs
def form_valid(self, form):
parent = form.cleaned_data['parent_page']
copy_from_canonical = form.cleaned_data['copy_from_canonical']
new_page = self.page.create_variation(
self.segment, copy_fields=copy_from_canonical, parent=parent)
return redirect(
'wagtailadmin_pages:edit', new_page.id
)
def get_context_data(self, *args, **kwargs):
context = super(AddVariation, self).get_context_data(*args, **kwargs)
edit_handler = self.edit_handler.bind_to_model(self.page)
context.update({
'page': self.page,
'segment': self.segment,
'content_type': self.page.content_type,
'parent_page': self.page.get_parent(),
'edit_handler': edit_handler(self.page, context['form']),
})
return context

View File

@ -6,9 +6,8 @@ from wagtail.contrib.modeladmin.views import IndexView
from wagtail.wagtailcore import hooks
from personalisation import admin_urls
from personalisation.forms import (
ReferralRuleForm, TimeRuleForm, VisitCountRuleForm)
from personalisation.models import Segment, PersonalisablePage
from personalisation.utils import impersonate_other_page
@hooks.register('register_admin_urls')
@ -87,6 +86,8 @@ def serve_variation(page, request, serve_args, serve_kwargs):
if variations:
variation = variations[0]
impersonate_other_page(variation, page)
return variation.serve(request, *serve_args, **serve_kwargs)
def _check_for_variations(segments, page):
@ -94,6 +95,7 @@ def _check_for_variations(segments, page):
variation = PersonalisablePage.objects.filter(canonical_page=page, segment=segment)
if variation:
return variation
return None