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
Prev Previous commit
Next Next commit
feat: Refactor function
  • Loading branch information
fabidick22 committed May 29, 2020
commit 83350eb5e0cdb669d5532b9b4faff6753bf5d6de
13 changes: 5 additions & 8 deletions time_tracker_api/time_entries/time_entries_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from utils.extend_model import add_project_name_to_time_entries
from utils import worked_time
from utils.extend_model import create_in_condition

from time_tracker_api.projects.projects_model import ProjectCosmosDBModel
from time_tracker_api.projects import projects_model
Expand Down Expand Up @@ -140,14 +141,8 @@ def find_all(
)

if time_entries:
projects_id = [str(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))
)
custom_conditions = "c.id IN {}".format(p_ids)
# TODO this must be refactored to be used from the utils module ↑
custom_conditions = create_in_condition(time_entries, "project_id")

project_dao = projects_model.create_dao()
projects = project_dao.get_all(
custom_sql_conditions=[custom_conditions]
Expand Down Expand Up @@ -384,6 +379,8 @@ def get_worked_time(self, conditions: dict = {}):
@staticmethod
def handle_date_filter_args(args: dict) -> dict:
date_range = None
year = None
month = None
if 'month' and 'year' in args:
month = int(args.get("month"))
year = int(args.get("year"))
Expand Down
14 changes: 14 additions & 0 deletions utils/extend_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import re


def add_customer_name_to_projects(projects, customers):
"""
Add attribute customer_name in project model, based on customer_id of the
Expand Down Expand Up @@ -26,3 +29,14 @@ def add_project_name_to_time_entries(time_entries, projects):
for project in projects:
if time_entry.project_id == project.id:
setattr(time_entry, 'project_name', project.name)


def create_in_condition(data_object, attr_to_filter, first_attr="c.id"):
attr_filter = re.sub('[^a-zA-Z_$0-9]', '', attr_to_filter)
object_id = [str(eval(f"object.{attr_filter}")) for object in data_object]
ids = (
str(tuple(object_id)).replace(",", "")
if len(object_id) == 1
else str(tuple(object_id))
)
return "{} IN {}".format(first_attr, ids)