7

adds match_any option to segments

This commit is contained in:
Boris Besemer
2016-12-22 14:25:00 +01:00
parent 4c0f0760b2
commit 5f8c768894
4 changed files with 49 additions and 5 deletions

View 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?'),
),
]

View File

@ -217,6 +217,10 @@ class Segment(ClusterableModel):
default="enabled") default="enabled")
persistent = models.BooleanField( persistent = models.BooleanField(
default=False, help_text=_("Should the segment persist between visits?")) 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 = [ panels = [
MultiFieldPanel([ MultiFieldPanel([
@ -225,6 +229,7 @@ class Segment(ClusterableModel):
FieldPanel('status'), FieldPanel('status'),
FieldPanel('persistent'), FieldPanel('persistent'),
]), ]),
FieldPanel('match_any'),
], heading="Segment"), ], heading="Segment"),
MultiFieldPanel([ MultiFieldPanel([
InlinePanel( InlinePanel(

View File

@ -97,7 +97,7 @@ def segment_user(page, request, serve_args, serve_kwargs):
queried_rules = rule.objects.filter(segment=segment) queried_rules = rule.objects.filter(segment=segment)
for result in queried_rules: for result in queried_rules:
segment_rules.append(result) segment_rules.append(result)
result = _test_rules(segment_rules, request) result = _test_rules(segment_rules, request, segment.match_any)
if result: if result:
_add_segment_to_user(segment, request) _add_segment_to_user(segment, request)
@ -112,16 +112,19 @@ def segment_user(page, request, serve_args, serve_kwargs):
segment.save() segment.save()
def _test_rules(rules, request): def _test_rules(rules, request, match_any=False):
"""Test whether the user matches a segment's rules'""" """Test whether the user matches a segment's rules'"""
if len(rules) > 0: if len(rules) > 0:
for rule in rules: for rule in rules:
result = rule.test_user(request) result = rule.test_user(request)
if match_any:
if result is True:
return result
if result is False: elif result is False:
return False return False
if not match_any:
return True return True
return False return False

View File

@ -213,6 +213,22 @@ class TestUserSegmenting(object):
assert not any(item['encoded_name'] == 'non-persistent' for item in client.session['segments']) 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 @pytest.mark.django_db
class TestUserVisitCount(object): class TestUserVisitCount(object):