Skip to content

Commit 8e0d123

Browse files
committed
refactor: 🚚 move custom modules to utils folder
1 parent a3a6654 commit 8e0d123

File tree

7 files changed

+66
-47
lines changed

7 files changed

+66
-47
lines changed

tests/time_tracker_api/time_entries/time_entries_namespace_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
from commons.data_access_layer.cosmos_db import (
1111
current_datetime,
1212
current_datetime_str,
13-
get_date_range_of_month,
1413
get_current_month,
1514
get_current_year,
1615
)
1716

18-
from time_tracker_api.time_entries.custom_modules import worked_time
17+
from utils import worked_time
18+
1919
from time_tracker_api.time_entries.time_entries_model import (
2020
TimeEntriesCosmosDBDao,
2121
)

time_tracker_api/projects/custom_modules/utils.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

time_tracker_api/projects/projects_model.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
from dataclasses import dataclass
22
from azure.cosmos import PartitionKey
3-
from commons.data_access_layer.cosmos_db import CosmosDBModel, CosmosDBDao, CosmosDBRepository
3+
from commons.data_access_layer.cosmos_db import (
4+
CosmosDBModel,
5+
CosmosDBDao,
6+
CosmosDBRepository,
7+
)
48
from time_tracker_api.database import CRUDDao, APICosmosDBDao
5-
from time_tracker_api.customers.customers_model import create_dao as customers_create_dao
9+
from time_tracker_api.customers.customers_model import (
10+
create_dao as customers_create_dao,
11+
)
612
from time_tracker_api.customers.customers_model import CustomerCosmosDBModel
713

8-
from time_tracker_api.projects.custom_modules.utils import (
9-
add_customer_name_to_projects
10-
)
14+
from utils.extend_model import add_customer_name_to_projects
15+
1116

1217
class ProjectDao(CRUDDao):
1318
pass
@@ -17,10 +22,8 @@ class ProjectDao(CRUDDao):
1722
'id': 'project',
1823
'partition_key': PartitionKey(path='/tenant_id'),
1924
'unique_key_policy': {
20-
'uniqueKeys': [
21-
{'paths': ['/name', '/customer_id', '/deleted']},
22-
]
23-
}
25+
'uniqueKeys': [{'paths': ['/name', '/customer_id', '/deleted']},]
26+
},
2427
}
2528

2629

@@ -36,7 +39,7 @@ class ProjectCosmosDBModel(CosmosDBModel):
3639
technologies: list
3740

3841
def __init__(self, data):
39-
super(ProjectCosmosDBModel, self).__init__(data) # pragma: no cover
42+
super(ProjectCosmosDBModel, self).__init__(data) # pragma: no cover
4043

4144
def __contains__(self, item):
4245
if type(item) is CustomerCosmosDBModel:
@@ -53,9 +56,12 @@ def __str___(self):
5356

5457
class ProjectCosmosDBRepository(CosmosDBRepository):
5558
def __init__(self):
56-
CosmosDBRepository.__init__(self, container_id=container_definition['id'],
57-
partition_key_attribute='tenant_id',
58-
mapper=ProjectCosmosDBModel)
59+
CosmosDBRepository.__init__(
60+
self,
61+
container_id=container_definition['id'],
62+
partition_key_attribute='tenant_id',
63+
mapper=ProjectCosmosDBModel,
64+
)
5965

6066

6167
class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao):
@@ -75,12 +81,14 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
7581

7682
customers_id = [customer.id for customer in customers]
7783
conditions = conditions if conditions else {}
78-
custom_condition = "c.customer_id IN {}".format(str(tuple(customers_id)))
84+
custom_condition = "c.customer_id IN {}".format(
85+
str(tuple(customers_id))
86+
)
7987
# TODO this must be refactored to be used from the utils module ↑
8088
if "custom_sql_conditions" in kwargs:
8189
kwargs["custom_sql_conditions"].append(custom_condition)
8290
else:
83-
kwargs["custom_sql_conditions"] = [custom_condition]
91+
kwargs["custom_sql_conditions"] = [custom_condition]
8492
projects = self.repository.find_all(event_ctx, conditions, **kwargs)
8593

