Beispiel #1
0
def serve_variation(page, request, serve_args, serve_kwargs):
    """Apply a segment to a visitor before serving the page.

    :param page: The page being served
    :type page: wagtail.wagtailcore.models.Page
    :param request: The http request
    :type request: django.http.HttpRequest
    :returns: A variation if one is available for the visitor's segment,
              otherwise the original page
    :rtype: wagtail.wagtailcore.models.Page

    """
    user_segments = []

    for segment in segments_adapter.get_all_segments():
        try:
            user_segment = Segment.objects.get(pk=segment['id'],
                                               status='enabled')
        except Segment.DoesNotExist:
            user_segment = None
        if user_segment:
            user_segments.append(user_segment)

    if len(user_segments) > 0:
        variations = _check_for_variations(user_segments, page)

        if variations:
            variation = variations[0]

            impersonate_other_page(variation, page)

            return variation.serve(request, *serve_args, **serve_kwargs)
def test_impersonate_other_page():
    page = Page(path="/", depth=0, url_path="/", title="Hoi")
    other_page = Page(path="/other", depth=1, url_path="/other", title="Doei")

    impersonate_other_page(page, other_page)

    assert page == other_page
def serve_variation(page, request, serve_args, serve_kwargs):
    """Apply a segment to a visitor before serving the page.

    :param page: The page being served
    :type page: wagtail.wagtailcore.models.Page
    :param request: The http request
    :type request: django.http.HttpRequest
    :returns: A variation if one is available for the visitor's segment,
              otherwise the original page
    :rtype: wagtail.wagtailcore.models.Page

    """
    user_segments = []
    if not isinstance(page, PersonalisablePageMixin):
        return

    adapter = get_segment_adapter(request)
    user_segments = adapter.get_segments()

    if user_segments:
        variations = page.variants_for_segments(user_segments)
        if variations:
            variation = variations.first()
            impersonate_other_page(variation, page)
            return variation.serve(request, *serve_args, **serve_kwargs)
def test_impersonate_other_page(page, otherpage):
    impersonate_other_page(page, otherpage)
    assert page.title == otherpage.title == 'Bye'
    assert page.path == otherpage.path