apis_core.utils package

Submodules

apis_core.utils.DateParser module

apis_core.utils.DateParser.parse_date(date_string: str) -> (<class 'datetime.datetime'>, <class 'datetime.datetime'>, <class 'datetime.datetime'>)[source]

function to parse a string date field of an entity

:param date_stringstr :

the field value passed by a user

:return date_singledatetime :

single date which represents either the precise date given by user or median in between a range.

:return date_abdatetime :

starting date of a range if user passed a range value either implicit or explicit.

:return date_bisdatetime :

ending date of a range if user passed a range value either implicit or explicit.

apis_core.utils.authentication module

class apis_core.utils.authentication.TokenAuthSupportQueryString[source]

Bases: TokenAuthentication

Extend the TokenAuthentication class to support querystring authentication in the form of “http://www.example.com/?auth_token=<api_key>”

authenticate(request)[source]

Authenticate the request and return a two-tuple of (user, token).

apis_core.utils.autocomplete module

class apis_core.utils.autocomplete.ExternalAutocomplete[source]

Bases: object

This is a helper base class for implementing external autocomplete classes. <Modelname>ExernalAutocomplete classes are expected to have a get_results(self, q) method that returns a list of results usable by the autocomplete view. This base class implements this get_results method in a way that you can inherit from it and just define a list of adapters. Those adapters are then used one by one to add external autocomplete search results.

adapters = []
get_results(q)[source]
session = <requests.sessions.Session object>
class apis_core.utils.autocomplete.ExternalAutocompleteAdapter(*args, **kwargs)[source]

Bases: object

Base class for ExternalAutocompleteAdapters. It provides the methods used for templating the autocomplete results. You can pass a template name to initialization, which is then used to style the results.

default_template(result)[source]
get_result_label(result)[source]
template = None
class apis_core.utils.autocomplete.LobidAutocompleteAdapter(*args, **kwargs)[source]

Bases: ExternalAutocompleteAdapter

This autocomplete adapters queries the lobid autocomplete apis. See https://lobid.org/gnd/api for details You can pass a lobid_params dict which will then be use as GET request parameters.

extract(res)[source]
get_results(q, session=<requests.sessions.Session object>)[source]
params = {}
class apis_core.utils.autocomplete.TypeSenseAutocompleteAdapter(*args, **kwargs)[source]

Bases: ExternalAutocompleteAdapter

This autocomplete adapters queries typesense collections on a typesense server. The collections variable can either be a string or a list - if its a string, that collection is queried directly, if its a list, the adapter uses typesense multi_search endpoint.

collections = None
default_template(result)[source]
extract(res)[source]
get_results(q, session=<requests.sessions.Session object>)[source]
server = None
token = None

apis_core.utils.fields module

class apis_core.utils.fields.NewlineSeparatedListField(*args, db_collation=None, **kwargs)[source]

Bases: TextField

This field is basically a textfield with a custom widget. It uses the NewlineSeparatedListWidget to provide a simply way for users to enter multiple values without having to think about the separator.

formfield(form_class=None, choices_form_class=None, **kwargs)[source]

Return a django.forms.Field instance for this field.

apis_core.utils.filters module

class apis_core.utils.filters.CustomSearchFilter[source]

Bases: SearchFilter

filter_queryset(request, queryset, view)[source]

Return a filtered queryset.

to_html(request, queryset, view)[source]

apis_core.utils.helpers module

apis_core.utils.helpers.construct_lookup(value: str) tuple[str, str][source]

