Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
refactor: 🚚 move custom modules to utils folder
  • Loading branch information
Angeluz-07 committed May 20, 2020
commit 8e0d123c88dd28509c5beeb352a75ca31345c995
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from commons.data_access_layer.cosmos_db import (
current_datetime,
current_datetime_str,
get_date_range_of_month,
get_current_month,
get_current_year,
)

from time_tracker_api.time_entries.custom_modules import worked_time
from utils import worked_time

from time_tracker_api.time_entries.time_entries_model import (
TimeEntriesCosmosDBDao,
)
Expand Down
9 changes: 0 additions & 9 deletions time_tracker_api/projects/custom_modules/utils.py

This file was deleted.

38 changes: 23 additions & 15 deletions time_tracker_api/projects/projects_model.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from dataclasses import dataclass
from azure.cosmos import PartitionKey
from commons.data_access_layer.cosmos_db import CosmosDBModel, CosmosDBDao, CosmosDBRepository
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 (
create_dao as customers_create_dao,
)
from time_tracker_api.customers.customers_model import CustomerCosmosDBModel

from time_tracker_api.projects.custom_modules.utils import (
add_customer_name_to_projects
)
from utils.extend_model import add_customer_name_to_projects


class ProjectDao(CRUDDao):
pass
Expand All @@ -17,10 +22,8 @@ class ProjectDao(CRUDDao):
'id': 'project',
'partition_key': PartitionKey(path='/tenant_id'),
'unique_key_policy': {
'uniqueKeys': [
{'paths': ['/name', '/customer_id', '/deleted']},
]
}
'uniqueKeys': [{'paths': ['/name', '/customer_id', '/deleted']},]
},
}


Expand All @@ -36,7 +39,7 @@ class ProjectCosmosDBModel(CosmosDBModel):
technologies: list

def __init__(self, data):
super(ProjectCosmosDBModel, self).__init__(data) # pragma: no cover
super(ProjectCosmosDBModel, self).__init__(data) # pragma: no cover

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

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


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

customers_id = [customer.id for customer in customers]
conditions = conditions if conditions else {}
custom_condition = "c.customer_id IN {}".format(str(tuple(customers_id)))
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]
kwargs["custom_sql_conditions"] = [custom_condition]
projects = self.repository.find_all(event_ctx, conditions, **kwargs)

add_customer_name_to_projects(projects, customers)
Expand Down
9 changes: 0 additions & 9 deletions time_tracker_api/time_entries/custom_modules/utils.py

This file was deleted.

27 changes: 18 additions & 9 deletions time_tracker_api/time_entries/time_entries_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
)
from commons.data_access_layer.database import EventContext

from time_tracker_api.time_entries.custom_modules import worked_time
from time_tracker_api.time_entries.custom_modules.utils import (
add_project_name_to_time_entries,
)
from time_tracker_api.projects.projects_model import ProjectCosmosDBModel, create_dao as project_create_dao
from utils.extend_model import add_project_name_to_time_entries
from utils import worked_time

from time_tracker_api.projects.projects_model import ProjectCosmosDBModel
from time_tracker_api.projects import projects_model
from time_tracker_api.database import CRUDDao, APICosmosDBDao
from time_tracker_api.security import current_user_id
Expand Down Expand Up @@ -142,11 +141,17 @@ def find_all(

if time_entries:
projects_id = [project.project_id for project in time_entries]
p_ids = str(tuple(projects_id)).replace(",", "") if len(projects_id) == 1 else str(tuple(projects_id))
p_ids = (
str(tuple(projects_id)).replace(",", "")
if len(projects_id) == 1
else str(tuple(projects_id))
)
custom_conditions = "c.id IN {}".format(p_ids)
# TODO this must be refactored to be used from the utils module ↑
project_dao = projects_model.create_dao()
projects = project_dao.get_all(custom_sql_conditions=[custom_conditions])
projects = project_dao.get_all(
custom_sql_conditions=[custom_conditions]
)
add_project_name_to_time_entries(time_entries, projects)
return time_entries

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

date_range = self.handle_date_filter_args(args=conditions)
return self.repository.find_all(event_ctx, conditions=conditions, date_range=date_range)
return self.repository.find_all(
event_ctx, conditions=conditions, date_range=date_range
)

def get(self, id):
event_ctx = self.create_event_context("read")
Expand Down Expand Up @@ -397,7 +404,9 @@ def handle_date_filter_args(args: dict) -> dict:
else:
month = get_current_month()
year = get_current_year()
return date_range if date_range else get_date_range_of_month(year, month)
return (
date_range if date_range else get_date_range_of_month(year, month)
)


def create_dao() -> TimeEntriesDao:
Expand Down
13 changes: 13 additions & 0 deletions utils/extend_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# TODO : check if we can improve this by using the overwritten __add__ method
def add_customer_name_to_projects(projects, customers):
for project in projects:
for customer in customers:
if project.customer_id == customer.id:
setattr(project, 'customer_name', customer.name)


def add_project_name_to_time_entries(time_entries, projects):
for time_entry in time_entries:
for project in projects:
if time_entry.project_id == project.id:
setattr(time_entry, 'project_name', project.name)
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
current_datetime_str,
datetime_str,
get_current_month,
get_current_year
get_current_year,
)


def start_datetime_of_current_month() -> datetime:
return datetime(year=get_current_year(), month=get_current_month(), day=1, tzinfo=timezone.utc)
return datetime(
year=get_current_year(),
month=get_current_month(),
day=1,
tzinfo=timezone.utc,
)


def start_datetime_of_current_week() -> datetime:
Expand All @@ -33,7 +38,9 @@ def str_to_datetime(
value: str, conversion_format: str = '%Y-%m-%dT%H:%M:%S.%fZ'
) -> datetime:
if 'Z' in value:
return datetime.strptime(value, conversion_format).astimezone(timezone.utc)
return datetime.strptime(value, conversion_format).astimezone(
timezone.utc
)
else:
return datetime.fromisoformat(value).astimezone(timezone.utc)

Expand Down