Code reference › domain › mail

class

MailContext

pretalx.mail.domain.context.MailContext source

Formatter and placeholders for one mail.

Rendering placeholder values can be expensive, so we keep them uncomputed and only render them when needed via the formatter's get_value. format_map renders the plain subject, the plain body and the HTML body, and MailContext allows all three to share a single render.

InheritsSafeFormatterFormatter

Methods 13

Defined here

get_value(key, args, kwargs)source

on pretalx.mail.domain.context.MailContext source

def get_value(self, key, args, kwargs):
    if key in self.context:
        return self.context[key]
    if key in self.placeholders:
        return self.render_placeholder(self.placeholders[key])
    if self.raise_on_missing:
        raise KeyError(key)
    return "{" + str(key) + "}"

on pretalx.common.text.formatting.SafeFormatter source

def get_value(self, key, args, kwargs):
    if not self.raise_on_missing and key not in self.context:
        return "{" + str(key) + "}"
    return self.context[key]

on string.Formatter

def get_value(self, key, args, kwargs):
    if isinstance(key, int):
        return args[key]
    else:
        return kwargs[key]
render_placeholder(placeholder)source
def render_placeholder(self, placeholder):
    identifier = placeholder.identifier
    if identifier in self.cache:
        return self.cache[identifier]
    try:
        value = placeholder.render(self.context_args)
    except (FieldFetchBlocked, DatabaseError):
        raise
    except Exception as e:
        raise MailPlaceholderError(
            f"Placeholder {identifier!r} raised {type(e).__name__}: {e!s}"
        ) from e
    self.cache[identifier] = value
    return value
__contains__(key)source
def __contains__(self, key):
    return key in self.context or key in self.placeholders
__getitem__(key)source
def __getitem__(self, key):
    return self.get_value(key, None, None)
__init__(*, context_args, placeholders, values)sourceInitialize self.

on pretalx.mail.domain.context.MailContext source

See help(type(self)) for accurate signature.

def __init__(self, *, context_args, placeholders, values):
    super().__init__(values)
    self.context_args = context_args
    self.placeholders = placeholders
    self.cache = {}

on pretalx.common.text.formatting.SafeFormatter source

Initialize self. See help(type(self)) for accurate signature.

def __init__(self, context, raise_on_missing=True, mode=MODE_PLAIN):
    self.context = context
    self.raise_on_missing = raise_on_missing
    self.mode = mode

From SafeFormatter pretalx.common.text.formatting

convert_field(value, conversion)source

on pretalx.common.text.formatting.SafeFormatter source

def convert_field(self, value, conversion):
    # Ignore any conversions (``{x!r}``, ``{x!s}``, ``{x!a}``) so
    # the output of ``{name}`` and ``{name!r}`` is identical.
    return value

on string.Formatter

def convert_field(self, value, conversion):
    # do any conversion on the resulting object
    if conversion is None:
        return value
    elif conversion == 's':
        return str(value)
    elif conversion == 'r':
        return repr(value)
    elif conversion == 'a':
        return ascii(value)
    raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
for_output(*, raise_on_missing, mode)sourceShallow clone, sharing instance state for better performance.
def for_output(self, *, raise_on_missing, mode):
    """Shallow clone, sharing instance state for better performance."""
    clone = copy.copy(self)
    clone.raise_on_missing = raise_on_missing
    clone.mode = mode
    return clone
format_field(value, format_spec)source

on pretalx.common.text.formatting.SafeFormatter source

def format_field(self, value, format_spec):
    # Ignore format_spec to block things like ``{x:!r}``.
    return super().format_field(self._prepare_value(value), "")

on string.Formatter

def format_field(self, value, format_spec):
    return format(value, format_spec)
get_field(field_name, args, kwargs)source

on pretalx.common.text.formatting.SafeFormatter source

def get_field(self, field_name, args, kwargs):
    return self.get_value(field_name, args, kwargs), field_name

on string.Formatter

# given a field_name, find the object it references. # field_name: the field being looked up, e.g. "0.name" # or "lookup[3]" # used_args: a set of which args have been used # args, kwargs: as passed in to vformat

def get_field(self, field_name, args, kwargs):
    first, rest = _string.formatter_field_name_split(field_name)

    obj = self.get_value(first, args, kwargs)

    # loop through the rest of the field_name, doing
    #  getattr or getitem as needed
    for is_attr, i in rest:
        if is_attr:
            obj = getattr(obj, i)
        else:
            obj = obj[i]

    return obj, first

From Formatter string

Standard string API, unchanged — check_unused_args(), format(), parse() and 1 more.