fairdm.utils.utils#

Small helpers for models, settings, documentation URLs and crispy layouts.

Module Contents#

Functions#

user_guide

Return the documentation URL for a given name.

get_setting

Return a value from a FAIRDM dictionary setting.

get_subclasses

Return the registered Django model subclasses of a given model.

get_inheritance_chain

Return the inheritance chain of a model up to a base model.

get_model_class

Return the core model class a UUID belongs to.

get_core_object_or_none

Return the model class and the object matching a UUID.

get_core_object_or_404

Return the core object with a shortuuid primary key, or raise a 404.

default_image_path

Generate the upload path for an image.

fieldsets_to_crispy_layout

Convert Django fieldsets into a crispy-forms Layout.

fields_to_crispy_layout

Convert a flat list of fields, or tuples and lists of fields, into a crispy-forms Layout.

fairdm_fieldsets_to_django

Convert FairDM-style fieldsets to Django-style fieldsets.

Data#

API#

DOCUMENTATION_BASE_URL#

‘https://docs.fairdm.org/latest/users/’

user_guide(name: str) → str[source]#

Return the documentation URL for a given name.

Args: name: The name of the documentation section.

Returns: The URL to the documentation section.

get_setting(name: str, key: str)[source]#

Return a value from a FAIRDM dictionary setting.

Args: name: The name of the FAIRDM setting (without FAIRDM_ prefix). key: The key within the setting dictionary.

Returns: The setting value or None if the key is not found.

get_subclasses(model)[source]#

Return the registered Django model subclasses of a given model.

Iterates the app registry’s models and keeps those that are subclasses of model, excluding the model itself.

Args: model: The Django model class for which to find subclasses.

Returns: The direct and indirect subclasses of model.

Example: >>> class A(models.Model): … pass >>> class B(A): … pass >>> class C(B): … pass >>> get_subclasses(A) [B, C]

get_inheritance_chain(model, base_model)[source]#

Return the inheritance chain of a model up to a base model.

Walks the model’s method resolution order and collects every model class that is a subclass of base_model.

Args: model: The Django model class for which to determine the inheritance chain. base_model: The base model class where the chain ends.

Returns: The model classes in the chain, starting from model and ending at base_model.

Example: >>> class A(models.Model): … pass >>> class B(A): … pass >>> class C(B): … pass >>> get_inheritance_chain(C, A) [C, B, A]

get_model_class(uuid: str)[source]#

Return the core model class a UUID belongs to.

The UUID’s first character identifies the model.

Args: uuid: A core object’s shortuuid primary key.

Returns: The model class.

get_core_object_or_none(uuid: str) → tuple[source]#

Return the model class and the object matching a UUID.

Args: uuid: The UUID of the object to retrieve.

Returns: A tuple of the model class and the first object with that UUID, or None in place of the object when none exists.

get_core_object_or_404(uuid: str)[source]#

Return the core object with a shortuuid primary key, or raise a 404.

Args: uuid: A core object’s shortuuid primary key.

Returns: The matching object. Http404 is raised when no object has that UUID.

default_image_path(instance, filename: str) → str[source]#

Generate the upload path for an image.

Args: instance: The model instance the image is being uploaded to. filename: The original filename of the uploaded image.

Returns: A relative file path for storing the image.

fieldsets_to_crispy_layout(fieldsets)[source]#

Convert Django fieldsets into a crispy-forms Layout.

Takes fieldsets typically defined in Django’s admin.py and transforms them into a crispy-forms Layout, grouping fields into Fieldset containers and organizing grouped fields into Row and Column structures. The fields and help_text entries are popped from each options dict, so the input is modified.

Args: fieldsets: A list of tuples, each containing: - legend (str or None): The title of the fieldset. - options (dict): A dictionary holding "fields", a list of field names or tuples/lists of field names to be grouped, and optionally "help_text".

Returns: A crispy-forms Layout representing the given fieldsets.

Example: >>> fieldsets = [ >>> (“Personal Info”, {“fields”: [“first_name”, “last_name”]}), >>> (“Contact”, {“fields”: [(“email”, “phone”)]}), >>> ] >>> fieldsets_to_crispy_layout(fieldsets) Layout( Fieldset(“Personal Info”, “first_name”, “last_name”), Fieldset(“Contact”, Row(Column(“email”), Column(“phone”))) )

fields_to_crispy_layout(fields)[source]#

Convert a flat list of fields, or tuples and lists of fields, into a crispy-forms Layout.

Single field names are added directly. Tuples and lists of field names are wrapped in Columns inside a Row.

Args: fields: Field names, or tuples or lists of field names to place side by side.

Returns: A crispy-forms Layout.

fairdm_fieldsets_to_django(fieldsets)[source]#

Convert FairDM-style fieldsets to Django-style fieldsets.

Args: fieldsets: A dict mapping each legend to its options dict.

Returns: A tuple of (legend, options) pairs.