Skip to content

Commit

Permalink
Added visit page view
Browse files Browse the repository at this point in the history
Fixes #74

Allows frontend users to submit page views and retrieve segments while
behind a cache or CDN.
  • Loading branch information
kaedroho committed May 31, 2017
1 parent 9790a44 commit 9f4a015
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/wagtail_personalisation/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def check_if_segmented(item):
segdict = create_segment_dictionary(segment)
self.request.session['segments'].append(segdict)

def add_page_visit(self, page):
def add_page_visit(self, page, path=None):
"""Mar kthe page as visited by the user"""
visit_count = self.request.session.setdefault('visit_count', [])
page_visits = [visit for visit in visit_count if visit['id'] == page.pk]
Expand All @@ -137,7 +137,7 @@ def add_page_visit(self, page):
visit_count.append({
'slug': page.slug,
'id': page.pk,
'path': self.request.path,
'path': path or self.request.path,
'count': 1,
})

Expand Down
38 changes: 37 additions & 1 deletion src/wagtail_personalisation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from django import forms

from django.http import HttpResponseForbidden, HttpResponseRedirect
from django.http import HttpResponseForbidden, HttpResponseRedirect, HttpResponseBadRequest, JsonResponse
from django.shortcuts import get_object_or_404, reverse
from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_POST
from django.utils.translation import ugettext_lazy as _
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from wagtail.contrib.modeladmin.views import IndexView
Expand Down Expand Up @@ -146,3 +148,37 @@ def copy_page_view(request, page_id, segment_id):
return HttpResponseRedirect(edit_url)

return HttpResponseForbidden()


@never_cache
@require_POST
def visit_page(request):
"""Allows a frontend user to submit a page view and retrieve their current
segments on a site that is behind a cache or CDN.
On each page view, the user must POST to this view the page ID and path
that they are browsing. It will return a JSON-formatted document containing
a list of segments that are currently active for them.
"""
page_id = request.POST.get('page_id')
path = request.POST.get('path')

if page_id is None or path is None:
return HttpResponseBadRequest()

page = Page.objects.filter(id=page_id).only('id', 'slug', 'live').first()

if page is None or page.live is False:
return HttpResponseBadRequest()

request.segment_adapter.add_page_visit(page, path=path)
request.segments_adapter.refresh()

return JsonResponse {
'segments': [
{
'slug': segment.encoded_name(),
}
for segment in request.segments_adapter.get_all_segments()
]
}

0 comments on commit 9f4a015

Please sign in to comment.