adhocracy_core.utils package

Helper functions shared between modules.

find_graph(context) → object[source]

Get the Graph object in the lineage of context or None.

Return type:adhocracy_core.graph.Graph
get_iresource(context) → <InterfaceClass zope.interface.interfaces.IInterface>[source]

Get the adhocracy_core.interfaces.IResource of context.

Returns:IInterface or None to ease testing
get_matching_isheet(context, isheet: <InterfaceClass zope.interface.interfaces.IInterface>) → <InterfaceClass zope.interface.interfaces.IInterface>[source]

Get isheet or a subclass of it if context provides it.

If context provides neither isheet nor any of its subclasses, None is returned.

log_compatible_datetime(dt: datetime.datetime=datetime.datetime(2017, 6, 14, 12, 36, 5, 357239))[source]

Format a datetime in the same way as the logging framework.

Mimics the output of the ‘%(asctime)’ placeholder.

to_dotted_name(context) → str[source]

Get the dotted name of context.

Returns:The dotted name of context, if it’s a type. If context is a string it is returned as is (since we suppose that it already represents a type name).
remove_keys_from_dict(dictionary: dict, keys_to_remove=()) → dict[source]

Remove keys from dictionary.

Return type:dict
Parameters:keys_to_remove – Tuple with keys or one key
exception_to_str(err: Exception)[source]

Convert an exception to a string.

Parameters:err (Exception) – the exception
Returns:“{type}: {str}”, where {type} is the class name of the exception and {str} is the result of calling str(err); or just “{type}” if {str} is empty
normalize_to_tuple(context) → tuple[source]

Convert context to tuple.

nested_dict_set(d: dict, keys: list, value: object)[source]

Set a nested key in a dictionary.

The following two expressions are equivalent, if d['key']['subkey'] already exists:

nested_dict_set(d, ['key', 'subkey', 'subsubkey'], value)
d['key']['subkey']['subsubkey'] = value

If parent elements such as d['key']['subkey'] or d['key'] don’t yet exist, this function will initialize them as dictionaries.

unflatten_multipart_request(request: pyramid.request.Request) → dict[source]

Convert a multipart/form-data request into the usual dict structure.

get_changelog_metadata(resource, registry) → adhocracy_core.interfaces.ChangelogMetadata[source]

Return transaction changelog for resource.

set_batchmode(request: pyramid.request.Request, value=True)[source]

Set ‘batchmode’ marker for the current request.

This is called by adhocracy_core.rest.batchview.BatchView. Other code can check is_batchmode() to modify behavior.

is_batchmode(request: pyramid.request.Request) → bool[source]

Get ‘batchmode’ marker for the current request.

is_hidden(resource: <InterfaceClass adhocracy_core.interfaces.IResource>) → dict[source]

Check whether a resource is hidden.

This also returns True for descendants of hidden resources, as a positive hidden status is inherited.

get_reason_if_blocked(resource: <InterfaceClass adhocracy_core.interfaces.IResource>) → str[source]

Check if a resource is blocked and return Reason, None otherwise.

list_resource_with_descendants(resource: <InterfaceClass adhocracy_core.interfaces.IResource>) → collections.abc.Iterable[source]

List all descendants of a resource, including the resource itself.

extract_events_from_changelog_metadata(meta: adhocracy_core.interfaces.ChangelogMetadata) → list[source]

Extract the relevant events affecting a resource.

Return type:list
Parameters:meta (ChangelogMetadata) – an entry in the transaction changelog
Returns:a list of 0 to 2 events
now() → datetime.datetime[source]

Return current date time with ‘UTC’ time zone.

get_modification_date(registry: pyramid.registry.Registry) → datetime.datetime[source]

Get the shared modification date for the current transaction.

This way every date created in one batch/post request can use this as default value. The frontend relies on this to ease sorting.

create_filename(directory='.', prefix='', suffix='.csv') → str[source]

Use current time to generate a unique filename.

Return type:str
Params dir:directory path for the filename. If non existing the directory is created.
Params prefix:prefix for the generated filename
Params suffix:type suffix for the generated filename, like ‘csv’
load_json(filename)[source]

Load a json file from the disk.

has_annotation_sheet_data(resource: <InterfaceClass adhocracy_core.interfaces.IResource>) → bool[source]

Check if resource has no data stored in AnnotationResourceSheets.

is_created_in_current_transaction(resource: <InterfaceClass adhocracy_core.interfaces.IResource>, registry: pyramid.registry.Registry) → bool[source]

Check if resource is created during the current transaction.

create_schema(schema_class, context, request, **kwargs) → colander.Schema[source]

Create schema from schema_class and add bindings.

The default bindings are: context, request , registry, creating (defaults to False). These can be overridden or extended by **kwargs.