Merge pull request #15 from LabD/feature/logged-in-rule
adding rule for logged in users
This commit is contained in:
@ -5,6 +5,12 @@ from django.contrib import admin
|
||||
from personalisation import models
|
||||
|
||||
|
||||
class UserIsLoggedInRuleAdminInline(admin.TabularInline):
|
||||
"""Inline the UserIsLoggedIn Rule into the administration interface for segments"""
|
||||
model = models.UserIsLoggedInRule
|
||||
extra = 0
|
||||
|
||||
|
||||
class TimeRuleAdminInline(admin.TabularInline):
|
||||
"""Inline the Time Rule into the administration interface for segments"""
|
||||
model = models.TimeRule
|
||||
@ -27,7 +33,7 @@ class VisitCountRuleAdminInline(admin.TabularInline):
|
||||
|
||||
class SegmentAdmin(admin.ModelAdmin):
|
||||
"""Add the inlines to the Segment admin interface"""
|
||||
inlines = (TimeRuleAdminInline,
|
||||
inlines = (UserIsLoggedInRuleAdminInline, TimeRuleAdminInline,
|
||||
ReferralRuleAdminInline, VisitCountRuleAdminInline)
|
||||
|
||||
|
||||
|
28
src/personalisation/migrations/0005_userisloggedinrule.py
Normal file
28
src/personalisation/migrations/0005_userisloggedinrule.py
Normal file
@ -0,0 +1,28 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.1 on 2016-12-11 12:15
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import modelcluster.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('personalisation', '0004_segment_persistent'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='UserIsLoggedInRule',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('is_logged_in', models.BooleanField(default=False)),
|
||||
('segment', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='personalisation_userisloggedinrule_related', related_query_name='personalisation_userisloggedinrules', to='personalisation.Segment')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
@ -180,6 +180,26 @@ class QueryRule(AbstractBaseRule):
|
||||
return 'Query Rule'
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class UserIsLoggedInRule(AbstractBaseRule):
|
||||
"""User should be logged in"""
|
||||
|
||||
is_logged_in = models.BooleanField(default=False)
|
||||
|
||||
panels = [
|
||||
FieldPanel('is_logged_in'),
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(UserIsLoggedInRule, self).__init__(*args, **kwargs)
|
||||
|
||||
def test_user(self, request=None):
|
||||
return request.user.is_authenticated() == self.is_logged_in
|
||||
|
||||
def __str__(self):
|
||||
return '{}'.format(self.is_logged_in)
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Segment(ClusterableModel):
|
||||
"""Model for a new segment"""
|
||||
@ -206,7 +226,6 @@ class Segment(ClusterableModel):
|
||||
FieldPanel('persistent'),
|
||||
]),
|
||||
], heading="Segment"),
|
||||
|
||||
MultiFieldPanel([
|
||||
InlinePanel(
|
||||
"{}_related".format(rule._meta.db_table),
|
||||
|
Reference in New Issue
Block a user