adds match_any option to segments
This commit is contained in:
20
src/personalisation/migrations/0006_segment_match_any.py
Normal file
20
src/personalisation/migrations/0006_segment_match_any.py
Normal file
@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.1 on 2016-12-22 13:23
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('personalisation', '0005_userisloggedinrule'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='segment',
|
||||
name='match_any',
|
||||
field=models.BooleanField(default=False, help_text='Should the segment match all the rules or just one of them?'),
|
||||
),
|
||||
]
|
@ -217,6 +217,10 @@ class Segment(ClusterableModel):
|
||||
default="enabled")
|
||||
persistent = models.BooleanField(
|
||||
default=False, help_text=_("Should the segment persist between visits?"))
|
||||
match_any = models.BooleanField(
|
||||
default=False,
|
||||
help_text=_("Should the segment match all the rules or just one of them?")
|
||||
)
|
||||
|
||||
panels = [
|
||||
MultiFieldPanel([
|
||||
@ -225,6 +229,7 @@ class Segment(ClusterableModel):
|
||||
FieldPanel('status'),
|
||||
FieldPanel('persistent'),
|
||||
]),
|
||||
FieldPanel('match_any'),
|
||||
], heading="Segment"),
|
||||
MultiFieldPanel([
|
||||
InlinePanel(
|
||||
|
@ -97,7 +97,7 @@ def segment_user(page, request, serve_args, serve_kwargs):
|
||||
queried_rules = rule.objects.filter(segment=segment)
|
||||
for result in queried_rules:
|
||||
segment_rules.append(result)
|
||||
result = _test_rules(segment_rules, request)
|
||||
result = _test_rules(segment_rules, request, segment.match_any)
|
||||
|
||||
if result:
|
||||
_add_segment_to_user(segment, request)
|
||||
@ -112,15 +112,18 @@ def segment_user(page, request, serve_args, serve_kwargs):
|
||||
segment.save()
|
||||
|
||||
|
||||
def _test_rules(rules, request):
|
||||
def _test_rules(rules, request, match_any=False):
|
||||
"""Test whether the user matches a segment's rules'"""
|
||||
if len(rules) > 0:
|
||||
for rule in rules:
|
||||
result = rule.test_user(request)
|
||||
if match_any:
|
||||
if result is True:
|
||||
return result
|
||||
|
||||
if result is False:
|
||||
elif result is False:
|
||||
return False
|
||||
|
||||
if not match_any:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
@ -213,6 +213,22 @@ class TestUserSegmenting(object):
|
||||
assert not any(item['encoded_name'] == 'non-persistent' for item in client.session['segments'])
|
||||
|
||||
|
||||
def test_match_any_segmenting(self, client):
|
||||
segment = SegmentFactory(name='Match any', match_any=True)
|
||||
query_rule = QueryRuleFactory(
|
||||
parameter='test',
|
||||
value='test',
|
||||
segment=segment,
|
||||
)
|
||||
second_query_rule = QueryRuleFactory(
|
||||
parameter='test2',
|
||||
value='test2',
|
||||
segment=segment
|
||||
)
|
||||
|
||||
client.get('/?test=test')
|
||||
|
||||
assert any(item['encoded_name'] == 'match-any' for item in client.session['segments'])
|
||||
|
||||
@pytest.mark.django_db
|
||||
class TestUserVisitCount(object):
|
||||
|
Reference in New Issue
Block a user