Import sorting and other code cleaning
This commit is contained in:
@ -4,18 +4,22 @@ from personalisation import models
|
|||||||
|
|
||||||
|
|
||||||
class TimeRuleAdminInline(admin.TabularInline):
|
class TimeRuleAdminInline(admin.TabularInline):
|
||||||
|
"""Inline the Time Rule into the administration interface for segments"""
|
||||||
model = models.TimeRule
|
model = models.TimeRule
|
||||||
extra = 0
|
extra = 0
|
||||||
|
|
||||||
class ReferralRuleAdminInline(admin.TabularInline):
|
class ReferralRuleAdminInline(admin.TabularInline):
|
||||||
|
"""Inline the Referral Rule into the administration interface for segments"""
|
||||||
model = models.ReferralRule
|
model = models.ReferralRule
|
||||||
extra = 0
|
extra = 0
|
||||||
|
|
||||||
class VisitCountRuleAdminInline(admin.TabularInline):
|
class VisitCountRuleAdminInline(admin.TabularInline):
|
||||||
|
"""Inline the Visit Count Rule into the administration interface for segments"""
|
||||||
model = models.VisitCountRule
|
model = models.VisitCountRule
|
||||||
extra = 0
|
extra = 0
|
||||||
|
|
||||||
class SegmentAdmin(admin.ModelAdmin):
|
class SegmentAdmin(admin.ModelAdmin):
|
||||||
|
"""Add the inlines to the Segment admin interface"""
|
||||||
inlines = (TimeRuleAdminInline, ReferralRuleAdminInline, VisitCountRuleAdminInline)
|
inlines = (TimeRuleAdminInline, ReferralRuleAdminInline, VisitCountRuleAdminInline)
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,4 +46,3 @@ class SegmentMiddleware(object):
|
|||||||
def add_segment_to_user(self, segment, request):
|
def add_segment_to_user(self, segment, request):
|
||||||
if segment not in request.session['segments']:
|
if segment not in request.session['segments']:
|
||||||
request.session['segments'].append(segment.encoded_name())
|
request.session['segments'].append(segment.encoded_name())
|
||||||
|
|
||||||
|
@ -2,13 +2,16 @@ from __future__ import absolute_import, unicode_literals
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from polymorphic.models import PolymorphicModel
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.encoding import python_2_unicode_compatible
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from modelcluster.fields import ParentalKey
|
||||||
from modelcluster.models import ClusterableModel
|
from modelcluster.models import ClusterableModel
|
||||||
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
|
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
|
||||||
|
from wagtail.wagtailcore.models import Orderable
|
||||||
|
|
||||||
|
from polymorphic.models import PolymorphicModel
|
||||||
|
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@python_2_unicode_compatible
|
||||||
@ -24,13 +27,13 @@ class Segment(ClusterableModel):
|
|||||||
panels = [
|
panels = [
|
||||||
FieldPanel('name'),
|
FieldPanel('name'),
|
||||||
FieldPanel('status'),
|
FieldPanel('status'),
|
||||||
InlinePanel('rules'),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def encoded_name(self):
|
def encoded_name(self):
|
||||||
|
"""Returns a string with a slug for the segment"""
|
||||||
return "".join(self.name.lower().split())
|
return "".join(self.name.lower().split())
|
||||||
|
|
||||||
|
|
||||||
@ -40,7 +43,8 @@ class AbstractBaseRule(PolymorphicModel):
|
|||||||
"""Base for creating rules to segment users with"""
|
"""Base for creating rules to segment users with"""
|
||||||
segment = models.ForeignKey(to=Segment, related_name="rules")
|
segment = models.ForeignKey(to=Segment, related_name="rules")
|
||||||
|
|
||||||
def test_user(self, request=None):
|
def test_user(self, request):
|
||||||
|
"""Test if the user matches this rule"""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -1,19 +1,18 @@
|
|||||||
from __future__ import absolute_import, unicode_literals
|
from __future__ import absolute_import, unicode_literals
|
||||||
|
|
||||||
|
from django.http import HttpResponse, HttpResponseRedirect
|
||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
from django.http import HttpResponseRedirect, HttpResponse
|
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
from personalisation.models import Segment
|
from personalisation.models import Segment
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
Segments overview
|
|
||||||
"""
|
|
||||||
def overview(request):
|
def overview(request):
|
||||||
|
"""Display segments overview. Dummy function"""
|
||||||
return render(request, 'wagtailadmin/segment.html')
|
return render(request, 'wagtailadmin/segment.html')
|
||||||
|
|
||||||
def enable(request, segment_id):
|
def enable(request, segment_id):
|
||||||
|
"""Enable the selected segment"""
|
||||||
segment = get_object_or_404(Segment, pk=segment_id)
|
segment = get_object_or_404(Segment, pk=segment_id)
|
||||||
segment.status = 'enabled'
|
segment.status = 'enabled'
|
||||||
segment.save()
|
segment.save()
|
||||||
@ -21,6 +20,7 @@ def enable(request, segment_id):
|
|||||||
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
|
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
|
||||||
|
|
||||||
def disable(request, segment_id):
|
def disable(request, segment_id):
|
||||||
|
"""Disable the selected segment"""
|
||||||
segment = get_object_or_404(Segment, pk=segment_id)
|
segment = get_object_or_404(Segment, pk=segment_id)
|
||||||
segment.status = 'disabled'
|
segment.status = 'disabled'
|
||||||
segment.save()
|
segment.save()
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
from django.conf import settings
|
|
||||||
from django.conf.urls import include, url
|
from django.conf.urls import include, url
|
||||||
from django.core.urlresolvers import reverse
|
|
||||||
|
|
||||||
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
|
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
|
||||||
from wagtail.wagtailcore import hooks
|
from wagtail.wagtailcore import hooks
|
||||||
|
|
||||||
@ -11,15 +8,17 @@ from personalisation.models import Segment
|
|||||||
|
|
||||||
@hooks.register('register_admin_urls')
|
@hooks.register('register_admin_urls')
|
||||||
def register_admin_urls():
|
def register_admin_urls():
|
||||||
|
"""Adds the administration urls for the personalisation apps."""
|
||||||
return [
|
return [
|
||||||
url(r'^personalisation/', include(admin_urls, app_name='personalisation', namespace='personalisation')),
|
url(r'^personalisation/', include(
|
||||||
|
admin_urls,
|
||||||
|
app_name='personalisation',
|
||||||
|
namespace='personalisation')),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
The base model for the Segments administration interface
|
|
||||||
"""
|
|
||||||
class SegmentModelAdmin(ModelAdmin):
|
class SegmentModelAdmin(ModelAdmin):
|
||||||
|
"""The base model for the Segments administration interface."""
|
||||||
model = Segment
|
model = Segment
|
||||||
menu_icon = 'group'
|
menu_icon = 'group'
|
||||||
add_to_settings_menu = False
|
add_to_settings_menu = False
|
||||||
@ -31,12 +30,14 @@ class SegmentModelAdmin(ModelAdmin):
|
|||||||
modeladmin_register(SegmentModelAdmin)
|
modeladmin_register(SegmentModelAdmin)
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
Update the users visit count before each page visit
|
|
||||||
"""
|
|
||||||
@hooks.register('before_serve_page')
|
@hooks.register('before_serve_page')
|
||||||
def set_visit_count(page, request, serve_args, serve_kwargs):
|
def set_visit_count(page, request, serve_args, serve_kwargs):
|
||||||
|
"""Update the users visit count before each page visit."""
|
||||||
if request.session.get('visit_count'):
|
if request.session.get('visit_count'):
|
||||||
request.session['visit_count'] = request.session.get('visit_count') + 1
|
request.session['visit_count'] = request.session.get('visit_count') + 1
|
||||||
else:
|
else:
|
||||||
request.session['visit_count'] = 1
|
request.session['visit_count'] = 1
|
||||||
|
|
||||||
|
print("User {} visited {} times.".format(
|
||||||
|
request.session.session_key,
|
||||||
|
request.session['visit_count']))
|
||||||
|
Reference in New Issue
Block a user