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.IResourceof context.Returns: IInterfaceor 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, 44, 51, 844659))[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: dictParameters: 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 exceptionReturns: “{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
-
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']ord['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 checkis_batchmode()to modify behavior.
-
is_batchmode(request: pyramid.request.Request) → bool[source]¶ Get ‘batchmode’ marker for the current request.
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: listParameters: meta ( ChangelogMetadata) – an entry in the transaction changelogReturns: a list of 0 to 2 events
-
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: strParams 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’
-
has_annotation_sheet_data(resource: <InterfaceClass adhocracy_core.interfaces.IResource>) → bool[source]¶ Check if resource has no data stored in AnnotationResourceSheets.