fairdm.contrib.autocomplete.fields#

Form fields for autocomplete functionality.

Module Contents#

Classes#

RecordSelectMixin

Draw the records a field has chosen the way the field sees them.

RecordSelectWidget

Choose one record.

RecordSelectMultipleWidget

Choose several records.

RecordFieldMixin

What a field that chooses records of one kind shares, single or multiple.

RecordChoiceFieldMixin

Choose one record.

RecordMultipleChoiceFieldMixin

Choose several records.

PublicationChoiceField

Choose one publication by searching its title, authors or DOI.

TagsWidget

Choose tags, which are words and not records, so the chosen ones need no lookup.

TagsField

Choose tags, drawn as chips with a remove button, cleaned to a list of stripped words.

RorChoiceField

Choose an organization from the Research Organization Registry by searching its name.

Functions#

record_fields

Make the single and the multiple field that choose one kind of record.

API#

class RecordSelectMixin#

Draw the records a field has chosen the way the field sees them.

A person cannot be shown the name of a record they may not see, so the field decides how each chosen record is labelled.

field#

None

get_autocomplete_params()#

Name the owner whose records the profile address offers.

get_context(name, value, attrs=None)#

Tell the page which field’s value to put first and which type to keep.

class RecordSelectWidget(config: django_tomselect.app_settings.TomSelectConfig | dict[str, Any] | None = None, **kwargs: Any)#

Bases: fairdm.contrib.autocomplete.fields.RecordSelectMixin, django_tomselect.widgets.TomSelectModelWidget

Choose one record.

Initialization

Initialize widget with model-specific attributes.

class RecordSelectMultipleWidget(config: django_tomselect.app_settings.TomSelectConfig | dict[str, Any] | None = None, **kwargs: Any)#

Bases: fairdm.contrib.autocomplete.fields.RecordSelectMixin, django_tomselect.widgets.TomSelectModelMultipleWidget

Choose several records.

Initialization

Initialize widget with model-specific attributes.

class RecordFieldMixin(*args, scope=None, owner=None, type=None, depends_on=None, prefer=None, **kwargs)#

What a field that chooses records of one kind shares, single or multiple.

The field offers, and accepts, the records its view offers the person at its scope. It never asks django-tomselect to validate, which rebuilds the view from the current page’s search and filter parameters.

Args: scope: How much the person may be offered: "public", "view" or "edit". Defaults to the field’s default_scope. Not given with owner. owner: A person whose records the field offers, as a person or a primary key: their private records only when they are the one using the field, public records to everyone else. For a filter on a list of that person’s records. Not for people or organizations. type: A registered sample or measurement type the field keeps, given as its model. Only for samples and measurements. depends_on: The name of another field on the same form whose value the records must match, such as "dataset" for a sample. It must be a filter the view allows. prefer: The name of another field on the same form whose value the records should match to be listed first, in a group of their own. Unlike depends_on it never narrows what the field offers or accepts. It must be a filter the view allows. **kwargs: Passed to the form field.

Attributes: view_class: The record view that decides what is offered. kind: The kind of record, as it appears in the view’s address. scopes: The scopes the kind offers. A person or an organization is offered at one. default_scope: The scope used when none is given. multiple: Whether the field chooses several records. label: The field’s label when none is given. placeholder: The text shown before anything is chosen. count_text: The plural word shown beside the number of records chosen. given_request: A request set on the field, for a field whose form carries none, such as a filter’s.

Initialization

view_class#

None

given_request#

None

kind = <Multiline-String>#
scopes#

None

default_scope#

‘view’

multiple#

False

label = <Multiline-String>#
placeholder = <Multiline-String>#
count_text = <Multiline-String>#
build_config()#

Return the look and the address shared by the fields of every kind.

Returns: The django-tomselect configuration of the field’s widget.

type_filter()#

Return the filter that keeps the field’s type, as the widget draws it.

Returns: A dict for the widget’s filters, or None when the field keeps every type.

get_bound_field(form, field_name)#

Remember the form, which holds the record being edited and the request.

property request#

The request the field is used in: the form’s, else the one it was given, else the current one.

property user#

The person using the field, or none when it is used outside a request.

view()#

Return the record view that serves the field’s scope.

narrowing()#

Return the lookup that the other field’s submitted value puts on the records.

Read from the bound form’s data and never from the page’s request.

Returns: A dict for the view’s offered(), or None when the field depends on nothing or the other field has no value.

offered(narrowed=False)#

Return the records the field offers the person using it.

