diff --git a/src/personalisation/admin.py b/src/personalisation/admin.py index 7d710bb..b2af20c 100644 --- a/src/personalisation/admin.py +++ b/src/personalisation/admin.py @@ -24,10 +24,16 @@ class VisitCountRuleAdminInline(admin.TabularInline): model = models.VisitCountRule 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): """Add the inlines to the Segment admin interface""" - inlines = (TimeRuleAdminInline, + inlines = (TimeRuleAdminInline, CloudfrontDeviceTypeRuleAdminInline, ReferralRuleAdminInline, VisitCountRuleAdminInline) diff --git a/src/personalisation/models.py b/src/personalisation/models.py index ac2a4ce..1141240 100644 --- a/src/personalisation/models.py +++ b/src/personalisation/models.py @@ -93,6 +93,54 @@ class ReferralRule(AbstractBaseRule): 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 class VisitCountRule(AbstractBaseRule): """Visit count rule to segment users based on amount of visits"""