fairdm.utils.utils#
Small helpers for models, settings, documentation URLs and crispy layouts.
Module Contents#
Functions#
Return the documentation URL for a given name. |
|
Return a value from a FAIRDM dictionary setting. |
|
Return the registered Django model subclasses of a given model. |
|
Return the inheritance chain of a model up to a base model. |
|
Return the core model class a UUID belongs to. |
|
Return the model class and the object matching a UUID. |
|
Return the core object with a shortuuid primary key, or raise a 404. |
|
Generate the upload path for an image. |
|
Convert Django fieldsets into a crispy-forms Layout. |
|
Convert a flat list of fields, or tuples and lists of fields, into a crispy-forms Layout. |
|
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
modeland ending atbase_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
Nonein 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.
Http404is 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.pyand transforms them into a crispy-formsLayout, grouping fields intoFieldsetcontainers and organizing grouped fields intoRowandColumnstructures. Thefieldsandhelp_textentries 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
Layoutrepresenting 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.