redirectory.libs_int.database.database_actions module

class redirectory.libs_int.database.database_actions.NoneType

Bases: object

redirectory.libs_int.database.database_actions.encode_model(model: sqlalchemy.ext.declarative.api.DeclarativeMeta, parent_class: Any = None, expand: bool = False) → dict[source]

Encodes a DB instance object of a given model into json

Parameters:
  • model – The DB model instance to serialize to json
  • parent_class – A DB model might inherit from another DB model. Pass the parent class in order to be serialized correctly
  • expand – to include relationships or not
Returns:

a dictionary with basic data types that are all serializable

redirectory.libs_int.database.database_actions.encode_query(query: list, expand: bool = False) → list[source]

Loops through all of the objects in a query and encodes every object with the help of encode_model() function. All of the individual encoded models are added into a list and then returned.

Parameters:
  • query – the query that you would like to encode
  • expand – if you should expand relationships in the models
Returns:

a list of dictionaries which are the encoded objects

redirectory.libs_int.database.database_actions.get_or_create(session, model, defaults=None, **kwargs)[source]

Gets an instance of an object or if it does not exist then create it.

Parameters:
  • session – the database session
  • model – the model / table to ger or create from
  • defaults – any default parameters for creating
  • **kwargs – the criteria to get or create
Returns:

a tuple(p,q) p: an instance of the object and q: if it is new or old

redirectory.libs_int.database.database_actions.get_table_row_count(db_session, model_table) → int[source]

Gets the number of rows in a given database in the given database session

Parameters:
  • db_session – the database session to use for db actions
  • model_table – the model / table
Returns:

integer represent the number of row in the table

redirectory.libs_int.database.database_actions.sanitize_like_query(query_str: str) → str[source]

Sanitizes a string to be used as a query in the DB. It will replace a * with % only when the star is not escaped.

Parameters:query_str – original string to sanitize
Returns:sanitized converted string