コード例 #1
0
ファイル: multi.py プロジェクト: uniteddiversity/lektor
 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
コード例 #2
0
ファイル: multi.py プロジェクト: uniteddiversity/lektor
class ChoiceSource:
    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
コード例 #3
0
    def get_pagination_query(self, record):
        items_expr = self.items
        if items_expr is None:
            return record.children
        if self._items_tmpl is None or self._items_tmpl[0] != items_expr:
            self._items_tmpl = (items_expr, Expression(self.env, items_expr))

        return self._items_tmpl[1].evaluate(record.pad, this=record)
コード例 #4
0
    def get_child_replacements(self, record):
        """Returns the query that should be used as replacement for the
        actual children.
        """
        replaced_with = self.child_config.replaced_with
        if replaced_with is None:
            return None

        if (self._child_replacements is None
                or self._child_replacements[0] != replaced_with):
            self._child_replacements = (
                replaced_with,
                Expression(self.env, replaced_with),
            )

        return self._child_replacements[1].evaluate(record.pad, this=record)
コード例 #5
0
 def eval_expr(expr, **kwargs):
     expr = Expression(env, expr)
     return expr.evaluate(**kwargs)