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
feat(time-entries): include project name in time-entries response
  • Loading branch information
Angeluz-07 committed May 12, 2020
commit c26459bd1f6a9255f94c624daa8c7af9f76c852b
5 changes: 5 additions & 0 deletions time_tracker_api/time_entries/custom_modules/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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)
29 changes: 28 additions & 1 deletion time_tracker_api/time_entries/time_entries_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import abc
from dataclasses import dataclass, field
from typing import List, Callable
from flask import jsonify

from azure.cosmos import PartitionKey
from flask_restplus._http import HTTPStatus
Expand All @@ -18,6 +19,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 import projects_model
from time_tracker_api.database import CRUDDao, APICosmosDBDao
from time_tracker_api.security import current_user_id

Expand Down Expand Up @@ -106,6 +111,24 @@ def create_sql_date_range_filter(date_range: dict) -> str:
else:
return ''

def find(
self,
id: str,
event_context: EventContext,
peeker: 'function' = None,
visible_only=True,
mapper: Callable = None,
):
time_entry = CosmosDBRepository.find(
self, id, event_context, peeker, visible_only, mapper,
)

project_dao = projects_model.create_dao()
project = project_dao.get(time_entry.project_id)
setattr(time_entry, 'project_name', project.name)

return time_entry

def find_all(
self,
event_context: EventContext,
Expand All @@ -119,14 +142,18 @@ def find_all(

custom_params = self.generate_params(date_range)

return CosmosDBRepository.find_all(
time_entries = CosmosDBRepository.find_all(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this returning the time-entries belonging to the JWT user or all of them?
Can you please restrict to only the time-entries belonging to the user in the JWT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this returning the time-entries belonging to the JWT user or all of them?

Yes. That is how it works.

Check out the user_id/owner_id value is passed in the dictionary conditions, in the dao:

class TimeEntriesCosmosDBDao(APICosmosDBDao, TimeEntriesDao):
...
   def get_all(self, conditions: dict = {}) -> list:
        event_ctx = self.create_event_context("read-many")
        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
        )

self,
event_context=event_context,
conditions=conditions,
custom_sql_conditions=custom_sql_conditions,
custom_params=custom_params,
)

projects = project_dao.get_all()
add_project_name_to_time_entries(time_entries, projects)
return time_entries

def on_create(self, new_item_data: dict, event_context: EventContext):
CosmosDBRepository.on_create(self, new_item_data, event_context)

Expand Down
7 changes: 7 additions & 0 deletions time_tracker_api/time_entries/time_entries_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@
description='User who owns the time entry',
example=faker.uuid4(),
),
'project_name': fields.String(
required=True,
title='Project Name',
max_length=50,
description='Name of the project where time-entry was registered',
example=faker.word(['mobile app', 'web app']),
),
}
time_entry_response_fields.update(common_fields)

Expand Down