fairdm.contrib.autocomplete.views#

Autocomplete views for FairDM records, people, organizations, publications, tags and ROR.

Module Contents#

Classes#

LimitedAutocompleteMixin

Keep what one request can cost, and what it can reveal, in the view’s hands.

RecordAutocompleteView

Offer the records of one kind that the person searching may reach at one scope.

DatasetAutocompleteView

Offer datasets, starting from every dataset so that private ones can be reached.

ProjectAutocompleteView

Offer projects, public by their own visibility.

DatasetRecordsAutocompleteView

Offer samples or measurements, the records that sit inside a dataset.

SampleAutocompleteView

Offer samples, found by name, local ID or identifier.

MeasurementAutocompleteView

Offer measurements, found by name or local ID.

ContributorAutocompleteView

Offer the people or the organizations of the portal, those nearest the person searching first.

PersonAutocompleteView

Offer people, found by name, alternative name or ORCID, collaborators first.

OrganizationAutocompleteView

Offer organizations, found by name, alternative name or ROR, the researcher’s own first.

PublicationAutocompleteView

Offer publications, found by title, author or DOI, a DOI typed exactly first.

TagAutocompleteView

Offer the tags in use on the records the person searching may reach, found by name.

RorAutocompleteView

Offer the organizations of the Research Organization Registry that match the search.

Functions#

scoped_view

Return the subclass of a record view that serves one scope.

Data#

API#

logger#

‘getLogger(…)’

SCOPES#

(‘public’, ‘view’, ‘edit’)

PROFILE#

‘profile’

TAG_SCOPES#

(‘public’, ‘view’)

scoped_view(view_class, scope)#

Return the subclass of a record view that serves one scope.

django-tomselect rebuilds a view as view_class() when it validates a value, so the scope has to be an attribute of the class and cannot be an argument of as_view().

Args: view_class: The record view to subclass. scope: One of "public", "view" or "edit".

Returns: A subclass of view_class with scope set. The same class for the same arguments.

class LimitedAutocompleteMixin#

Keep what one request can cost, and what it can reveal, in the view’s hands.

Attributes: page_size: The results in a page. A request cannot change it. max_pages: The last page that is served. max_query_length: The longest search text. Longer text is cut. throttle_classes: The limits on one caller, checked before anything is searched. allowed_filter_fields: The fields a request may filter on through f and e. A request naming any other field is offered nothing. allowed_ordering_fields: The fields a request may order by. Others are ignored.

skip_authorization#

True

allow_anonymous#

True

page_size#

20

max_pages#

25

max_query_length#

100

allowed_filter_fields: list[str]#

[]

allowed_ordering_fields: list[str]#

[]

throttle_classes#

()

dispatch(request, *args, **kwargs)#

Refuse a caller over a limit, before anything is searched.

setup(request, *args, **kwargs)#

Fix the page size and the length of the search text, whatever the request asks.

paginate_queryset(queryset)#

Serve nothing past the last page of results or past the page limit.

get(request, *args, **kwargs)#

Answer a search that failed with the failure and nothing else.

class RecordAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.LimitedAutocompleteMixin, django_tomselect.autocompletes.AutocompleteModelView

Offer the records of one kind that the person searching may reach at one scope.

Attributes: scope: "public" offers public records to everyone. "view" adds the records the person may view. "edit" offers only the records the person may edit. "profile" offers the records one person, the owner, is credited on: their private ones only to them. Fixed by the address and never read from the request. noun_plural: The kind’s name in the plural, for the headings of the groups. exact_lookups: Lookups on which an exact match is offered first. prefer_param: The request parameter that names a relation and a value, as dataset=5. Records that match come first, in a group of their own. It orders and groups, and never adds or removes a record. preferred_heading: The heading of the preferred group, with %(things)s standing for the kind’s name in the plural.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

scope#

‘view’

exact_lookups: list[str]#

[]

noun_plural = <Multiline-String>#
prefer_param#

‘prefer’

preferred_heading = <Multiline-String>#
classmethod for_scope(scope)#

Return the subclass of this view that serves one scope.

Args: scope: One of "public", "view" or "edit".

Returns: The view class for that scope.

base_queryset()#

Return every record of the kind, before any visibility rule.

Returns: A queryset over the kind’s records.

public_q()#

Return the condition under which a record is public.

Returns: A Q object over the kind’s records.

offered(user, *, owner=None, type=None, depends_on=None)#

Return the records this scope offers a person, before any search or page.

Args: user: The person searching, or an anonymous user. owner: The person whose records the profile scope offers, as a person or a primary key. Other scopes ignore it. type: A registered sample or measurement type to keep, or None for every type. depends_on: Lookups the records must match, such as {"dataset": 5}, when the field chooses within another field’s value. Each lookup must be one the view allows a request to filter on.

Returns: A queryset of the records the scope offers that person.

