def __init__(self, *args, **kwargs): Segment.panels = [ MultiFieldPanel([ FieldPanel('name', classname="title"), FieldRowPanel([ FieldPanel('status'), FieldPanel('persistent'), ]), FieldPanel('match_any'), FieldPanel('type', widget=forms.RadioSelect), FieldPanel('count', classname='count_field'), FieldPanel('randomisation_percent', classname='percent_field'), ], heading="Segment"), MultiFieldPanel([ InlinePanel( "{}s".format(rule_model._meta.db_table), label='{}{}'.format( rule_model._meta.verbose_name, ' ({})'.format(_('Static compatible')) if rule_model.static else '' ), ) for rule_model in AbstractBaseRule.__subclasses__() ], heading=_("Rules")), ] super(Segment, self).__init__(*args, **kwargs)
def refresh(self): """Retrieve the request session segments and verify whether or not they still apply to the requesting visitor. """ enabled_segments = Segment.objects.enabled() rule_models = AbstractBaseRule.get_descendant_models() current_segments = self.get_segments() # Run tests on all remaining enabled segments to verify applicability. additional_segments = [] for segment in enabled_segments: if segment.is_static and segment.static_users.filter(id=self.request.user.id).exists(): additional_segments.append(segment) elif not segment.is_static or not segment.is_full: segment_rules = [] for rule_model in rule_models: segment_rules.extend(rule_model.objects.filter(segment=segment)) result = self._test_rules(segment_rules, self.request, match_any=segment.match_any) if result and segment.is_static and not segment.is_full: if self.request.user.is_authenticated(): segment.static_users.add(self.request.user) if result: additional_segments.append(segment) self.set_segments(current_segments + additional_segments) self.update_visit_count()
def refresh(self): """Retrieve the request session segments and verify whether or not they still apply to the requesting visitor. """ enabled_segments = Segment.objects.filter(status='enabled') persistent_segments = enabled_segments.filter(persistent=True) session_segments = self.request.session['segments'] rules = AbstractBaseRule.__subclasses__() # Create a list to store the new request session segments and # re-apply all persistent segments (if they are still enabled). new_segments = [segment for segment in session_segments if persistent_segments.filter(id=segment['id']).exists()] # Run tests on all remaining enabled segments to verify applicability. for segment in enabled_segments: segment_rules = [] for rule in rules: segment_rules += rule.objects.filter(segment=segment) result = self._test_rules(segment_rules, self.request, match_any=segment.match_any) if result: segdict = create_segment_dictionary(segment) if not any(seg['id'] == segdict['id'] for seg in new_segments): new_segments.append(segdict) self.request.session['segments'] = new_segments self.update_visit_count()
def get_rules(self): """Retrieve all rules in the segment.""" segment_rules = [] for rule_model in AbstractBaseRule.get_descendant_models(): segment_rules.extend( rule_model._default_manager.filter(segment=self)) return segment_rules
def get_rules(self): """Retrieve all rules in the segment.""" rules = AbstractBaseRule.__subclasses__() segment_rules = [] for rule in rules: segment_rules += rule.objects.filter(segment=self) return segment_rules
def __init__(self, *args, **kwargs): Segment.panels = [ MultiFieldPanel([ FieldPanel('name', classname="title"), FieldRowPanel([ FieldPanel('status'), FieldPanel('persistent'), ]), FieldPanel('match_any'), ], heading="Segment"), MultiFieldPanel([ InlinePanel( "{}_related".format(rule_model._meta.db_table), label=rule_model._meta.verbose_name, ) for rule_model in AbstractBaseRule.__subclasses__() ], heading=_("Rules")), ] super(Segment, self).__init__(*args, **kwargs)