8694
add_customer_name_to_projects(projects, customers)

time_tracker_api/time_entries/custom_modules/utils.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
)
1818
from commons.data_access_layer.database import EventContext
1919

20-
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.projects_model import ProjectCosmosDBModel, create_dao as project_create_dao
20+
from utils.extend_model import add_project_name_to_time_entries
21+
from utils import worked_time
22+
23+
from time_tracker_api.projects.projects_model import ProjectCosmosDBModel
2524
from time_tracker_api.projects import projects_model
2625
from time_tracker_api.database import CRUDDao, APICosmosDBDao
2726
from time_tracker_api.security import current_user_id
@@ -142,11 +141,17 @@ def find_all(
142141

143142
if time_entries:
144143
projects_id = [project.project_id for project in time_entries]
145-
p_ids = str(tuple(projects_id)).replace(",", "") if len(projects_id) == 1 else str(tuple(projects_id))
144+
p_ids = (
145+
str(tuple(projects_id)).replace(",", "")
146+
if len(projects_id) == 1
147+
else str(tuple(projects_id))
148+
)
146149
custom_conditions = "c.id IN {}".format(p_ids)
147150
# TODO this must be refactored to be used from the utils module ↑
148151
project_dao = projects_model.create_dao()
149-
projects = project_dao.get_all(custom_sql_conditions=[custom_conditions])
152+
projects = project_dao.get_all(
153+
custom_sql_conditions=[custom_conditions]
154+
)
150155
add_project_name_to_time_entries(time_entries, projects)
151156
return time_entries
152157

@@ -306,7 +311,9 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
306311
conditions.update({"owner_id": event_ctx.user_id})
307312

308313
date_range = self.handle_date_filter_args(args=conditions)
309-
return self.repository.find_all(event_ctx, conditions=conditions, date_range=date_range)
314+
return self.repository.find_all(
315+
event_ctx, conditions=conditions, date_range=date_range
316+
)
310317

311318
def get(self, id):
312319
event_ctx = self.create_event_context("read")
@@ -397,7 +404,9 @@ def handle_date_filter_args(args: dict) -> dict:
397404
else:
398405
month = get_current_month()
399406
year = get_current_year()
400-
return date_range if date_range else get_date_range_of_month(year, month)
407+
return (
408+
date_range if date_range else get_date_range_of_month(year, month)
409+
)
401410

402411

403412
def create_dao() -> TimeEntriesDao:

utils/extend_model.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# TODO : check if we can improve this by using the overwritten __add__ method
2+
def add_customer_name_to_projects(projects, customers):
3+
for project in projects:
4+
for customer in customers:
5+
if project.customer_id == customer.id:
6+
setattr(project, 'customer_name', customer.name)
7+
8+
9+
def add_project_name_to_time_entries(time_entries, projects):
10+
for time_entry in time_entries:
11+
for project in projects:
12+
if time_entry.project_id == project.id:
13+
setattr(time_entry, 'project_name', project.name)

time_tracker_api/time_entries/custom_modules/worked_time.py renamed to utils/worked_time.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
current_datetime_str,
55
datetime_str,
66
get_current_month,
7-
get_current_year
7+
get_current_year,
88
)
99

1010

1111
def start_datetime_of_current_month() -> datetime:
12-
return datetime(year=get_current_year(), month=get_current_month(), day=1, tzinfo=timezone.utc)
12+
return datetime(
13+
year=get_current_year(),
14+
month=get_current_month(),
15+
day=1,
16+
tzinfo=timezone.utc,
17+
)
1318

1419

1520
def start_datetime_of_current_week() -> datetime:
@@ -33,7 +38,9 @@ def str_to_datetime(
3338
value: str, conversion_format: str = '%Y-%m-%dT%H:%M:%S.%fZ'
3439
) -> datetime:
3540
if 'Z' in value:
36-
return datetime.strptime(value, conversion_format).astimezone(timezone.utc)
41+
return datetime.strptime(value, conversion_format).astimezone(
42+
timezone.utc
43+
)
3744
else:
3845
return datetime.fromisoformat(value).astimezone(timezone.utc)
3946

0 commit comments

Comments
 (0)