Ejemplo n.º 1
0
Archivo: multi.py Proyecto: jab/lektor
class ChoiceSource(object):

    def __init__(self, env, options):
        source = options.get('source')
        if source is not None:
            self.source = Expression(env, source)
            self.choices = None
            item_key = options.get('item_key') or '{{ this._id }}'
            item_label = options.get('item_label') or '{{ this._id }}'
        else:
            self.source = None
            self.choices = parse_choices(options.get('choices'))
            item_key = options.get('item_key') or '{{ this.0 }}'
            item_label = options.get('item_label') or '{{ this.1 }}'
        self.item_key = FormatExpression(env, item_key)
        self.item_label = FormatExpression(env, item_label)

    @property
    def has_choices(self):
        return self.source is not None or self.choices is not None

    def iter_choices(self, pad):
        if self.choices is not None:
            iterable = self.choices
        else:
            iterable = self.source.evaluate(pad)

        for item in iterable or ():
            key = self.item_key.evaluate(pad, this=item)
            label = self.item_label.evaluate(pad, this=item)
            yield key, label
Ejemplo n.º 2
0
class ChoiceSource(object):

    def __init__(self, env, options):
        source = options.get('source')
        if source is not None:
            self.source = Expression(env, source)
            self.choices = None
            item_key = options.get('item_key') or '{{ this._id }}'
            item_label = options.get('item_label')
        else:
            self.source = None
            self.choices = _parse_choices(options)
            item_key = options.get('item_key') or '{{ this.0 }}'
            item_label = options.get('item_label')
        self.item_key = FormatExpression(env, item_key)
        if item_label is not None:
            item_label = FormatExpression(env, item_label)
        self.item_label = item_label

    @property
    def has_choices(self):
        return self.source is not None or self.choices is not None

    def iter_choices(self, pad, record=None, alt=PRIMARY_ALT):
        values = {}
        if record is not None:
            values['record'] = record
        if self.choices is not None:
            iterable = self.choices
        else:
            try:
                iterable = self.source.evaluate(pad, alt=alt, values=values)
            except Exception:
                traceback.print_exc()
                iterable = ()

        for item in iterable or ():
            key = self.item_key.evaluate(pad, this=item, alt=alt,
                                         values=values)

            # If there is a label expression, use it.  Since in that case
            # we only have one language to fill in, we fill it in for the
            # default language
            if self.item_label is not None:
                label = {
                    'en': self.item_label.evaluate(pad, this=item, alt=alt,
                                                   values=values)
                }

            # Otherwise we create a proper internationalized key out of
            # our target label
            else:
                if isinstance(item, (tuple, list)) and len(item) == 2:
                    label = item[1]
                elif hasattr(item, 'get_record_label_i18n'):
                    label = item.get_record_label_i18n()
                else:
                    label = {'en': item['_id']}

            yield key, label
Ejemplo n.º 3
0
        def generate_tag_pages(source):
            if not self.has_config():
                return

            parent_path = self.get_parent_path()
            if source.path != parent_path:
                return

            pad = source.pad
            url_exp = FormatExpression(self.env, self.get_url_path_expression())

            for tag in self.get_all_tags(source):
                page = TagPage(source, tag)
                url_path = url_exp.evaluate(pad, this=page, values={'tag': tag})
                page.set_url_path(url_path)
                yield page
Ejemplo n.º 4
0
class ChoiceSource(object):
    def __init__(self, env, options):
        source = options.get('source')
        if source is not None:
            self.source = Expression(env, source)
            self.choices = None
            item_key = options.get('item_key') or '{{ this._id }}'
            item_label = options.get('item_label')
        else:
            self.source = None
            self.choices = _parse_choices(options)
            item_key = options.get('item_key') or '{{ this.0 }}'
            item_label = options.get('item_label')
        self.item_key = FormatExpression(env, item_key)
        if item_label is not None:
            item_label = FormatExpression(env, item_label)
        self.item_label = item_label

    @property
    def has_choices(self):
        return self.source is not None or self.choices is not None

    def iter_choices(self, pad, record=None, alt=PRIMARY_ALT):
        values = {}
        if record is not None:
            values['record'] = record
        if self.choices is not None:
            iterable = self.choices
        else:
            try:
                iterable = self.source.evaluate(pad, alt=alt, values=values)
            except Exception:
                traceback.print_exc()
                iterable = ()

        for item in iterable or ():
            key = self.item_key.evaluate(pad,
                                         this=item,
                                         alt=alt,
                                         values=values)

            # If there is a label expression, use it.  Since in that case
            # we only have one language to fill in, we fill it in for the
            # default language
            if self.item_label is not None:
                label = {
                    'en':
                    self.item_label.evaluate(pad,
                                             this=item,
                                             alt=alt,
                                             values=values)
                }

            # Otherwise we create a proper internationalized key out of
            # our target label
            else:
                if isinstance(item, (tuple, list)) and len(item) == 2:
                    label = item[1]
                elif hasattr(item, 'get_record_label_i18n'):
                    label = item.get_record_label_i18n()
                else:
                    label = {'en': item['_id']}

            yield key, label
 def format_display(self, display_item):
     record = display_item['record']
     display = display_item['display']
     exp = FormatExpression(self.pad.env, self.display)
     return exp.evaluate(self.pad, this=record, display=display)