credited_to(user, owner)#

Return the records one person is credited on that a person may be offered.

Args: user: The person searching, or an anonymous user. owner: The person whose records are wanted, or their primary key.

Returns: The owner’s records, private ones included only when user is the owner. Nothing when there is no such person.

requested_owner()#

Return the primary key the request names as the owner, or None.

preference()#

Return the condition the request asks to see first.

Only a relation the view allows a request to filter on is accepted, and only a value that can name a record of it. Anything else is ignored.

Returns: A Q object, or None when the request asks for nothing the view can use.

get_queryset()#

Return the offered records, searched, ranked into groups and ordered.

exact_q(query)#

Return the condition under which a record matches a search exactly.

Args: query: The text searched for.

Returns: A Q object, empty when the kind has no exact lookups.

search(queryset, query)#

Match the text on the search lookups, and on the exact lookups as well.

get_value_fields()#

Return the fields the results are built from.

group_names()#

Return the heading of each group, by rank.

hook_prepare_results(results)#

Add each result’s group heading and its row, drawn on the server.

describe(result)#

Turn the raw values of one result into what its row shows.

Args: result: The result, changed in place. It gains private and may gain detail, the line shown beneath the name.

class DatasetAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.RecordAutocompleteView

Offer datasets, starting from every dataset so that private ones can be reached.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

model#

None

noun_plural#

‘_(…)’

search_lookups#

[‘name__icontains’]

exact_lookups#

[‘uuid__iexact’]

value_fields#

[‘visibility’, ‘project__name’, ‘project__visibility’]

base_queryset()#

Return every dataset, private ones included.

public_q()#

Return the condition for a dataset that is public along with its project.

describe(result)#

Show the dataset’s project, and mark it private when it or its project is.

class ProjectAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.RecordAutocompleteView

Offer projects, public by their own visibility.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

model#

None

noun_plural#

‘_(…)’

search_lookups#

[‘name__icontains’]

exact_lookups#

[‘uuid__iexact’]

value_fields#

[‘visibility’, ‘status’]

describe(result)#

Show the project’s status.

DATASET_RECORD_FIELDS#

[‘local_id’, ‘dataset__name’, ‘polymorphic_ctype’]

class DatasetRecordsAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.RecordAutocompleteView

Offer samples or measurements, the records that sit inside a dataset.

A record of these kinds is public when its dataset is public and published.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

allowed_filter_fields#

[‘dataset’, ‘polymorphic_ctype’]

preferred_heading#

‘_(…)’

public_q()#

Return the condition for a record whose dataset is public and published.

get_value_fields()#

Add the visibility of the record’s own dataset, which decides its badge.

describe(result)#

Show the record’s type, local ID and dataset, and mark it private unless released.

class SampleAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.DatasetRecordsAutocompleteView

Offer samples, found by name, local ID or identifier.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

model#

None

noun_plural#

‘_(…)’

search_lookups#

[‘name__icontains’, ‘local_id__icontains’]

exact_lookups#

[‘local_id__iexact’, ‘uuid__iexact’]

value_fields#

None

exact_q(query)#

Match the local ID, the UUID and any identifier typed exactly.

search(queryset, query)#

Match the text on name and local ID, on an identifier that contains it, and exactly.

class MeasurementAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.DatasetRecordsAutocompleteView

Offer measurements, found by name or local ID.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

model#

None

noun_plural#

‘_(…)’

search_lookups#

[‘name__icontains’, ‘local_id__icontains’]

exact_lookups#

[‘local_id__iexact’, ‘uuid__iexact’]

value_fields#

None

class ContributorAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.LimitedAutocompleteMixin, django_tomselect.autocompletes.AutocompleteModelView

Offer the people or the organizations of the portal, those nearest the person searching first.

A person or an organization is public information, so one address serves everyone and the scope plays no part.

Attributes: identifier_type: The identifier on which an exact match is offered first. near_heading: The heading of the group listed first. far_heading: The heading of the group listed after it.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

identifier_type = <Multiline-String>#
near_heading = <Multiline-String>#
far_heading = <Multiline-String>#
classmethod for_scope(scope)#

Return this view, which serves every scope alike.

Args: scope: Ignored.

Returns: The view class.

base_queryset()#

Return every contributor of the kind, before any rule on who is offered.

Returns: A queryset over the kind’s contributors.

offered(user, *, depends_on=None)#

Return the contributors offered to a person, before any search or page.

Args: user: The person searching, or an anonymous user. depends_on: Not used, since these views allow no filter.

Returns: A queryset of the offered contributors.

nearby(user)#

Return the primary keys of the contributors listed first for a person.

Args: user: The signed-in person searching.

Returns: A list of primary keys.

exact_q(query)#

Return the condition under which a contributor’s identifier is the text typed.

Args: query: The text searched for.

Returns: A Q object over the kind’s contributors.

