Skip to content

Commit b9610bd

Browse files
authored
Merge pull request #225 from ioet/refactor-large-files
closes #224 - Refactor projects and time entries classes
2 parents 59a6122 + 7c57f9f commit b9610bd

File tree

7 files changed

+614
-75
lines changed

7 files changed

+614
-75
lines changed

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from time_tracker_api import create_app
1212
from time_tracker_api.database import init_sql
1313
from time_tracker_api.security import get_or_generate_dev_secret_key
14-
from time_tracker_api.time_entries.time_entries_model import (
14+
from time_tracker_api.time_entries.time_entries_repository import (
1515
TimeEntryCosmosDBRepository,
1616
)
1717

tests/time_tracker_api/time_entries/time_entries_model_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
from commons.data_access_layer.database import EventContext
55
from time_tracker_api.time_entries.time_entries_model import (
6-
TimeEntryCosmosDBRepository,
76
TimeEntryCosmosDBModel,
87
)
9-
8+
from time_tracker_api.time_entries.time_entries_repository import TimeEntryCosmosDBRepository
109

1110
def create_time_entry(
1211
start_date: str,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from azure.cosmos import PartitionKey
2+
from commons.data_access_layer.cosmos_db import (
3+
CosmosDBDao,
4+
CosmosDBRepository,
5+
)
6+
from time_tracker_api.database import CRUDDao, APICosmosDBDao
7+
from time_tracker_api.customers.customers_model import (
8+
create_dao as customers_create_dao,
9+
)
10+
from time_tracker_api.projects.projects_model import ProjectCosmosDBModel
11+
12+
from utils.extend_model import add_customer_name_to_projects
13+
14+
15+
class ProjectDao(CRUDDao):
16+
pass
17+
18+
19+
container_definition = {
20+
'id': 'project',
21+
'partition_key': PartitionKey(path='/tenant_id'),
22+
'unique_key_policy': {
23+
'uniqueKeys': [{'paths': ['/name', '/customer_id', '/deleted']},]
24+
},
25+
}
26+
27+
28+
class ProjectCosmosDBRepository(CosmosDBRepository):
29+
def __init__(self):
30+
CosmosDBRepository.__init__(
31+
self,
32+
container_id=container_definition['id'],
33+
partition_key_attribute='tenant_id',
34+
mapper=ProjectCosmosDBModel,
35+
)
36+
37+
38+
class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao):
39+
def __init__(self, repository):
40+
CosmosDBDao.__init__(self, repository)
41+
42+
def get_all(self, conditions: dict = None, **kwargs) -> list:
43+
event_ctx = self.create_event_context("read-many")
44+
customer_dao = customers_create_dao()
45+
customers = customer_dao.get_all(
46+
max_count=kwargs.get('max_count', None)
47+
)
48+
49+
customers_id = [customer.id for customer in customers]
50+
conditions = conditions if conditions else {}
51+
custom_condition = "c.customer_id IN {}".format(
52+
str(tuple(customers_id))
53+
)
54+
# TODO this must be refactored to be used from the utils module ↑
55+
if "custom_sql_conditions" in kwargs:
56+
kwargs["custom_sql_conditions"].append(custom_condition)
57+
else:
58+
kwargs["custom_sql_conditions"] = [custom_condition]
59+
projects = self.repository.find_all(event_ctx, conditions, **kwargs)
60+
61+
add_customer_name_to_projects(projects, customers)
62+
return projects
63+
64+
65+
def create_dao() -> ProjectDao:
66+
repository = ProjectCosmosDBRepository()
67+
68+
return ProjectCosmosDBDao(repository)
Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,9 @@
11
from dataclasses import dataclass
2-
from azure.cosmos import PartitionKey
32
from commons.data_access_layer.cosmos_db import (
43
CosmosDBModel,
5-
CosmosDBDao,
6-
CosmosDBRepository,
7-
)
8-
from time_tracker_api.database import CRUDDao, APICosmosDBDao
9-
from time_tracker_api.customers.customers_model import (
10-
create_dao as customers_create_dao,
114
)
125
from time_tracker_api.customers.customers_model import CustomerCosmosDBModel
136

14-
from utils.extend_model import add_customer_name_to_projects
15-
16-
17-
class ProjectDao(CRUDDao):
18-
pass
19-
20-
21-
container_definition = {
22-
'id': 'project',
23-
'partition_key': PartitionKey(path='/tenant_id'),
24-
'unique_key_policy': {
25-
'uniqueKeys': [{'paths': ['/name', '/customer_id', '/deleted']},]
26-
},
27-
}
28-
297

308
@dataclass()
319
class ProjectCosmosDBModel(CosmosDBModel):
@@ -52,52 +30,3 @@ def __repr__(self):
5230

5331
def __str___(self):
5432
return "the project \"%s\"" % self.name # pragma: no cover
55-
56-
57-
class ProjectCosmosDBRepository(CosmosDBRepository):
58-
def __init__(self):
59-
CosmosDBRepository.__init__(
60-
self,
61-
container_id=container_definition['id'],
62-
partition_key_attribute='tenant_id',
63-
mapper=ProjectCosmosDBModel,
64-
)
65-
66-
67-
class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao):
68-
def __init__(self, repository):
69-
CosmosDBDao.__init__(self, repository)
70-
71-
def get_all(self, conditions: dict = None, **kwargs) -> list:
72-
"""
73-
Get all the projects an active client has
74-
:param (dict) conditions: Conditions for querying the database
75-
:param (dict) kwargs: Pass arguments
76-
:return (list): ProjectCosmosDBModel object list
77-
"""
78-
event_ctx = self.create_event_context("read-many")
79-
customer_dao = customers_create_dao()
80-
customers = customer_dao.get_all(
81-
max_count=kwargs.get('max_count', None)
82-
)
83-
84-
customers_id = [customer.id for customer in customers]
85-
conditions = conditions if conditions else {}
86-
custom_condition = "c.customer_id IN {}".format(
87-
str(tuple(customers_id))
88-
)
89-
# TODO this must be refactored to be used from the utils module ↑
90-
if "custom_sql_conditions" in kwargs:
91-
kwargs["custom_sql_conditions"].append(custom_condition)
92-
else:
93-
kwargs["custom_sql_conditions"] = [custom_condition]
94-
projects = self.repository.find_all(event_ctx, conditions, **kwargs)
95-
96-
add_customer_name_to_projects(projects, customers)
97-
return projects
98-
99-
100-
def create_dao() -> ProjectDao:
101-
repository = ProjectCosmosDBRepository()
102-
103-
return ProjectCosmosDBDao(repository)

time_tracker_api/projects/projects_namespace.py

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

55
from time_tracker_api.api import common_fields, create_attributes_filter, UUID, api, remove_required_constraint, \
66
NullableString
7-
from time_tracker_api.projects.projects_model import create_dao
7+
from time_tracker_api.projects.projects_dao import create_dao
88

99
faker = Faker()
1010

0 commit comments

Comments
 (0)