redirectory.libs_int.database.database_rule_actions module

redirectory.libs_int.database.database_rule_actions.add_redirect_rule(db_session, domain: str, domain_is_regex: bool, path: str, path_is_regex: bool, destination: str, destination_is_rewrite: bool, weight: int, commit: bool = True) → Union[redirectory.models.redirect_rule.RedirectRule, int][source]

Creates a new Redirect Rule from all of the given arguments. If a domain, path or destination is already used it is just going to be re-used in the new rule. Before all that it validates rules which are rewrites to see if they are configured correctly.

Depending on where the check failed different integers will be returned.

Parameters:
  • db_session – the database session to use for the DB actions
  • domain – the domain of the new rule
  • domain_is_regex – is the domain a regex or not
  • path – the path of the new rule
  • path_is_regex – is the path a regex or not
  • destination – the destination of the new rule
  • destination_is_rewrite – is the destination a rewrite or not
  • weight – the weight of the new rule
  • commit – should the function commit the new rule or just flush for ids
Returns:

Redirect Rule - if all went well 1 (int) - if the check failed for rewrite rule 2 (int) - if the check for already existing rule failed

redirectory.libs_int.database.database_rule_actions.delete_redirect_rule(db_session, redirect_rule_id: int) → bool[source]

Tries to delete a redirect rule with a given id If the rule doesn’t exist then false will be returned

Parameters:
  • db_session – the database session to use for db actions
  • redirect_rule_id – the id of the rule to delete
Returns:

true if rule deleted successfully else false if rule not found

redirectory.libs_int.database.database_rule_actions.get_model_by_id(db_session, model, model_id)[source]

Queries a specific model / table in a given database session for a row with a given ID

Parameters:
  • db_session – the database session to use for db actions
  • model – the model / table to query
  • model_id – the id of the given model
Returns:

an instance of the model or None if not found

redirectory.libs_int.database.database_rule_actions.get_usage_count(db_session, model, model_instance_id) → int[source]

Creates a query that counts the usage of a given model with model_instance_id in the RedirectRule model / table. After that executes the query and returns the result

Parameters:
  • db_session – the database session to use for db actions
  • model – the model to count the usages for
  • model_instance_id – the id of the model instance
Returns:

an integer representing how many times a certain model with that id is used

redirectory.libs_int.database.database_rule_actions.update_redirect_rule(db_session, redirect_rule_id: int, domain: str, domain_is_regex: bool, path: str, path_is_regex: bool, destination: str, destination_is_rewrite: bool, weight: int) → Union[redirectory.models.redirect_rule.RedirectRule, int][source]

Updates the rule with the given ID and with the given arguments. Finds the rule specified with the redirect_rule_id and updates it’s values correspondingly. If everything goes correctly then the new version of the rule returned. If no rule with that ID is found an integer is returned If the new rule fails the rewrite validation an integer is returned

Parameters:
  • db_session – the database session to use for db actions
  • redirect_rule_id – the ID of the rule to update
  • domain – the new domain of the rule
  • domain_is_regex – the new status of the domain rule
  • path – the new path of the rule
  • path_is_regex – the new status of the path rule
  • destination – the new destination of the rule
  • destination_is_rewrite – the new status of the destination rule
  • weight – the new weight of the rule
Returns:

Redirect Rule - which is the updated version if all went well 1 (int) - rule exists but fails validation check for rewrite rule 2 (int) - rule with this id does not exist

redirectory.libs_int.database.database_rule_actions.validate_rewrite_rule(path: str, path_is_regex: bool, destination: str) → bool[source]

Checks if all of the needed variables/placeholders in the destination rule are also appearing in the path when compiled to a regex pattern.

Parameters:
  • path – the path rule to check for
  • path_is_regex – if the path rule is a regex (hint in order to pass this check it always has to be)
  • destination – the destination rule with placeholders in it
Returns:

True if the rule is valid else False