Skip to content

Commit b559493

Browse files
authored
Merge pull request #131 from ioet/fix/dont-display-projects-deleted-customers#121
fix: #131 Don't display projects that belong to deleted customers
2 parents d296444 + 5c87f0e commit b559493

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
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)

0 commit comments

Comments
 (0)