Jump to >

reviewboard.webapi.mixins

class MarkdownFieldsMixin[source]

Bases: object

Mixes in common logic for Markdown text fields.

Any resource implementing this is assumed to have at least one Markdown-capable text field.

Clients can pass ?force-text-type= (for GET) or force_text_type= (for POST/PUT) with a value of plain, markdown or html to return the given text fields in the payload using the requested format.

When markdown is specified, the Markdown text fields will return valid Markdown content, escaping if necessary.

When plain is specified, plain text will be returned instead. If the content was in Markdown before, this will unescape the content.

When html is specified, the content will be transformed into HTML suitable for display.

Clients can also pass ?include-text-types=<type1>[,<type2>,...] (for GET) or include_text_types=<type1>[,<type2>,...] (for POST/PUT) to return the text fields within special type_text_fields entries in the resource payload. A special type of “raw” is allowed, which will return the text types stored in the database.

(Note that passing ?include-text-types=raw is equivalent to passing ?include-raw-text-fields=1 in 2.0.9 and 2.0.10. The latter is deprecated.)

TEXT_TYPE_PLAIN = u’plain’[source]
TEXT_TYPE_MARKDOWN = u’markdown’[source]
TEXT_TYPE_HTML = u’html’[source]
TEXT_TYPE_RAW = u’raw’[source]
TEXT_TYPES = (u’plain’, u’markdown’, u’html’)[source]
SAVEABLE_TEXT_TYPES = (u’plain’, u’markdown’)[source]
INCLUDEABLE_TEXT_TYPES = (u’plain’, u’markdown’, u’html’, u’raw’)[source]
DEFAULT_EXTRA_DATA_TEXT_TYPE = u’markdown’[source]
serialize_object(obj, *args, **kwargs)[source]

Serializes the object, transforming text fields.

This is a specialization of serialize_object that transforms any text fields that support text types. It also handles attaching the raw text to the payload, on request.

serialize_text_type_field(obj, request=None, **kwargs)[source]
can_import_extra_data_field(obj, field)[source]

Returns whether a particular field in extra_data can be imported.

If an extra_data field is marked as supporting rich text, we’ll skip importing it through normal means. Instead, it will be handled separately later.

get_extra_data_field_supports_markdown(obj, key)[source]

Returns whether a particular field in extra_data supports Markdown.

If the field supports Markdown text, the value will be normalized based on the requested ?force-text-type= parameter.

set_text_fields(obj, text_field, rich_text_field_name=None, text_type_field_name=None, text_model_field=None, **kwargs)[source]

Normalizes Markdown-capable text fields that are being saved.

set_extra_data_text_fields(obj, text_field, extra_fields, **kwargs)[source]

Normalizes Markdown-capable text fields in extra_data.

This will check if any Markdown-capable text fields in extra_data have been changed (either by changing the text or the text type), and handle the saving of the text and type.

This works just like set_text_fields, but specially handles how things are stored in extra_data (text_type vs. rich_text fields, possible lack of presence of a text_type field, etc.).

class UpdateFormMixin[source]

Bases: djblets.webapi.resources.mixins.forms.UpdateFormMixin

A mixin for providing the ability to create and update using a form.

A WebAPIResource class using this mixin must set the form_class attribute to a ModelForm instance that corresponds to the model being updated.

Classes using this mixin can provide methods of the form parse_<field_name>_field to do parsing of form data before it is passed to the form. These methods may return either a single value or a list (in the case where the corresponding field expects a list of values, such as a django.forms.ModelMultipleChoiceField).

The create_form() and save_form() methods should be used for creating new form instances and saving them. A form created this way can be given an optional instance argument to allow for updating the instance. Any fields missing from data (but appearing in the form_class’s fields attribute) will be copied over from the instance.

save_form(form, extra_fields=None)[source]

Save the form and extra data.

Parameters:
  • form (django.forms.ModelForm) – The form to save.
  • extra_fields (dict) – The extra data to save on the object. These should be key-value pairs in the form of extra_data.key = value.
Returns:

The saved model instance.

Return type:

django.db.models.Model