From 1c284e43881f0a81b4a1e8b5ab410f1e25700217 Mon Sep 17 00:00:00 2001 From: Jasper Berghoef Date: Fri, 25 Aug 2017 16:42:11 +0200 Subject: [PATCH] Use settings module --- src/wagtail_personalisation/models.py | 61 +++++++++++++++------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/wagtail_personalisation/models.py b/src/wagtail_personalisation/models.py index d90ebe77..0cee1a4c 100644 --- a/src/wagtail_personalisation/models.py +++ b/src/wagtail_personalisation/models.py @@ -172,6 +172,9 @@ class SegmentVisit(models.Model): max_length=64, editable=False, null=True, db_index=True) visit_date = models.DateTimeField(auto_now_add=True) + class Meta: + ordering = ['-visit_date'] + @classmethod def create_segment_visit(cls, page, request, metadata=None): """Create a segment visit object. @@ -185,37 +188,39 @@ def create_segment_visit(cls, page, request, metadata=None): :rtype: wagtail_personalisation.models.SegmentVisit """ from wagtail_personalisation.adapters import get_segment_adapter + wxp_settings = PersonalisationSettings.for_site(request.site) + + if wxp_settings.detailed_visits: + adapter = get_segment_adapter(request) + user_segments = adapter.get_segments() + + if not metadata: + metadata = page.personalisation_metadata + metadata = metadata.metadata_for_segments(user_segments) + + user = request.user if request.user.is_authenticated else None + visit = cls.objects.create( + user=user, + page=page, + served_segment=metadata.first().segment, + served_variant=metadata.first().variant, + session=request.session.session_key + ) - adapter = get_segment_adapter(request) - user_segments = adapter.get_segments() - - if not metadata: - metadata = page.personalisation_metadata - metadata = metadata.metadata_for_segments(user_segments) - - user = request.user if request.user.is_authenticated() else None - visit = cls.objects.create( - user=user, - page=page, - served_segment=metadata.first().segment, - served_variant=metadata.first().variant, - session=request.session.session_key - ) + for segment in user_segments: + rules = [ + rule for rule in segment.get_rules() if rule.unique_encoded_name + in request.matched_rules + ] - for segment in user_segments: - rules = [ - rule for rule in segment.get_rules() if rule.unique_encoded_name - in request.matched_rules - ] - - SegmentVisitMetadata.objects.create( - visit=visit, - segment=segment, - matched_rules=','.join( - rule.unique_encoded_name for rule in rules) - ) + SegmentVisitMetadata.objects.create( + visit=visit, + segment=segment, + matched_rules=','.join( + rule.unique_encoded_name for rule in rules) + ) - return visit + return visit @classmethod def reverse_match(cls, user):