Skip to content

Commit

Permalink
Use the Segment Adapter for retrieving page visits
Browse files Browse the repository at this point in the history
This fixes the last occurence of directly accessing the session in the
codebase
  • Loading branch information
mvantellingen committed May 31, 2017
1 parent 31f8a32 commit 9705947
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/wagtail_personalisation/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ def add_page_visit(self, page):
'count': 1,
})

def get_visit_count(self, page=None):
"""Return the number of visits on the current request or given page"""
path = page.path if page else self.request.path
visit_count = self.request.session.setdefault('visit_count', [])
for visit in visit_count:
if visit['path'] == path:
return visit['count']
return 0

def update_visit_count(self):
"""Update the visit count for all segments in the request session."""
segments = self.request.session['segments']
Expand Down
19 changes: 4 additions & 15 deletions src/wagtail_personalisation/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,12 @@ def test_user(self, request):
operator = self.operator
segment_count = self.count

def get_visit_count(request):
"""Search through the request sessions to get the page visit count
corresponding to the request.
# Local import for cyclic import
from wagtail_personalisation.adapters import get_segment_adapter

:param request: The http request
:type request: django.http.HttpRequest
:returns: A number indicating the amount of visits
to the requested page
:rtype: int
"""
for page in request.session['visit_count']:
if page['path'] == request.path:
return page['count']

visit_count = get_visit_count(request)
adapter = get_segment_adapter(request)

visit_count = adapter.get_visit_count()
if visit_count and operator == "more_than":
if visit_count > segment_count:
return True
Expand Down

0 comments on commit 9705947

Please sign in to comment.