Visit count rule
This commit is contained in:
23
src/personalisation/migrations/0006_visitcountrule.py
Normal file
23
src/personalisation/migrations/0006_visitcountrule.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Generated by Django 1.10.3 on 2016-11-08 14:47
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('personalisation', '0005_referralrule'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='VisitCountRule',
|
||||||
|
fields=[
|
||||||
|
('abstractbaserule_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='personalisation.AbstractBaseRule')),
|
||||||
|
],
|
||||||
|
bases=('personalisation.abstractbaserule',),
|
||||||
|
),
|
||||||
|
]
|
25
src/personalisation/migrations/0007_auto_20161108_1610.py
Normal file
25
src/personalisation/migrations/0007_auto_20161108_1610.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Generated by Django 1.10.3 on 2016-11-08 15:10
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('personalisation', '0006_visitcountrule'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='visitcountrule',
|
||||||
|
name='count',
|
||||||
|
field=models.PositiveSmallIntegerField(default=0),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='visitcountrule',
|
||||||
|
name='operator',
|
||||||
|
field=models.CharField(choices=[('more_than', 'More than'), ('less_than', 'Less than'), ('equal_to', 'Equal to')], default='ht', max_length=20),
|
||||||
|
),
|
||||||
|
]
|
@ -94,3 +94,34 @@ class ReferralRule(AbstractBaseRule):
|
|||||||
if pattern.search(referer):
|
if pattern.search(referer):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
"""
|
||||||
|
Visit count rule to segment users based on amount of visits
|
||||||
|
"""
|
||||||
|
class VisitCountRule(AbstractBaseRule):
|
||||||
|
OPERATOR_CHOICES = (
|
||||||
|
('more_than', 'More than'),
|
||||||
|
('less_than', 'Less than'),
|
||||||
|
('equal_to', 'Equal to'),
|
||||||
|
)
|
||||||
|
operator = models.CharField(max_length=20, choices=OPERATOR_CHOICES, default="ht")
|
||||||
|
count = models.PositiveSmallIntegerField(default=0)
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(VisitCountRule, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def test_user(self, request):
|
||||||
|
operator = self.operator
|
||||||
|
segment_count = self.count
|
||||||
|
visit_count = request.session.get('visit_count')
|
||||||
|
|
||||||
|
if operator is "more_than":
|
||||||
|
if visit_count > segment_count:
|
||||||
|
return True
|
||||||
|
elif operator is "less_than":
|
||||||
|
if visit_count < segment_count:
|
||||||
|
return True
|
||||||
|
elif operator is "equal_to":
|
||||||
|
if visit_count is segment_count:
|
||||||
|
return True
|
||||||
|
return False
|
@ -16,6 +16,9 @@ def register_admin_urls():
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
The base model for the Segments administration interface
|
||||||
|
"""
|
||||||
class SegmentModelAdmin(ModelAdmin):
|
class SegmentModelAdmin(ModelAdmin):
|
||||||
model = Segment
|
model = Segment
|
||||||
menu_icon = 'group'
|
menu_icon = 'group'
|
||||||
@ -25,3 +28,14 @@ class SegmentModelAdmin(ModelAdmin):
|
|||||||
form_view_extra_css = ['personalisation/segment/form.css']
|
form_view_extra_css = ['personalisation/segment/form.css']
|
||||||
|
|
||||||
modeladmin_register(SegmentModelAdmin)
|
modeladmin_register(SegmentModelAdmin)
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Update the users visit count before each page visit
|
||||||
|
"""
|
||||||
|
@hooks.register('before_serve_page')
|
||||||
|
def set_visit_count(page, request, serve_args, serve_kwargs):
|
||||||
|
if request.session.get('visit_count'):
|
||||||
|
request.session['visit_count'] = request.session.get('visit_count') + 1
|
||||||
|
else:
|
||||||
|
request.session['visit_count'] = 1
|
Reference in New Issue
Block a user