Example #1
0
    def renderwob(self, md):
        """RENDER WithOutBatch"""
        expr = self.expr
        name = self.__name__
        if expr is None:
            sequence = md[name]
            cache = {name: sequence }
        else:
            sequence = expr(md)
            cache = None

        if not sequence:
            if self.elses:
                return render_blocks(self.elses, md)
            return ''

        if isinstance(sequence, StringType):
            raise InError('Strings are not allowed as input to the in tag.')

        section = self.section
        mapping = self.mapping


        if self.sort_expr is not None:
            self.sort = self.sort_expr.eval(md)
            sequence = self.sort_sequence(sequence)
        elif self.sort is not None:
            sequence = self.sort_sequence(sequence)

        if self.reverse_expr is not None and self.reverse_expr.eval(md):
            sequence = self.reverse_sequence(sequence)
        elif self.reverse is not None:
            sequence = self.reverse_sequence(sequence)

        vars = sequence_variables(sequence)
        kw = vars.data
        kw['mapping'] = mapping

        l = len(sequence)
        last = l - 1

        push = md._push
        pop = md._pop
        render = render_blocks
        get = self.args.get

        if cache:
            push(cache)
        push(vars)
        try:
            result = []
            append = result.append
            validate = md.validate
            for index in range(l):
                if index == last:
                    kw['sequence-end'] = 1
                client = sequence[index]

                if validate is not None:
                    try:
                        vv = validate(sequence, sequence, None, client, md)
                    except:
                        vv = 0
                    if not vv:
                        if get('skip_unauthorized'):
                            if index == 1:
                                kw['sequence-start'] = 0
                            continue
                        raise ValidationError(index)

                kw['sequence-index'] = index
                if isinstance(client, TupleType) and len(client) == 2:
                    client = client[1]

                if mapping:
                    push(client)
                else:
                    md._push_instance(client)

                try:
                    append(render(section, md))
                finally:
                    pop()
                if index == 0:
                    kw['sequence-start'] = 0

            result = ''.join(result)

        finally:
            if cache:
                pop()
            pop()

        return result
Example #2
0
    def renderwb(self, md):
        expr = self.expr
        name = self.__name__
        if expr is None:
            sequence = md[name]
            cache = {name: sequence }
        else:
            sequence = expr(md)
            cache = None

        if not sequence:
            if self.elses:
                return render_blocks(self.elses, md)
            return ''

        if isinstance(sequence, StringType):
            raise InError('Strings are not allowed as input to the in tag.')


        section = self.section
        params = self.args

        mapping = self.mapping

        if self.sort_expr is not None:
            self.sort = self.sort_expr.eval(md)
            sequence = self.sort_sequence(sequence)
        elif self.sort is not None:
            sequence = self.sort_sequence(sequence)

        if self.reverse_expr is not None and self.reverse_expr.eval(md):
            sequence = self.reverse_sequence(sequence)
        elif self.reverse is not None:
            sequence = self.reverse_sequence(sequence)

        next = previous = 0
        try:
            start = int_param(params, md, 'start', 0)
        except:
            start=1
        end = int_param(params, md, 'end', 0)
        size = int_param(params, md, 'size', 0)
        overlap = int_param(params, md, 'overlap', 0)
        orphan = int_param(params, md, 'orphan', '3')
        start, end, sz = opt(start, end, size, orphan, sequence)
        if params.has_key('next'):
            next = 1
        if params.has_key('previous'):
            previous = 1

        last = end - 1
        first = start - 1

        try:
            query_string = md['QUERY_STRING']
        except:
            query_string = ''

        vars = sequence_variables(sequence,'?' + query_string,
                                  self.start_name_re)
        kw = vars.data
        kw['mapping'] = mapping
        kw['sequence-step-size'] = sz
        kw['sequence-step-overlap'] = overlap
        kw['sequence-step-start'] = start
        kw['sequence-step-end'] = end
        kw['sequence-step-start-index'] = start - 1
        kw['sequence-step-end-index'] = end - 1
        kw['sequence-step-orphan'] = orphan

        push = md._push
        pop = md._pop
        render = render_blocks

        if cache:
            push(cache)
        push(vars)
        try:
            if previous:
                if first > 0:
                    pstart, pend, psize = opt(0, first+overlap,
                                              sz, orphan, sequence)
                    kw['previous-sequence'] = 1
                    kw['previous-sequence-start-index'] = pstart - 1
                    kw['previous-sequence-end-index'] = pend - 1
                    kw['previous-sequence-size'] = pend + 1 - pstart
                    result=render(section,md)

                elif self.elses:
                    result = render(self.elses, md)
                else:
                    result = ''
            elif next:
                try:
                    # The following line is a sneaky way to test whether
                    # there are more items, without actually
                    # computing a length:
                    sequence[end]
                except IndexError:
                    if self.elses:
                        result = render(self.elses, md)
                    else:
                        result = ''
                else:
                    pstart, pend, psize = opt(end+1-overlap, 0,
                                              sz, orphan, sequence)
                    kw['next-sequence'] = 1
                    kw['next-sequence-start-index'] = pstart - 1
                    kw['next-sequence-end-index'] = pend - 1
                    kw['next-sequence-size'] = pend + 1 - pstart
                    result = render(section, md)
            else:
                result = []
                append = result.append
                validate = md.validate
                for index in range(first,end):
                    # preset
                    kw['previous-sequence'] = 0
                    # now more often defined then previously
                    kw['next-sequence'] = 0

                    if index==first or index==last:
                        # provide batching information
                        if first > 0:
                            pstart, pend, psize = opt(0, first + overlap,
                                                      sz, orphan, sequence)
                            if index == first:
                                kw['previous-sequence'] = 1
                            kw['previous-sequence-start-index'] = pstart - 1
                            kw['previous-sequence-end-index'] = pend - 1
                            kw['previous-sequence-size'] = pend + 1 - pstart
                        try:
                            # The following line is a sneaky way to
                            # test whether there are more items,
                            # without actually computing a length:
                            sequence[end]
                            pstart, pend, psize = opt(end + 1 - overlap, 0,
                                                      sz, orphan, sequence)
                            if index == last:
                                kw['next-sequence'] = 1
                            kw['next-sequence-start-index'] = pstart - 1
                            kw['next-sequence-end-index'] = pend - 1
                            kw['next-sequence-size'] = pend + 1 - pstart
                        except:
                            pass

                    if index == last:
                        kw['sequence-end'] = 1

                    client = sequence[index]

                    if validate is not None:
                        try:
                            vv = validate(sequence, sequence, None, client,md)
                        except:
                            vv = 0
                        if not vv:
                            if (params.has_key('skip_unauthorized') and
                                params['skip_unauthorized']):
                                if index == first:
                                    kw['sequence-start'] = 0
                                continue
                            raise ValidationError(index)

                    kw['sequence-index'] = index
                    if isinstance(client, TupleType) and len(client) == 2:
                        client = client[1]

                    if mapping:
                        push(client)
                    else:
                        md._push_instance(client)

                    try:
                        append(render(section, md))
                    finally:
                        pop(1)

                    if index == first:
                        kw['sequence-start'] = 0

                result = ''.join(result)

        finally:
            if cache:
                pop()
            pop()

        return result