Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Revert "Refactor projects and time entries classes"
  • Loading branch information
Angeluz-07 authored Nov 10, 2020
commit f9b0f481c213b3d6049211ee0383030995f237ae
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from time_tracker_api import create_app
from time_tracker_api.database import init_sql
from time_tracker_api.security import get_or_generate_dev_secret_key
from time_tracker_api.time_entries.time_entries_repository import (
from time_tracker_api.time_entries.time_entries_model import (
TimeEntryCosmosDBRepository,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

from commons.data_access_layer.database import EventContext
from time_tracker_api.time_entries.time_entries_model import (
TimeEntryCosmosDBRepository,
TimeEntryCosmosDBModel,
)
from time_tracker_api.time_entries.time_entries_repository import TimeEntryCosmosDBRepository


def create_time_entry(
start_date: str,
Expand Down
68 changes: 0 additions & 68 deletions time_tracker_api/projects/projects_dao.py

This file was deleted.

71 changes: 71 additions & 0 deletions time_tracker_api/projects/projects_model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
from dataclasses import dataclass
from azure.cosmos import PartitionKey
from commons.data_access_layer.cosmos_db import (
CosmosDBModel,
CosmosDBDao,
CosmosDBRepository,
)
from time_tracker_api.database import CRUDDao, APICosmosDBDao
from time_tracker_api.customers.customers_model import (
create_dao as customers_create_dao,
)
from time_tracker_api.customers.customers_model import CustomerCosmosDBModel

from utils.extend_model import add_customer_name_to_projects


class ProjectDao(CRUDDao):
pass


container_definition = {
'id': 'project',
'partition_key': PartitionKey(path='/tenant_id'),
'unique_key_policy': {
'uniqueKeys': [{'paths': ['/name', '/customer_id', '/deleted']},]
},
}


@dataclass()
class ProjectCosmosDBModel(CosmosDBModel):
Expand All @@ -30,3 +52,52 @@ def __repr__(self):

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


class ProjectCosmosDBRepository(CosmosDBRepository):
def __init__(self):
CosmosDBRepository.__init__(
self,
container_id=container_definition['id'],
partition_key_attribute='tenant_id',
mapper=ProjectCosmosDBModel,
)


class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao):
def __init__(self, repository):
CosmosDBDao.__init__(self, repository)

def get_all(self, conditions: dict = None, **kwargs) -> list:
"""
Get all the projects an active client has
:param (dict) conditions: Conditions for querying the database
:param (dict) kwargs: Pass arguments
:return (list): ProjectCosmosDBModel object list
"""
event_ctx = self.create_event_context("read-many")
customer_dao = customers_create_dao()
customers = customer_dao.get_all(
max_count=kwargs.get('max_count', None)
)

customers_id = [customer.id for customer in customers]
conditions = conditions if conditions else {}
custom_condition = "c.customer_id IN {}".format(
str(tuple(customers_id))
)
# TODO this must be refactored to be used from the utils module ↑
if "custom_sql_conditions" in kwargs:
kwargs["custom_sql_conditions"].append(custom_condition)
else:
kwargs["custom_sql_conditions"] = [custom_condition]
projects = self.repository.find_all(event_ctx, conditions, **kwargs)

add_customer_name_to_projects(projects, customers)
return projects


def create_dao() -> ProjectDao:
repository = ProjectCosmosDBRepository()

return ProjectCosmosDBDao(repository)
2 changes: 1 addition & 1 deletion time_tracker_api/projects/projects_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from time_tracker_api.api import common_fields, create_attributes_filter, UUID, api, remove_required_constraint, \
NullableString
from time_tracker_api.projects.projects_dao import create_dao
from time_tracker_api.projects.projects_model import create_dao

faker = Faker()

Expand Down
Loading