Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'master' into feature/get-time-entries-per-month-and-yea…
…r#106
  • Loading branch information
Angeluz-07 authored Apr 30, 2020
commit f001ece1b376d51a24b478775609a113edaaff6f
4 changes: 2 additions & 2 deletions commons/data_access_layer/cosmos_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ def find(self, id: str, event_context: EventContext, peeker: 'function' = None,
function_mapper = self.get_mapper_or_dict(mapper)
return function_mapper(self.check_visibility(found_item, visible_only))

def find_all(self, partition_key_value: str, conditions: dict = {}, custom_sql_conditions: List[str] = [],
def find_all(self, event_context: EventContext, conditions: dict = {}, custom_sql_conditions: List[str] = [],
custom_params: dict = {}, max_count=None, offset=0, visible_only=True, mapper: Callable = None):
# TODO Use the tenant_id param and change container alias
partition_key_value = self.find_partition_key_value(event_context)
max_count = self.get_page_size_or(max_count)
params = [
{"name": "@partition_key_value", "value": partition_key_value},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from commons.data_access_layer.cosmos_db import current_datetime, \
current_datetime_str, get_date_range_of_month, get_current_month, \
get_current_year, datetime_str
from time_tracker_api.time_entries.time_entries_model import TimeEntriesCosmosDBDao

fake = Faker()

Expand Down Expand Up @@ -477,3 +478,4 @@ def test_find_all_is_called_with_generated_dates(client: FlaskClient,
repository_find_all_mock.assert_called_once_with(partition_key_value=tenant_id,
conditions=conditions,
custom_args=custom_args)

23 changes: 12 additions & 11 deletions time_tracker_api/time_entries/time_entries_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

from commons.data_access_layer.cosmos_db import CosmosDBDao, CosmosDBRepository, CustomError, current_datetime_str, \
CosmosDBModel, get_date_range_of_month, get_current_year, get_current_month
from commons.data_access_layer.database import CRUDDao
from commons.data_access_layer.database import EventContext
from time_tracker_api.database import CRUDDao, APICosmosDBDao
from time_tracker_api.security import current_user_id


Expand Down Expand Up @@ -92,7 +93,7 @@ def create_sql_date_range_filter(date_range: dict) -> str:
else:
return ''

def find_all(self, partition_key_value: str, conditions: dict, date_range: dict):
def find_all(self, event_context: EventContext, conditions: dict, date_range: dict):
custom_sql_conditions = []
custom_sql_conditions.append(
self.create_sql_date_range_filter(date_range)
Expand All @@ -102,14 +103,14 @@ def find_all(self, partition_key_value: str, conditions: dict, date_range: dict)

return CosmosDBRepository.find_all(
self,
partition_key_value=partition_key_value,
event_context=event_context,
conditions=conditions,
custom_sql_conditions=custom_sql_conditions,
custom_params=custom_params
)

def on_create(self, new_item_data: dict):
CosmosDBRepository.on_create(self, new_item_data)
def on_create(self, new_item_data: dict, event_context: EventContext):
CosmosDBRepository.on_create(self, new_item_data, event_context)

if new_item_data.get("start_date") is None:
new_item_data['start_date'] = current_datetime_str()
Expand Down Expand Up @@ -164,7 +165,7 @@ def find_running(self, tenant_id: str, owner_id: str, mapper: Callable = None):
conditions_clause=self.create_sql_where_conditions(conditions),
),
parameters=self.generate_params(conditions),
partition_key=partition_key_value,
partition_key=tenant_id,
max_item_count=1)

function_mapper = self.get_mapper_or_dict(mapper)
Expand All @@ -191,8 +192,7 @@ def validate_data(self, data, event_context: EventContext):
description="There is another time entry in that date range")



class TimeEntriesCosmosDBDao(TimeEntriesDao, CosmosDBDao):
class TimeEntriesCosmosDBDao(APICosmosDBDao, TimeEntriesDao):
def __init__(self, repository):
CosmosDBDao.__init__(self, repository)

Expand All @@ -218,7 +218,8 @@ def checks_owner_and_is_not_started(cls, data: dict):
raise CustomError(HTTPStatus.UNPROCESSABLE_ENTITY, "The specified time entry is already running")

def get_all(self, conditions: dict = {}) -> list:
conditions.update({"owner_id": self.current_user_id()})
event_ctx = self.create_event_context("read-many")
conditions.update({"owner_id": event_ctx.user_id})

if 'month' and 'year' in conditions:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much logic for a single function don't you think? Clean code my friend.

month = int(conditions.get("month"))
Expand All @@ -239,7 +240,7 @@ def get_all(self, conditions: dict = {}) -> list:
'start_date': start_date.isoformat(),
'end_date': end_date.isoformat(),
}
return self.repository.find_all(partition_key_value=self.partition_key_value,
return self.repository.find_all(event_ctx,
conditions=conditions,
date_range=date_range)

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.