wuttjamaican.diffs

Tools for displaying simple data diffs

class wuttjamaican.diffs.Diff(config, old_data: dict, new_data: dict, fields: list = None, columns=None, nature='update', old_color='#ffebe9', new_color='#dafbe1', cell_padding=None, extra_row_attrs=None)[source]

Represent / display a basic “diff” between two data records.

You must provide both the “old” and “new” data records, when constructing an instance of this class. Then call render_html() to display the diff table.

Parameters:
  • config – The app config object.

  • old_data – Dict of “old” data record.

  • new_data – Dict of “new” data record.

  • fields – Optional list of field names. If not specified, will be derived from the data records.

  • columns – Optional column names for the table. Default columns are: ["field name", "old value", "new value"]

  • nature – What sort of diff is being represented; must be one of: ("create", "update", "delete")

  • old_color – Background color to display for “old/deleted” field data, when applicable.

  • new_color – Background color to display for “new/created” field data, when applicable.

  • cell_padding – Optional override for cell padding style.

  • extra_row_attrs – Optional extra attributes to be applied to the table rows. See extra_row_attrs below.

extra_row_attrs

Optional extra attributes to be applied to the table rows.

This is normally set via the constructor. It is checked by get_row_attrs() when collecting the final attributes for a row.

If a dict is specified here, the attributes are applied to all rows.

If a callable is specified, it is called for each row to determine which attributes to apply. The callable signature is (field, is_diff, attrs) with params meaning:

  • field - name of the current row’s field

  • is_diff - whether the row field has a diff

  • attrs dict of row attrs collected so far

The callable should return a dict, which is then merged with attributes already collected.

get_row_attrs(field, is_diff=False)[source]

Collect the HTML attributes for a <tr> element.

The default logic here does not declare any attributes. However it does merge whatever comes from extra_row_attrs (if set) for the final attributes.

Parameters:
  • field – Name of the field for the row.

  • is_diff – Boolean indicating whether the row field has a diff.

Returns:

HTML attributes as dict

render_field_row(field)[source]

Render a complete <tr> element for the given field.

This calls other methods in pursuit of the final HTML:

  • get_row_attrs()

  • get_old_value_attrs()

  • get_new_value_attrs()

  • render_old_value()

  • render_new_value()

Parameters:

field – Name of the field to render.

Returns:

HTML literal <tr> element

render_html(template=None, **kwargs)[source]

Render the diff as HTML table.

Parameters:
  • template – Name of template to render, if you need to override the default.

  • **kwargs – Remaining kwargs are passed as context to the template renderer.

Returns:

HTML literal string