Skip to content

Commit

Permalink
Redirect to existing variant if present
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoorman authored and pjstevns committed May 31, 2017
1 parent 63d5de9 commit 4deaaa9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/wagtail_personalisation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def toggle(request, segment_id):


def copy_page_view(request, page_id, segment_id):
"""Copy page with selected segment.
"""Copy page with selected segment. If the page for the selected segment
already exists the user will be redirected to that particular page.
:param request: The http request
:type request: django.http.HttpRequest
Expand All @@ -126,8 +127,12 @@ def copy_page_view(request, page_id, segment_id):
if request.user.has_perm('wagtailadmin.access_admin'):
segment = get_object_or_404(Segment, pk=segment_id)
page = get_object_or_404(Page, pk=page_id).specific
new_page = page.copy_for_segment(segment)
edit_url = reverse('wagtailadmin_pages:edit', args=[new_page.id])
variants = page.variants_for_segments([segment])
if variants.exists():
variant = variants.first()
else:
variant = page.copy_for_segment(segment)
edit_url = reverse('wagtailadmin_pages:edit', args=[variant.id])

return HttpResponseRedirect(edit_url)

Expand Down

0 comments on commit 4deaaa9

Please sign in to comment.