get_queryset()#

Return the offered contributors, searched, grouped by nearness and ordered.

get_value_fields()#

Return the fields the results are built from.

loaded(ids)#

Load the contributors of a page with what their rows show.

Args: ids: The primary keys of the page.

Returns: A dict of contributors by primary key.

describe(result, contributor)#

Work out what one row shows.

Args: result: The result, changed in place. contributor: The contributor the result stands for.

Returns: The extra context the row is drawn with.

hook_prepare_results(results)#

Add each result’s group heading and its row, drawn on the server.

class PersonAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.ContributorAutocompleteView

Offer people, found by name, alternative name or ORCID, collaborators first.

Superusers and the anonymous placeholder account are never offered. An email address is neither searched nor shown.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

model#

None

identifier_type#

None

search_lookups#

[‘name__icontains’, ‘alternative_names__icontains’]

value_fields#

[]

near_heading#

‘_(…)’

far_heading#

‘_(…)’

offered(user, *, depends_on=None)#

Offer every person who is not a superuser or the anonymous placeholder.

nearby(user)#

Return the people credited on a record that the person searching can open.

static openable_credits(user)#

Return the person’s credits on the records the person may view.

A credit on a private record that carries no access to it does not count, so a person met only there is not someone the searcher has worked with.

Args: user: The person searching.

Returns: A list of contributions.

loaded(ids)#

Load the people with their identifiers and affiliations.

describe(result, person)#

Show the person’s primary affiliation and ORCID, and mark one nobody has claimed.

class OrganizationAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.ContributorAutocompleteView

Offer organizations, found by name, alternative name or ROR, the researcher’s own first.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

model#

None

identifier_type#

None

search_lookups#

[‘name__icontains’, ‘alternative_names__icontains’]

value_fields#

[]

near_heading#

‘_(…)’

far_heading#

‘_(…)’

nearby(user)#

Return the organizations the person belongs to: verified and not ended.

describe(result, organization)#

Show where the organization is, and no identifier.

class PublicationAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.LimitedAutocompleteMixin, django_tomselect.autocompletes.AutocompleteModelView

Offer publications, found by title, author or DOI, a DOI typed exactly first.

Literature carries no visibility of its own, so one address serves everyone and the scope plays no part.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

model#

None

search_lookups#

[‘title__icontains’, ‘item_names__name__family__icontains’, ‘item_names__name__literal__icontains’]

value_fields#

[‘title’]

classmethod for_scope(scope)#

Return this view, which serves every scope alike.

Args: scope: Ignored.

Returns: The view class.

static bare_doi(text)#

Take the DOI out of text that is one, or the address of one.

Args: text: A search term.

Returns: The text without a doi: or https://doi.org/ prefix.

get_queryset()#

Return the publications that match the search, those with the DOI typed first.

hook_prepare_results(results)#

Give each result its authors, year and DOI, and its row drawn on the server.

class TagAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.LimitedAutocompleteMixin, django_tomselect.autocompletes.AutocompleteModelView

Offer the tags in use on the records the person searching may reach, found by name.

Attributes: scope: "public" offers the tags on public records, to everyone. "view" adds the tags on the records the person may view. Fixed by the address and never read from the request.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

model#

None

search_lookups#

[‘name__icontains’]

value_fields#

[‘name’]

scope#

‘view’

classmethod for_scope(scope)#

Return the subclass of this view that serves one scope.

Args: scope: One of "public" or "view".

Returns: The view class for that scope.

offered(user, *, depends_on=None)#

Return the tags this scope offers a person, before any search or page.

A tag is offered when it is in use on a project, dataset, sample or measurement that the record view of the same scope offers that person, so no tag reveals a record.

Args: user: The person searching, or an anonymous user. depends_on: Not used, since this view allows no filter.

Returns: A queryset of tags.

get_queryset()#

Return the offered tags, searched and ordered by name.

class RorAutocompleteView(**kwargs)#

Bases: fairdm.contrib.autocomplete.views.LimitedAutocompleteMixin, django_tomselect.autocompletes.AutocompleteIterablesView

Offer the organizations of the Research Organization Registry that match the search.

The registry holds public information, so one address serves everyone and the scope plays no part. A search that ROR does not answer is answered with status 503.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

classmethod for_scope(scope)#

Return this view, which serves every scope alike.

Args: scope: Ignored.

Returns: The view class.

get_iterable()#

Search ROR for the text typed, and draw each match as a row.

Returns: A list of {"value", "label", "detail", "html"} dicts, empty when nothing is typed.

Raises: RegistryUnavailable: ROR could not be asked or answered badly.

get(request, *args, **kwargs)#

Answer with the matches, or with the failure and nothing else.

refusal(status)#

Return an empty answer with a failure status.

Args: status: The HTTP status.

Returns: A JsonResponse that names no cause.