Skip to content

Commit 5a27fe4

Browse files
authored
Merge pull request #228 from ioet/revert-225-refactor-large-files
Revert "Refactor projects and time entries classes"
2 parents d52ff0b + f9b0f48 commit 5a27fe4

File tree

7 files changed

+75
-614
lines changed

7 files changed

+75
-614
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_repository import (
14+
from time_tracker_api.time_entries.time_entries_model import (
1515
TimeEntryCosmosDBRepository,
1616
)
1717

tests/time_tracker_api/time_entries/time_entries_model_test.py

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

44
from commons.data_access_layer.database import EventContext
55
from time_tracker_api.time_entries.time_entries_model import (
6+
TimeEntryCosmosDBRepository,
67
TimeEntryCosmosDBModel,
78
)
8-
from time_tracker_api.time_entries.time_entries_repository import TimeEntryCosmosDBRepository
9+
910

1011
def create_time_entry(
1112
start_date: str,

time_tracker_api/projects/projects_dao.py

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

time_tracker_api/projects/projects_model.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
from dataclasses import dataclass
2+
from azure.cosmos import PartitionKey
23
from commons.data_access_layer.cosmos_db import (
34
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,
411
)
512
from time_tracker_api.customers.customers_model import CustomerCosmosDBModel
613

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+
729

830
@dataclass()
931
class ProjectCosmosDBModel(CosmosDBModel):
@@ -30,3 +52,52 @@ def __repr__(self):
3052

3153
def __str___(self):
3254
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_dao import create_dao
7+
from time_tracker_api.projects.projects_model import create_dao
88

99
faker = Faker()
1010

0 commit comments

Comments
 (0)