fairdm.templatetags.fairdm#
Template tags and filters used across FairDM templates.
Module Contents#
Classes#
A pint formatter that prints quantities with two decimals and unit symbols. |
Functions#
Return |
|
Write a concept as the code it was stored as before it had an address. |
|
Render the HTML of a unit. |
|
Return the registry configuration for a model, an instance or a queryset. |
|
Return a URL without its scheme and |
|
Return a model field by name. |
|
Return a model field together with its value on an instance. |
|
Return the fields named, each paired with its value on an instance. |
|
Return the update URL for an instance. |
|
Return the contributor’s image URL, or the default user icon markup when there is none. |
|
Return the URL of a plugin view for the current object. |
|
Render markdown to sanitised HTML, safe to output without further escaping. |
|
Check whether any of the comma-separated permissions is in the permission object. |
|
Check whether the user holds any of the comma-separated permissions. |
|
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.PrettyFormatterA 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 withurl.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, orNone.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
Nonewhen the model is not registered, because a template asking what the registry knows about something is asking, not asserting. Python callers useregistry.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
Nonewhen 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
Nonewhen 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
fieldand itsvalue.
- 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 offields.
- 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 afieldsquery parameter.Returns: The update URL, with the
fieldsquery parameter whenfieldsis 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 %}fromfairdm.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:
Truewhen 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_permissionsfirst, then falls back to the user’s own permissions.Args: context: The template context. perms: Comma-separated permission names.
Returns:
Truewhen at least one permission is held.
- get_related_field(obj, field_name)#
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 isNone.An attribute missing from the path before the last part raises
AttributeError.