helpers#
get_technical_metax_user#
parse_date_or_datetime#
parse_iso_dates_in_nested_dict#
Recursive function to parse ISO dates to datetime objects in nested dictionaries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d |
Dict
|
Dictionary to parse |
required |
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
Parsed dictionary. |
Note: The dictionary values are updated in-place.
Source code in src/apps/common/helpers.py
process_nested#
Generic nested dict and list processing.
For each nested value: - Pass value through pre_handler callback - In case of dict or list, copy it and process contained values - Pass value through post_handler callback
Source code in src/apps/common/helpers.py
get_attr_or_item#
Return value for attribute. If not found, get item with key. Return None if not found.
Source code in src/apps/common/helpers.py
cachalot_toggle#
Context manager that allows disabling cachalot.
Useful for heavy one-off queries that may be too large for memcached.
Usage
with cachalot_toggle(enabled=False): do_stuff() # run code without caching
Source code in src/apps/common/helpers.py
get_filter_openapi_parameters#
Extract OpenAPI (swagger) parameters for filter query parameters.
Useful in special cases where automatic swagger generation is unable to produce query parameters for a view. Most of the time (i.e. for 'list' method of a ViewSet that has 'filterset_class') the parameters are produced automatically by DjangoFilterBackend.
Usage example:
@swagger_auto_schema(
manual_parameters=get_filter_openapi_parameters(SomeFilterSet),
)
def view_function_to_decorate():
...
Source code in src/apps/common/helpers.py
prepare_for_copy#
ensure_instance_id#
date_to_datetime#
datetime_to_date#
changed_fields#
is_valid_uuid#
is_valid_float_str#
is_valid_url#
is_valid_email#
quote_url#
Percent-encode url. Assumes any '%' characters in the url are already correct.
Note: Empty URL components (e.g. '#' in 'https://example.com#') are removed from the resulting URL due to how urllib works. See https://github.com/python/cpython/issues/67041
Source code in src/apps/common/helpers.py
deduplicate_list#
has_values#
format_multiline#
Multiline string cleanup and formatting helper.
Does the following:
- Remove leading and trailing newlines
- Remove all whitespace after "\" (so it can be used for line continuation)
- Remove common indentation
- Format string with .format
using the provided"" arguments.
Source code in src/apps/common/helpers.py
single_translation#
Return single translation value for multilanguage dict.
Source code in src/apps/common/helpers.py
omit_none#
is_empty_string#
omit_empty#
Return copy of dict with None values and empty lists, empty strings and empty dicts removed.
Source code in src/apps/common/helpers.py
ensure_list#
Convert None into empty list, raise validation error on other non-list values.
Source code in src/apps/common/helpers.py
ensure_dict#
remove_wkt_point_duplicates#
Remove points from wkt_list
that are equal or very similar to point
.
Source code in src/apps/common/helpers.py
is_field_value_provided#
Determine if Model.init arguments have a value for field_name.
Useful when we require a default value that is different from the field default.
Source code in src/apps/common/helpers.py
batched#
Implementation of itertools.batched upcoming in Python 12.
See https://docs.python.org/3/library/itertools.html#itertools.batched
Source code in src/apps/common/helpers.py
datetime_to_header#
merge_sets#
Merge all sets that contain at least one common element.