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