Esempio n. 1
0
    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)
Esempio n. 2
0
    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()
Esempio n. 3
0
 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)