From 775389c54a46a6d4a4f8d6f377f551be40331ac5 Mon Sep 17 00:00:00 2001 From: MohmdFo Date: Thu, 26 Sep 2024 20:46:18 +0330 Subject: [PATCH] feat(BaseDataGenerator): add get_random_timedelta method to the generator --- sage_tools/repository/generator/base.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/sage_tools/repository/generator/base.py b/sage_tools/repository/generator/base.py index 53783e7..a58dfc3 100644 --- a/sage_tools/repository/generator/base.py +++ b/sage_tools/repository/generator/base.py @@ -1,7 +1,7 @@ import io import random import secrets -from datetime import datetime, timezone +from datetime import datetime, timezone, timedelta from typing import Any, List, Set, Type, TypeVar from django.db.models import Model @@ -417,3 +417,17 @@ def add_to_m2m( except IndexError: raise IndexError("`objs` is empty. Please ensure population is not None.") attr.add(*items_to_add) + + def get_random_timedelta(self, min_minutes=0, max_minutes=1): + """ + Scenario: + Create a random duration within a specified range of minutes for varied + applications. + + Algorithm: + Converts a given minute range to seconds and generates a random duration + within this range, returning it as a timedelta object. + """ + max_seconds = max_minutes * 60 + min_seconds = min_minutes * 60 + return timedelta(seconds=random.randint(min_seconds, max_seconds))