Skip to content

Commit 096e18d

Browse files
committed
Added visit page view
Fixes #74 Allows frontend users to submit page views and retrieve segments while behind a cache or CDN.
1 parent 59b6e7f commit 096e18d

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/wagtail_personalisation/adapters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def check_if_segmented(item):
131131
segdict = create_segment_dictionary(segment)
132132
self.request.session['segments'].append(segdict)
133133

134-
def add_page_visit(self, page):
134+
def add_page_visit(self, page, path=None):
135135
"""Mark the page as visited by the user"""
136136
visit_count = self.request.session.setdefault('visit_count', [])
137137
page_visits = [visit for visit in visit_count if visit['id'] == page.pk]
@@ -144,7 +144,7 @@ def add_page_visit(self, page):
144144
visit_count.append({
145145
'slug': page.slug,
146146
'id': page.pk,
147-
'path': self.request.path,
147+
'path': path or self.request.path,
148148
'count': 1,
149149
})
150150

src/wagtail_personalisation/views.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
from django import forms
44

5-
from django.http import HttpResponseForbidden, HttpResponseRedirect
5+
from django.http import HttpResponseForbidden, HttpResponseRedirect, HttpResponseBadRequest, JsonResponse
66
from django.shortcuts import get_object_or_404, reverse
7+
from django.views.decorators.cache import never_cache
8+
from django.views.decorators.http import require_POST
79
from django.utils.translation import ugettext_lazy as _
810
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
911
from wagtail.contrib.modeladmin.views import IndexView
@@ -148,3 +150,37 @@ def copy_page_view(request, page_id, segment_id):
148150
return HttpResponseRedirect(edit_url)
149151

150152
return HttpResponseForbidden()
153+
154+
155+
@never_cache
156+
@require_POST
157+
def visit_page(request):
158+
"""Allows a frontend user to submit a page view and retrieve their current
159+
segments on a site that is behind a cache or CDN.
160+
161+
On each page view, the user must POST to this view the page ID and path
162+
that they are browsing. It will return a JSON-formatted document containing
163+
a list of segments that are currently active for them.
164+
"""
165+
page_id = request.POST.get('page_id')
166+
path = request.POST.get('path')
167+
168+
if page_id is None or path is None:
169+
return HttpResponseBadRequest()
170+
171+
page = Page.objects.filter(id=page_id).only('id', 'slug', 'live').first()
172+
173+
if page is None or page.live is False:
174+
return HttpResponseBadRequest()
175+
176+
request.segment_adapter.add_page_visit(page, path=path)
177+
request.segments_adapter.refresh()
178+
179+
return JsonResponse({
180+
'segments': [
181+
{
182+
'slug': segment.encoded_name(),
183+
}
184+
for segment in request.segments_adapter.get_all_segments()
185+
]
186+
})

0 commit comments

Comments
 (0)