Helper method to parse input values and construct field lookups (https://docs.djangoproject.com/en/4.2/ref/models/querysets/#field-lookups) Parses user input for wildcards and returns a tuple containing the interpreted django lookup string and the trimmed value E.g.

  • example -> ('__icontains', 'example')

  • *example* -> ('__icontains', 'example')

  • *example -> ('__iendswith', 'example')

  • example*-> ('__istartswith', 'example')

  • "example" -> ('__iexact', 'example')

Parameters:

value (str) – text to be parsed for *

Returns:

a tuple containing the lookup type and the value without modifiers

apis_core.utils.helpers.create_object_from_uri(uri: str, model: object, raise_on_fail=False) object[source]
apis_core.utils.helpers.datadump_get_objects(models: list = [], *args, **kwargs)[source]
apis_core.utils.helpers.datadump_get_queryset(additional_app_labels: list = [])[source]

This method is loosely based on the dumpdata admin command. It iterates throug the relevant app models and exports them using a serializer and natural foreign keys. Data exported this way can be reimported into a newly created Django APIS app

apis_core.utils.helpers.datadump_serializer(additional_app_labels: list = [], serialier_format='json')[source]
apis_core.utils.helpers.get_html_diff(a, b, show_a=True, show_b=True, shorten=0)[source]

Create an colorized html represenation of the difference of two values a and b If show_a is True, colorize deletions in a If show_b is True, colorize insertions in b The value of shorten defines if long parts of strings that contains no change should be shortened

apis_core.utils.helpers.get_importer_for_model(model: object)[source]

apis_core.utils.normalize module

apis_core.utils.normalize.clean_uri(uri: str) str[source]

apis_core.utils.rdf module

apis_core.utils.rdf.definition_matches_model(definition: str, model: object) bool[source]
apis_core.utils.rdf.definition_matches_uri(definition: str, uri: str) bool[source]
apis_core.utils.rdf.get_definition_and_attributes_from_uri(uri: str, model: object) Tuple[dict, dict][source]

This function looks for .toml files in the rdfimport app directories and loads all the files it can parse. For every file that contains a superclass key it checks if it is a superclass of model. It uses the first file that matches to extract attributes from the RDF endpoint and then returns both the parsed file contents and the extracted attributes. The reason we are also returning the parsed file contents is, that you then can define a model in the file and then use this function to iterate over a list of URIs and you can use the matched definition to choose which model to create. The dict containing the parsed file contents also contains the filename, to make debugging a bit easier.

apis_core.utils.settings module

apis_core.utils.settings.dict_from_toml_directory(directory: str) dict[source]
apis_core.utils.settings.get_entity_settings_by_modelname(entity: str = None) dict[source]

return the settings for a specific entity or the dict for all entities if no entity is given

apis_core.utils.settings.internal_uris() list[str][source]

apis_core.utils.test_DateParser module

class apis_core.utils.test_DateParser.DateParserTest(methodName='runTest')[source]

Bases: TestCase

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

test_dates()[source]
apis_core.utils.test_DateParser.fi(datestring)[source]

apis_core.utils.test_helpers module

class apis_core.utils.test_helpers.FilterMethodstest(methodName='runTest')[source]

Bases: TestCase

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

test_lookup()[source]

apis_core.utils.test_normalize module

class apis_core.utils.test_normalize.NormalizeTest(methodName='runTest')[source]

Bases: TestCase

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

test_clean_uri_dnb()[source]
test_clean_uri_geonames()[source]
test_clean_uri_wikidata()[source]

apis_core.utils.test_rdf module

class apis_core.utils.test_rdf.Institution(id, label)[source]

Bases: E74_Group

exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

label

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

objects = <django.db.models.manager.Manager object>
class apis_core.utils.test_rdf.Person(id, forename, surname, gender, date_of_birth, date_of_death)[source]

Bases: E21_Person

exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

date_of_birth

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

date_of_death

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

forename

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

gender

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

objects = <django.db.models.manager.Manager object>
surname

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

class apis_core.utils.test_rdf.Place(id, label, latitude, longitude)[source]

Bases: E53_Place

exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

label

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

latitude

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

longitude

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

objects = <django.db.models.manager.Manager object>
class apis_core.utils.test_rdf.RdfTest(methodName='runTest')[source]

Bases: TestCase

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

test_get_definition_from_dict_institution_from_dnb()[source]
test_get_definition_from_dict_institution_from_dnb2()[source]
test_get_definition_from_dict_person_from_dnb()[source]
test_get_definition_from_dict_place_from_dnb()[source]
test_get_definition_from_dict_place_from_geonames()[source]