Skip to content

Commit ea2cfe9

Browse files
committed
Merge branch 'master' into fix/return-all-time-metries-range-time#122
2 parents d94fde7 + 65adf5d commit ea2cfe9

File tree

7 files changed

+84
-15
lines changed

7 files changed

+84
-15
lines changed

commons/data_access_layer/cosmos_db.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def create_custom_sql_conditions(custom_sql_conditions: List[str]) -> str:
156156
return ''
157157

158158
@staticmethod
159-
def generate_params(conditions: dict) -> dict:
159+
def generate_params(conditions: dict) -> list:
160160
result = []
161161
for k, v in conditions.items():
162162
result.append({"name": "@%s" % k, "value": v})
@@ -345,9 +345,10 @@ class CosmosDBDao(CRUDDao):
345345
def __init__(self, repository: CosmosDBRepository):
346346
self.repository = repository
347347

348-
def get_all(self, conditions: dict = {}) -> list:
348+
def get_all(self, conditions: dict = None, **kwargs) -> list:
349+
conditions = conditions if conditions else {}
349350
event_ctx = self.create_event_context("read-many")
350-
return self.repository.find_all(event_ctx, conditions=conditions)
351+
return self.repository.find_all(event_ctx, conditions=conditions, **kwargs)
351352

352353
def get(self, id):
353354
event_ctx = self.create_event_context("read")

time_tracker_api/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class CRUDDao(abc.ABC):
1818
@abc.abstractmethod
19-
def get_all(self, conditions: dict):
19+
def get_all(self, conditions: dict = None, **kwargs):
2020
raise NotImplementedError # pragma: no cover
2121

2222
@abc.abstractmethod

time_tracker_api/projects/projects_model.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from dataclasses import dataclass
2-
32
from azure.cosmos import PartitionKey
4-
53
from commons.data_access_layer.cosmos_db import CosmosDBModel, CosmosDBDao, CosmosDBRepository
64
from time_tracker_api.database import CRUDDao, APICosmosDBDao
5+
from time_tracker_api.customers.customers_model import create_dao as customers_create_dao
6+
from time_tracker_api.customers.customers_model import CustomerCosmosDBModel
77

88

99
class ProjectDao(CRUDDao):
@@ -35,19 +35,48 @@ class ProjectCosmosDBModel(CosmosDBModel):
3535
def __init__(self, data):
3636
super(ProjectCosmosDBModel, self).__init__(data) # pragma: no cover
3737

38+
def __contains__(self, item):
39+
if type(item) is CustomerCosmosDBModel:
40+
return True if item.id == self.customer_id else False
41+
else:
42+
raise NotImplementedError
43+
3844
def __repr__(self):
3945
return '<Project %r>' % self.name # pragma: no cover
4046

4147
def __str___(self):
4248
return "the project \"%s\"" % self.name # pragma: no cover
4349

4450

45-
def create_dao() -> ProjectDao:
46-
repository = CosmosDBRepository.from_definition(container_definition,
47-
mapper=ProjectCosmosDBModel)
51+
class ProjectCosmosDBRepository(CosmosDBRepository):
52+
def __init__(self):
53+
CosmosDBRepository.__init__(self, container_id=container_definition['id'],
54+
partition_key_attribute='tenant_id',
55+
mapper=ProjectCosmosDBModel)
56+
4857

49-
class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao):
50-
def __init__(self):
51-
CosmosDBDao.__init__(self, repository)
58+
class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao):
59+
def __init__(self, repository):
60+
CosmosDBDao.__init__(self, repository)
61+
62+
def get_all(self, conditions: dict = None, **kwargs) -> list:
63+
"""
64+
Get all the projects an active client has
65+
:param (dict) conditions: Conditions for querying the database
66+
:param (dict) kwargs: Pass arguments
67+
:return (list): ProjectCosmosDBModel object list
68+
"""
69+
event_ctx = self.create_event_context("read-many")
70+
customer_dao = customers_create_dao()
71+
customers = customer_dao.get_all()
72+
73+
customers_id = [customer.id for customer in customers]
74+
conditions = conditions if conditions else {}
75+
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)
77+
78+
79+
def create_dao() -> ProjectDao:
80+
repository = ProjectCosmosDBRepository()
5281

53-
return ProjectCosmosDBDao()
82+
return ProjectCosmosDBDao(repository)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def add_project_name_to_time_entries(time_entries, projects):
2+
for time_entry in time_entries:
3+
for project in projects:
4+
if time_entry.project_id == project.id:
5+
setattr(time_entry, 'project_name', project.name)

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
from commons.data_access_layer.database import EventContext
1919

2020
from time_tracker_api.time_entries.custom_modules import worked_time
21+
from time_tracker_api.time_entries.custom_modules.utils import (
22+
add_project_name_to_time_entries,
23+
)
24+
from time_tracker_api.projects import projects_model
2125
from time_tracker_api.database import CRUDDao, APICosmosDBDao
2226
from time_tracker_api.security import current_user_id
2327

@@ -106,6 +110,24 @@ def create_sql_date_range_filter(date_range: dict) -> str:
106110
else:
107111
return ''
108112

113+
def find(
114+
self,
115+
id: str,
116+
event_context: EventContext,
117+
peeker: 'function' = None,
118+
visible_only=True,
119+
mapper: Callable = None,
120+
):
121+
time_entry = CosmosDBRepository.find(
122+
self, id, event_context, peeker, visible_only, mapper,
123+
)
124+
125+
project_dao = projects_model.create_dao()
126+
project = project_dao.get(time_entry.project_id)
127+
setattr(time_entry, 'project_name', project.name)
128+
129+
return time_entry
130+
109131
def find_all(
110132
self,
111133
event_context: EventContext,
@@ -119,14 +141,19 @@ def find_all(
119141

120142
custom_params = self.generate_params(date_range)
121143

122-
return CosmosDBRepository.find_all(
144+
time_entries = CosmosDBRepository.find_all(
123145
self,
124146
event_context=event_context,
125147
conditions=conditions,
126148
custom_sql_conditions=custom_sql_conditions,
127149
custom_params=custom_params,
128150
)
129151

152+
project_dao = projects_model.create_dao()
153+
projects = project_dao.get_all()
154+
add_project_name_to_time_entries(time_entries, projects)
155+
return time_entries
156+
130157
def on_create(self, new_item_data: dict, event_context: EventContext):
131158
CosmosDBRepository.on_create(self, new_item_data, event_context)
132159

time_tracker_api/time_entries/time_entries_namespace.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@
115115
description='User who owns the time entry',
116116
example=faker.uuid4(),
117117
),
118+
'project_name': fields.String(
119+
required=True,
120+
title='Project Name',
121+
max_length=50,
122+
description='Name of the project where time-entry was registered',
123+
example=faker.word(['mobile app', 'web app']),
124+
),
118125
}
119126
time_entry_response_fields.update(common_fields)
120127

time_tracker_api/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.11.1'
1+
__version__ = '0.12.1'

0 commit comments

Comments
 (0)