fairdm.templatetags.fairdm#

Template tags and filters used across FairDM templates.

Module Contents#

Classes#

MyFormatter

A pint formatter that prints quantities with two decimals and unit symbols.

Functions#

is_active

Return "active" when the current request path starts with url.

notation_filter

Write a concept as the code it was stored as before it had an address.

unit

Render the HTML of a unit.

get_registry_info

Return the registry configuration for a model, an instance or a queryset.

display_url

Return a URL without its scheme and www. prefix, for display.

get_field

Return a model field by name.

get_field_and_value

Return a model field together with its value on an instance.

get_fields

Return the fields named, each paired with its value on an instance.

edit_url

Return the update URL for an instance.

avatar_url

Return the contributor’s image URL, or the default user icon markup when there is none.

plugin_url

Return the URL of a plugin view for the current object.

safe_markdown

Render markdown to sanitised HTML, safe to output without further escaping.

has_perms

Check whether any of the comma-separated permissions is in the permission object.

has_permission

Check whether the user holds any of the comma-separated permissions.

get_related_field

Drill down an object’s attributes using Django-style double-underscore notation.

Data#

API#

register#

‘Library(…)’

ureg#

None

class MyFormatter(registry: pint.registry.UnitRegistry | None = None)#

Bases: pint.delegates.formatter.plain.PrettyFormatter

A pint formatter that prints quantities with two decimals and unit symbols.

Initialization

default_format#

‘.2f~P’

format_uncertainty(uncertainty, unc_spec: str = '', sort_func=None, **babel_kwds) → str#

Format an uncertainty with spaces around the ± sign.

format_measurement(measurement, meas_spec='', sort_func=None, **babel_kwds) → str#

Format a measurement without the surrounding parentheses.

is_active(context, url)#

Return "active" when the current request path starts with url.

Args: context: The template context, which must hold request. url: The path prefix to compare against.

Returns: "active" or an empty string.

notation_filter(concept)#

Write a concept as the code it was stored as before it had an address.

Args: concept: A controlled_vocabularies.Record, or None.

Returns: The concept’s skos:notation, or an empty string.

unit(unit)#

Render the HTML of a unit.

Args: unit: A unit, or a unit string such as "m" or "m/s".

Returns: The unit formatted as HTML with symbols.

get_registry_info(model_or_qs)#

Return the registry configuration for a model, an instance or a queryset.

Returns None when the model is not registered, because a template asking what the registry knows about something is asking, not asserting. Python callers use registry.get_for_model(), which raises.

Args: model_or_qs: A model class, a model instance or a queryset.

Returns: The model’s registry configuration, or None when it is not registered.

display_url(url)#

Return a URL without its scheme and www. prefix, for display.

Args: url: The URL to shorten.

Returns: The shortened URL.

get_field(obj, fname)#

Return a model field by name.

Args: obj: A model instance or class. fname: The field name.

Returns: The field, or None when the model has no such field.

get_field_and_value(obj, fname)#

Return a model field together with its value on an instance.

Args: obj: The model instance. fname: The field name.

Returns: A dict with the field and its value.

get_fields(obj, fields)#

Return the fields named, each paired with its value on an instance.

Args: obj: The model instance. fields: The field names.

Returns: A list of (field, value) tuples in the order of fields.

edit_url(obj, fields=None)#

Return the update URL for an instance.

Args: obj: The model instance, which needs a uuid. fields: Field names to restrict the form to, sent as a fields query parameter.

Returns: The update URL, with the fields query parameter when fields is given.

avatar_url(contributor, **kwargs)#

Return the contributor’s image URL, or the default user icon markup when there is none.

Args: contributor: The contributor, or a falsy value for an anonymous user. **kwargs: Ignored.

Returns: The image URL, or the rendered default icon.

plugin_url(context, view_name, *args, **kwargs)#

Return the URL of a plugin view for the current object.

Deprecated: use {% plugin_url %} from fairdm.contrib.plugins.templatetags.plugin_tags, which replaces this tag.

Args: context: The template context, which supplies the object. view_name: The plugin view name. *args: Positional arguments for the URL. **kwargs: Keyword arguments for the URL.

Returns: The URL, or an empty string when the context holds no object.

safe_markdown(content)#

Render markdown to sanitised HTML, safe to output without further escaping.

Args: content: The markdown source.

Returns: The sanitised HTML, marked safe.

has_perms(permission_obj, perms)#

Check whether any of the comma-separated permissions is in the permission object.

Args: permission_obj: A collection of permission names. perms: Comma-separated permission names.

Returns: True when at least one of them is present.

has_permission(context, perms)#

Check whether the user holds any of the comma-separated permissions.

Checks the context’s user_permissions first, then falls back to the user’s own permissions.

Args: context: The template context. perms: Comma-separated permission names.

Returns: True when at least one permission is held.

Drill down an object’s attributes using Django-style double-underscore notation.

Args: obj: The root model instance. field_name: A path such as "reference__publisher__name", of any depth.

Returns: A tuple (final_obj, final_attr): the object holding the last attribute and that attribute’s name. (None, None) when an intermediate object is None.

An attribute missing from the path before the last part raises AttributeError.