Args: narrowed: Whether to keep only the records that match the field it depends on. A value that cannot name a record offers nothing.

Returns: A queryset of the offered records.

stored_pks()#

Return the primary keys the record being edited already holds in this field.

Returns: A list, empty when the form edits no saved record.

allowed()#

Return the records the field accepts: those it offers and those already stored.

A field used outside any request, in a script or a test, serves no person. It accepts what the form assigned to its queryset and nothing by default.

selected_options(value)#

Describe the chosen records for the widget.

A record the person may not see is labelled as private and never named. A value that is neither offered nor stored is not shown at all.

Args: value: The chosen primary key, or the list of them.

Returns: A list of {"value", "label"} dicts.

class RecordChoiceFieldMixin(*args, scope=None, owner=None, type=None, depends_on=None, prefer=None, **kwargs)#

Bases: fairdm.contrib.autocomplete.fields.RecordFieldMixin

Choose one record.

Initialization

clean(value)#

Accept the record when the field offers it, or already holds it.

class RecordMultipleChoiceFieldMixin(*args, scope=None, owner=None, type=None, depends_on=None, prefer=None, **kwargs)#

Bases: fairdm.contrib.autocomplete.fields.RecordFieldMixin

Choose several records.

Initialization

multiple#

True

clean(value)#

Accept the records when the field offers each, or already holds it.

record_fields(view_class, kind, *, label, one_label, any_label, count_text, scopes=SCOPES)#

Make the single and the multiple field that choose one kind of record.

Args: view_class: The record view that decides what is offered. kind: The kind of record, as it appears in the view’s address. label: The label of both fields. one_label: The placeholder of the single field. any_label: The placeholder of the multiple field. count_text: The plural word shown beside the number the multiple field has chosen. scopes: The scopes the kind offers. Both fields default to view where the kind offers it.

Returns: The single field class and the multiple field class.

class PublicationChoiceField(**kwargs)#

Bases: django_tomselect.forms.TomSelectModelChoiceField

Choose one publication by searching its title, authors or DOI.

Publications carry no visibility, so the field accepts any publication that exists. It never asks django-tomselect to validate, which rebuilds the view from the current page’s search parameters.

Args: **kwargs: Passed to the form field. label defaults to “Publication”.

Initialization

Initialize a TomSelect model field with optional configuration.

clean(value)#

Accept the publication when it exists.

class TagsWidget(config: django_tomselect.app_settings.TomSelectConfig | dict[str, Any] | None = None, **kwargs: Any)#

Bases: django_tomselect.widgets.TomSelectModelMultipleWidget

Choose tags, which are words and not records, so the chosen ones need no lookup.

Initialization

Initialize widget with model-specific attributes.

selected_tags(value)#

Return the chosen tags as the options the control starts with.

Args: value: The tags chosen, as a list or a single tag.

Returns: A list of {"value", "label"} dicts.

get_context(name, value, attrs=None)#

Show the chosen tags even where the page has no request to look them up in.

class TagsField(*, scope='view', allow_new=False, **kwargs)#

Bases: django.forms.Field

Choose tags, drawn as chips with a remove button, cleaned to a list of stripped words.

With allow_new off the field accepts only tags in use on the records its scope offers the person. With it on, any tag that is not empty.

Args: scope: How much the person may be offered: "public" or "view". Defaults to "view". allow_new: Whether a person may type a tag nobody has used. **kwargs: Passed to the form field.

Attributes: given_request: A request set on the field, for a field whose form carries none, such as a filter’s.

Initialization

default_error_messages#

None

given_request#

None

property user#

The person using the field, or an anonymous user when it is used outside a request.

to_python(value)#

Strip each tag, and drop the empty ones and the repeats.

Args: value: The submitted tags, as a list or a single tag.

Returns: A list of tags in the order given.

validate(value)#

Refuse a tag nobody has used, unless the field lets a new one be added.

has_changed(initial, data)#

Say whether the chosen tags differ from the initial ones, whatever their order.

class RorChoiceField(**kwargs)#

Bases: django_tomselect.forms.TomSelectChoiceField

Choose an organization from the Research Organization Registry by searching its name.

The value is the ROR ID. The field accepts any value in the form of a ROR ID or its address and never asks the registry, so a form saves while ROR does not answer.

Args: **kwargs: Passed to the form field. label defaults to “ROR ID”.

Initialization

Initialize a TomSelect field with optional configuration.

default_error_messages#

None

clean(value)#

Accept a ROR ID or its address, or nothing when the field is not required.