diff --git a/addons/cetmix_tower_server/models/tools.py b/addons/cetmix_tower_server/models/tools.py new file mode 100644 index 0000000..41b0f80 --- /dev/null +++ b/addons/cetmix_tower_server/models/tools.py @@ -0,0 +1,35 @@ +# Copyright (C) 2022 Cetmix OÜ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from random import choices + +CHARS = "23456789acefhjkmnprtvwxyz" + + +def generate_random_id(sections=1, population=4, separator="-"): + """Generates random id + eg 'ahj2-jer83' + + Args: + sections (int, optional): number of sections. Defaults to 1. + population (int, optional): number of symbols per section. Defaults to 4. + separator (str, optional): section separator. Defaults to "-". + + Returns: + Str: generated id + """ + if sections < 1 or population < 0: + return None + + def get_section(): + return "".join(choices(CHARS, k=population)) + + # Single section + if sections == 1: + return get_section() + + # Multiple sections + result = [] + for _ in range(sections): + result.append(get_section()) + + return separator.join(result)