Utility functions¶
- metapensiero.sqlalchemy.proxy.utils.SQLALCHEMY_VERSION = (2, 0, 29)¶
SQLAlchemy version.
- metapensiero.sqlalchemy.proxy.utils.col_by_name(query, colname)¶
Helper: find the (first) column with the given name.
- metapensiero.sqlalchemy.proxy.utils.create_change_saver(adaptor=None, save_changes=None, modified_slot_name='modified_records', deleted_slot_name='deleted_records', inserted_ids_slot='inserted_ids', modified_ids_slot='modified_ids', deleted_ids_slot='deleted_ids', result_slot='root', success_slot='success', message_slot='message')¶
Function factory to implement the standard POST handler for a proxy.
- Parameters:
adaptor – a function that adapts the changes before application
save_changes – the function that concretely applies the changes
modified_slot_name – a string, by default ‘modified_records’
deleted_slot_name – a string, by default ‘deleted_records’
inserted_ids_slot – a string, by default ‘inserted_ids’
modified_ids_slot – a string, by default ‘modified_ids’
deleted_ids_slot – a string, by default ‘deleted_ids’
result_slot – a string, by default ‘root’
success_slot – a string, by default ‘success’
message_slot – a string, by default ‘message’
- Returns:
a dictionary, with a boolean success slot with a
True
value if the operation was completed without errors,False
otherwise: in the latter case the message slot contains the reason for the failure. Three other slots carry lists of dictionaries with the ids of the inserted, modified and deleted records.
This implements the generic behaviour we need to save changes back to the database.
The adaptor function takes four arguments, respectively the SA session, the request, a list of added/modified records and a list of deleted records; it must return two (possibly modified) lists, one containing added/modified records and the other with the records to delete, e.g.:
def adaptor(session, request, modified_recs, deleted_recs): # do any step to adapt incoming data return modified_recs, deleted_recs
- metapensiero.sqlalchemy.proxy.utils.csv_to_list(csv)¶
Build a list of strings from a CSV or JSON array.
- Parameters:
csv – a string containing either a
CSV
or a JSON array- Return type:
a Python list
This is very simplicistic: since its used to transfer a list of field names, that is plain ASCII strings, JSON escapes are not even considered.
csv may be either a plain CSV string such as
first,second,third
or a JSON array, such as["first","second","third"]
.
- metapensiero.sqlalchemy.proxy.utils.get_column_type(column)¶
Return the concrete type of a column.
- metapensiero.sqlalchemy.proxy.utils.get_sqlalchemy_version()¶
Return the SQLAlchemy version as a tuple of integers.