Skip to content

Commit 83350eb

Browse files
committed
feat: Refactor function
1 parent f87c797 commit 83350eb

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from utils.extend_model import add_project_name_to_time_entries
2121
from utils import worked_time
22+
from utils.extend_model import create_in_condition
2223

2324
from time_tracker_api.projects.projects_model import ProjectCosmosDBModel
2425
from time_tracker_api.projects import projects_model
@@ -140,14 +141,8 @@ def find_all(
140141
)
141142

142143
if time_entries:
143-
projects_id = [str(project.project_id) for project in time_entries]
144-
p_ids = (
145-
str(tuple(projects_id)).replace(",", "")
146-
if len(projects_id) == 1
147-
else str(tuple(projects_id))
148-
)
149-
custom_conditions = "c.id IN {}".format(p_ids)
150-
# TODO this must be refactored to be used from the utils module ↑
144+
custom_conditions = create_in_condition(time_entries, "project_id")
145+
151146
project_dao = projects_model.create_dao()
152147
projects = project_dao.get_all(
153148
custom_sql_conditions=[custom_conditions]
@@ -384,6 +379,8 @@ def get_worked_time(self, conditions: dict = {}):
384379
@staticmethod
385380
def handle_date_filter_args(args: dict) -> dict:
386381
date_range = None
382+
year = None
383+
month = None
387384
if 'month' and 'year' in args:
388385
month = int(args.get("month"))
389386
year = int(args.get("year"))

utils/extend_model.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import re
2+
3+
14
def add_customer_name_to_projects(projects, customers):
25
"""
36
Add attribute customer_name in project model, based on customer_id of the
@@ -26,3 +29,14 @@ def add_project_name_to_time_entries(time_entries, projects):
2629
for project in projects:
2730
if time_entry.project_id == project.id:
2831
setattr(time_entry, 'project_name', project.name)
32+
33+
34+
def create_in_condition(data_object, attr_to_filter, first_attr="c.id"):
35+
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]
37+
ids = (
38+
str(tuple(object_id)).replace(",", "")
39+
if len(object_id) == 1
40+
else str(tuple(object_id))
41+
)
42+
return "{} IN {}".format(first_attr, ids)

0 commit comments

Comments
 (0)