Skip to content

Commit

Permalink
Use settings module
Browse files Browse the repository at this point in the history
  • Loading branch information
jberghoef committed May 26, 2018
1 parent bbe0d4a commit 3ef7861
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/wagtail_personalisation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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):
Expand Down

0 comments on commit 3ef7861

Please sign in to comment.