Python API

altstreamfield.blocks.fields

class altstreamfield.blocks.fields.BooleanField(required=True, validators=(), help_text='', error_messages=None)

Represents a boolean value.

to_python(value)

Converts the JSON value to an equivalent Python value.

class altstreamfield.blocks.fields.CharField(max_length=None, min_length=None, strip=True, **kwargs)

Represents a single line of text.

to_python(value)

Ensures that the JSON value is converted to a proper Python string.

class altstreamfield.blocks.fields.ChoiceField(choices=(), **kwargs)

Represents a selection from a list of choices.

to_python(value)

Ensure that the value is a string or empty string.

valid_value(value)

Check to see if the provided value is a valid choice.

validate(value)

Custom validation to ensure that the value is in the list of choices.

class altstreamfield.blocks.fields.DocumentChooserField(required=True, validators=(), help_text='', error_messages=None)

Represents a selection of a Wagtail document.

property media

Allow the field to pass Media objects out to the altstreamfield.fields.BlockInput widget.

model

alias of wagtail.documents.models.Document

class altstreamfield.blocks.fields.Field(required=True, validators=(), help_text='', error_messages=None)

Base class for all fields.

get_args()

Returns the arguments needed to reproduce this instance in JavaScript.

get_dependencies()

If this field depends on any other blocks then override this method and return them here.

property media

Allow the field to pass Media objects out to the altstreamfield.fields.BlockInput widget.

run_validators(value)

Runs registered validators against the value and raises a ValidationError if any validators fail.

to_json(value)

Converts the Python value to an equivalent JSON value (a value that can be passed to json.dump).

to_python(value)

Converts the JSON value to an equivalent Python value.

validate(value)

Does basic validation that cannot be done with validators.

Override this method to perform custom validation.

class altstreamfield.blocks.fields.ImageChooserField(required=True, validators=(), help_text='', error_messages=None)

Represents a selection of a Wagtail Image.

property media

Allow the field to pass Media objects out to the altstreamfield.fields.BlockInput widget.

class altstreamfield.blocks.fields.IntegerField(min_value=None, max_value=None, **kwargs)

Represents and integer value.

to_python(value)

Validate that int() can be called on the input. Return the result of int() or None for empty values.

class altstreamfield.blocks.fields.ModelChooserField(required=True, validators=(), help_text='', error_messages=None)

Base class for Model based chooser fields.

to_json(value)

Converts the Python value to an equivalent JSON value (a value that can be passed to json.dump).

to_python(value)

Converts the JSON value to an equivalent Python value.

class altstreamfield.blocks.fields.RichTextField(required=True, validators=(), help_text='', error_messages=None)

Represents rich text.

property media

Allow the field to pass Media objects out to the altstreamfield.fields.BlockInput widget.

class altstreamfield.blocks.fields.TextField(max_length=None, min_length=None, strip=True, **kwargs)

Represents multiple lines of text.

altstreamfield.blocks.core

class altstreamfield.blocks.core.BaseBlock

Note this was taken directly from wagtail’s source code because we may need to modify it in the future.

class altstreamfield.blocks.core.DeclarativeFieldsMetaclass

Collect Fields declared on the base classes.

class altstreamfield.blocks.core.DeclarativeSubBlocksMetaclass

