Skip to content

Commit 6505560

Browse files
committed
feat: Add new utils
1 parent 83350eb commit 6505560

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

utils/extend_model.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,27 @@ def add_project_name_to_time_entries(time_entries, projects):
3131
setattr(time_entry, 'project_name', project.name)
3232

3333

34-
def create_in_condition(data_object, attr_to_filter, first_attr="c.id"):
34+
def create_in_condition(data_object, attr_to_filter="", first_attr="c.id"):
3535
attr_filter = re.sub('[^a-zA-Z_$0-9]', '', attr_to_filter)
36-
object_id = [str(eval(f"object.{attr_filter}")) for object in data_object]
36+
object_id = (
37+
[str(i) for i in data_object]
38+
if type(data_object[0]) == str
39+
else [str(eval(f"object.{attr_filter}")) for object in data_object]
40+
)
3741
ids = (
3842
str(tuple(object_id)).replace(",", "")
3943
if len(object_id) == 1
4044
else str(tuple(object_id))
4145
)
4246
return "{} IN {}".format(first_attr, ids)
47+
48+
49+
def create_custom_query_from_str(
50+
data: str, first_attr, delimiter: str = ","
51+
) -> str:
52+
data = data.split(delimiter)
53+
if len(data) > 1:
54+
query_str = create_in_condition(data, first_attr=first_attr)
55+
else:
56+
query_str = "{} = '{}'".format(first_attr, data[0])
57+
return query_str

0 commit comments

Comments
 (0)