Skip to content

Commit ceb6ced

Browse files
committed
fix: Update time-entries model #122
1 parent ea2cfe9 commit ceb6ced

File tree

4 files changed

+59
-28
lines changed

4 files changed

+59
-28
lines changed

commons/data_access_layer/cosmos_db.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,8 @@ def find(
222222
function_mapper = self.get_mapper_or_dict(mapper)
223223
return function_mapper(self.check_visibility(found_item, visible_only))
224224

225-
def find_all(
226-
self,
227-
event_context: EventContext,
228-
conditions: dict = {},
229-
custom_sql_conditions: List[str] = [],
230-
custom_params: dict = {},
231-
max_count=None,
232-
offset=0,
233-
visible_only=True,
234-
mapper: Callable = None,
235-
):
225+
def find_all(self, event_context: EventContext, conditions: dict = {}, custom_sql_conditions: List[str] = [],
226+
custom_params: dict = {}, max_count=None, offset=0, visible_only=True, mapper: Callable = None):
236227
partition_key_value = self.find_partition_key_value(event_context)
237228
max_count = self.get_page_size_or(max_count)
238229
params = [
@@ -242,8 +233,8 @@ def find_all(
242233
]
243234
params.extend(self.generate_params(conditions))
244235
params.extend(custom_params)
245-
result = self.container.query_items(
246-
query="""
236+
print("before query")
237+
query_str = """
247238
SELECT * FROM c
248239
WHERE c.{partition_key_attribute}=@partition_key_value
249240
{conditions_clause}
@@ -261,7 +252,10 @@ def find_all(
261252
custom_sql_conditions
262253
),
263254
order_clause=self.create_sql_order_clause(),
264-
),
255+
)
256+
257+
result = self.container.query_items(
258+
query=query_str,
265259
parameters=params,
266260
partition_key=partition_key_value,
267261
max_item_count=max_count,

time_tracker_api/projects/projects_model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
7373
customers_id = [customer.id for customer in customers]
7474
conditions = conditions if conditions else {}
7575
custom_condition = "c.customer_id IN {}".format(str(tuple(customers_id)))
76-
return self.repository.find_all(event_ctx, conditions, custom_sql_conditions=[custom_condition], **kwargs)
76+
if "custom_sql_conditions" in kwargs:
77+
kwargs["custom_sql_conditions"].append(custom_condition)
78+
else:
79+
kwargs["custom_sql_conditions"] = [custom_condition]
80+
return self.repository.find_all(event_ctx, conditions, **kwargs)
7781

7882

7983
def create_dao() -> ProjectDao:

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from time_tracker_api.time_entries.custom_modules.utils import (
2222
add_project_name_to_time_entries,
2323
)
24+
from time_tracker_api.projects.projects_model import ProjectCosmosDBModel, create_dao as project_create_dao
2425
from time_tracker_api.projects import projects_model
2526
from time_tracker_api.database import CRUDDao, APICosmosDBDao
2627
from time_tracker_api.security import current_user_id
@@ -74,6 +75,14 @@ def __init__(self, data): # pragma: no cover
7475
def running(self):
7576
return self.end_date is None
7677

78+
def __add__(self, other):
79+
if type(other) is ProjectCosmosDBModel:
80+
time_entry = self.__class__
81+
time_entry.project_id = other.__dict__
82+
return time_entry
83+
else:
84+
raise NotImplementedError
85+
7786
def __repr__(self):
7887
return '<Time Entry %r>' % self.start_date # pragma: no cover
7988

@@ -102,6 +111,7 @@ def create_sql_ignore_id_condition(id: str):
102111

103112
@staticmethod
104113
def create_sql_date_range_filter(date_range: dict) -> str:
114+
print("data: {}".format(date_range))
105115
if 'start_date' and 'end_date' in date_range:
106116
return """
107117
((c.start_date BETWEEN @start_date AND @end_date) OR
@@ -134,13 +144,12 @@ def find_all(
134144
conditions: dict = {},
135145
date_range: dict = {},
136146
):
137-
custom_sql_conditions = []
138-
custom_sql_conditions.append(
139-
self.create_sql_date_range_filter(date_range)
140-
)
147+
custom_sql_conditions = [self.create_sql_date_range_filter(date_range)]
141148

142-
custom_params = self.generate_params(date_range)
149+
if event_context.is_admin:
150+
conditions.pop("owner_id")
143151

152+
custom_params = self.generate_params(date_range)
144153
time_entries = CosmosDBRepository.find_all(
145154
self,
146155
event_context=event_context,
@@ -149,9 +158,14 @@ def find_all(
149158
custom_params=custom_params,
150159
)
151160

152-
project_dao = projects_model.create_dao()
153-
projects = project_dao.get_all()
154-
add_project_name_to_time_entries(time_entries, projects)
161+
if time_entries:
162+
projects_id = [project.project_id for project in time_entries]
163+
p_ids = str(tuple(projects_id)).replace(",", "") if len(projects_id) == 1 else str(tuple(projects_id))
164+
custom_conditions = "c.id IN {}".format(p_ids)
165+
166+
project_dao = projects_model.create_dao()
167+
projects = project_dao.get_all(custom_sql_conditions=[custom_conditions])
168+
add_project_name_to_time_entries(time_entries, projects)
155169
return time_entries
156170

157171
def on_create(self, new_item_data: dict, event_context: EventContext):
@@ -305,14 +319,18 @@ def checks_owner_and_is_not_started(cls, data: dict):
305319
"The specified time entry is already running",
306320
)
307321

308-
def get_all(self, conditions: dict = {}) -> list:
322+
def get_all(self, conditions: dict = None, **kwargs) -> list:
309323
event_ctx = self.create_event_context("read-many")
310324
conditions.update({"owner_id": event_ctx.user_id})
311325

312-
date_range = self.handle_date_filter_args(args=conditions)
313-
return self.repository.find_all(
314-
event_ctx, conditions=conditions, date_range=date_range
315-
)
326+
if "start_date" and "end_date" in conditions:
327+
date_range = conditions.copy()
328+
date_range.pop("owner_id")
329+
conditions.pop("start_date")
330+
conditions.pop("end_date")
331+
else:
332+
date_range = self.handle_date_filter_args(args=conditions)
333+
return self.repository.find_all(event_ctx, conditions=conditions, date_range=date_range)
316334

317335
def get(self, id):
318336
event_ctx = self.create_event_context("read")

time_tracker_api/time_entries/time_entries_namespace.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@
151151
location='args',
152152
)
153153

154+
attributes_filter.add_argument(
155+
'start_date',
156+
required=False,
157+
store_missing=False,
158+
help="(Filter) Start to filter by",
159+
location='args',
160+
)
161+
attributes_filter.add_argument(
162+
'end_date',
163+
required=False,
164+
store_missing=False,
165+
help="(Filter) End time to filter by",
166+
location='args',
167+
)
168+
154169

155170
@ns.route('')
156171
class TimeEntries(Resource):

0 commit comments

Comments
 (0)