Metaclass that collects sub-blocks declared on the base classes. (cheerfully stolen from https://github.com/django/django/blob/master/django/forms/forms.py)

class altstreamfield.blocks.core.Block(*args, **kwargs)

Base class for all blocks.

check(**kwargs)

Hook for the Django system checks framework - returns a list of django.core.checks.Error objects indicating validity errors in the block

clean(value)

Validate value and return a cleaned version or throw a ValidationError if validation fails.

get_context(value, parent_context=None)

Return a dict of context variables (derived from the block value and combined with the parent_context) to be used as the template context when rendering this value through a template.

get_template(context=None)

Return the template to use for rendering the block if specified on meta class. This extraction was added to make dynamic templates possible if you override this method

property js_type

Allows retriving the class name from a template.

render(value, context=None)

Return a text rendering of ‘value’, suitable for display on templates. By default, this will use a template (with the passed context, supplemented by the result of get_context) if a ‘template’ property is specified on the block, and fall back on render_basic otherwise.

render_basic(value, context=None)

Return a text rendering of ‘value’, suitable for display on templates. render() will fall back on this if the block does not define a ‘template’ property.

render_edit_js(rendered_blocks=None)

Override this method do render custom JavaScript needed for editing this type of block.

property required

Flag used to determine whether labels for this block should display a ‘required’ asterisk. False by default, since Block does not provide any validation of its own - it’s up to subclasses to define what required-ness means.

to_json(value)

Converts the Python value to an equivalent JSON value (a value that can be passed to json.dump).

to_python(value)

Converts a JSON value to a Python version of the value.

validate(value)

To perform validation, override this method. Throw a ValidationError if validation fails.

Note: this validation should be done on the value returned from to_python().

class altstreamfield.blocks.core.UnknownBlock(*args, **kwargs)

This is a block that should be used when the block type cannot be found.

altstreamfield.blocks.structblock

class altstreamfield.blocks.structblock.StructBlock(*args, **kwargs)

A block that contains a “structure” of data.

This class describes a Block that can contain a dictionary of values collected by it’s fields that are used to render the block in various ways.

check(**kwargs)

Hook for the Django system checks framework - returns a list of django.core.checks.Error objects indicating validity errors in the block

clean(value)

Cleans all the data of every field and returns the cleaned StructValue instance.

render_basic(value, context=None)

Return a text rendering of ‘value’, suitable for display on templates. render() will fall back on this if the block does not define a ‘template’ property.

render_edit_js(rendered_blocks=None)

Override this method do render custom JavaScript needed for editing this type of block.

to_json(value)

Converts the StructValue to a dict for rendering to JSON.

to_python(value)

Converts a JSON value in to a Python version of the value, in this case a StructValue.

class altstreamfield.blocks.structblock.StructValue(block, *args)

A class that generates a StructBlock value from provided fields.

altstreamfield.blocks.streamblock

class altstreamfield.blocks.streamblock.StreamBlock(*args, **kwargs)

Represents a stream of sub-blocks.

This Block type specifies what kinds of blocks that it can contain and then stores those values in a StreamValue.

check(**kwargs)

Hook for the Django system checks framework - returns a list of django.core.checks.Error objects indicating validity errors in the block

clean(value)

Validate value and return a cleaned version or throw a ValidationError if validation fails.

get_default()

Default values set on StreamBlock should be a list of (type_name, value) tuples - we can’t use StreamValue directly because that would require a reference back to the StreamBlock that hasn’t been built yet.

get_searchable_content(value)

Gets all searchable content from the child blocks in this StreamBlock.

render_basic(value, context=None)

Very simple rendering of the stream block.

render_edit_js(rendered_blocks=None)

Override this method do render custom JavaScript needed for editing this type of block.

property required

Flag used to determine whether labels for this block should display a ‘required’ asterisk. False by default, since Block does not provide any validation of its own - it’s up to subclasses to define what required-ness means.

sorted_child_blocks()

Child blocks, sorted into their groups.

to_json(value)

Converts the Python value to an equivalent JSON value (a value that can be passed to json.dump).

to_python(value)

Converts the JSON representation to a Python Representation.

validate(value, errors=None)

Performs validation of the StreamBlock.

class altstreamfield.blocks.streamblock.StreamBlockField(block, *args, **kwargs)

A field that allows for nesting StreamBlocks.

get_args()

Returns the arguments needed to reproduce this instance in JavaScript.

get_dependencies()

Returns the dependent blocks as a dictionary.

to_json(value)

Converts the Python value to an equivalent JSON value (a value that can be passed to json.dump).

to_python(value)

Converts the JSON value to an equivalent Python value.

validate(value)

Does basic validation that cannot be done with validators.

Override this method to perform custom validation.

exception altstreamfield.blocks.streamblock.StreamBlockValidationError(block_errors=None, non_block_errors=None)

Represents an error caused when validating a StreamBlock’s value.

class altstreamfield.blocks.streamblock.StreamValue(stream_block, stream_data)

The default value type for a StreamBlock.

This organizes the sub-blocks into an array and allows for the subblocks to handle their own values.

class StreamChild(*args, **kwargs)

Extends bound block with methods that make sense in the context of children of StreamField, but not necessarily elsewhere that BoundBlock is used.

property block_type

Returns the block type of the block.

to_json()

Convert this StreamValue to a JSON representation.

altstreamfield.fields

class altstreamfield.fields.AltStreamField(block_type, **kwargs)

An alternate Django Model Field implementation of a Wagtail StreamField.

deconstruct()

Return enough information to recreate the field as a 4-tuple:

  • The name of the field on the model, if contribute_to_class() has been run.

  • The import path of the field, including the class:e.g. django.db.models.IntegerField This should be the most portable version, so less specific may be better.

  • A list of positional arguments.

  • A dict of keyword arguments.

Note that the positional or keyword arguments must contain values of the following types (including inner values of collection types):

  • None, bool, str, int, float, complex, set, frozenset, list, tuple, dict

  • UUID

  • datetime.datetime (naive), datetime.date

  • top-level classes, top-level functions - will be referenced by their full import path

  • Storage instances - these have their own deconstruct() method

This is because the values here must be serialized into a text format (possibly new Python code, possibly JSON) and these are the only types with encoding handlers defined.

There’s no need to return the exact way the field was instantiated this time, just ensure that the resulting field is the same - prefer keyword arguments over positional ones, and omit parameters with their default values.

formfield(**kwargs)

Return a django.forms.Field instance for this field.

get_prep_value(value)

Perform preliminary non-db specific value checks and conversions.

to_python(value)

Convert the input value into the expected Python data type, raising django.core.exceptions.ValidationError if the data can’t be converted. Return the converted value. Subclasses should override this.

value_to_string(obj)

Return a string value of this field from the passed obj. This is used by the serialization framework.

class altstreamfield.fields.StreamBlockField(block=None, **kwargs)

A Django Form Field for collecting StreamBlock data.

class altstreamfield.fields.StreamBlockInput(block, *args, **kwargs)

A Django Form Widget for collecting StreamBlock data.