7

adding cloudfront device detection rule

This commit is contained in:
Pim Vernooij
2016-12-12 20:44:21 +01:00
parent 426770e1d5
commit 7c52d318ff
2 changed files with 55 additions and 1 deletions

View File

@ -24,10 +24,16 @@ class VisitCountRuleAdminInline(admin.TabularInline):
model = models.VisitCountRule model = models.VisitCountRule
extra = 0 extra = 0
class CloudfrontDeviceTypeRuleAdminInline(admin.TabularInline):
"""Inline the Cloudfront DeviceType rule into the
administration interface for segments"""
model = models.CloudfrontDeviceTypeRule
extra = 0
class SegmentAdmin(admin.ModelAdmin): class SegmentAdmin(admin.ModelAdmin):
"""Add the inlines to the Segment admin interface""" """Add the inlines to the Segment admin interface"""
inlines = (TimeRuleAdminInline, inlines = (TimeRuleAdminInline, CloudfrontDeviceTypeRuleAdminInline,
ReferralRuleAdminInline, VisitCountRuleAdminInline) ReferralRuleAdminInline, VisitCountRuleAdminInline)

View File

@ -93,6 +93,54 @@ class ReferralRule(AbstractBaseRule):
return 'Referral Rule' return 'Referral Rule'
@python_2_unicode_compatible
class CloudfrontDeviceTypeRule(AbstractBaseRule):
"""Referral rule to segment users based on a their device type as it was
detected by Cloudfront"""
is_tablet = models.BooleanField(default=False)
is_smartphone = models.BooleanField(default=False)
is_desktop = models.BooleanField(default=False)
is_smarttv = models.BooleanField(default=False)
panels = [
FieldPanel('is_tablet'),
FieldPanel('is_smartphone'),
FieldPanel('is_desktop'),
FieldPanel('is_smarttv'),
]
def __init__(self, *args, **kwargs):
super(CloudfrontDeviceTypeRule, self).__init__(*args, **kwargs)
def test_user(self, request):
"""test different cloudfront headers. If those are not available,
False will be returned"""
return (
self.is_smartphone == self._header_value(request,
'HTTP_CLOUDFRONT_IS_MOBILE_VIEWER')
or self.is_tablet == self._header_value(request,
'HTTP_CLOUDFRONT_IS_TABLET_VIEWER')
or self.is_desktop == self._header_value(request,
'HTTP_CLOUDFRONT_IS_DESKTOP_VIEWER')
or self.is_smarttv == self._header_value(request,
'HTTP_CLOUDFRONT_IS_SMARTTV_VIEWER')
)
def _header_value(self, request, header):
header_value = request.META.get(header, None),
if None not in header_value:
return True if 'true' in header_value else False
return None
def __str__(self):
return 'Cloudfront Device Type Rule'
@python_2_unicode_compatible @python_2_unicode_compatible
class VisitCountRule(AbstractBaseRule): class VisitCountRule(AbstractBaseRule):
"""Visit count rule to segment users based on amount of visits""" """Visit count rule to segment users based on amount of visits"""