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: add owner email to time-entries payload
  • Loading branch information
Angeluz-07 committed Jun 2, 2020
commit dbea8ea4ab999294ab33ba4b179111626f3091f1
11 changes: 7 additions & 4 deletions time_tracker_api/time_entries/time_entries_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
from utils.extend_model import (
add_project_name_to_time_entries,
add_activity_name_to_time_entries,
)
from utils import worked_time
from utils.worked_time import str_to_datetime
from utils.extend_model import (
create_in_condition,
create_custom_query_from_str,
add_user_email_to_time_entries,
)
from utils import worked_time
from utils.worked_time import str_to_datetime

from utils.azure_users import AzureUsers
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
Expand Down Expand Up @@ -177,6 +178,8 @@ def find_all(
activity_dao = activities_model.create_dao()
activities = activity_dao.get_all()
add_activity_name_to_time_entries(time_entries, activities)

add_user_email_to_time_entries(time_entries, AzureUsers().users())
return time_entries

def on_create(self, new_item_data: dict, event_context: EventContext):
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 @@ -129,6 +129,13 @@
description='Name of the activity associated with the time-entry',
example=faker.word(['development', 'QA']),
),
'owner_email': fields.String(
required=True,
title="Owner's Email",
max_length=50,
description='Email of the user that owns the time-entry',
example=faker.email(),
),
}
time_entry_response_fields.update(common_fields)

Expand Down
43 changes: 42 additions & 1 deletion utils/azure_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class MSALConfig:
"""


class AzureUser:
def __init__(self, id, display_name, email):
self.id = id
self.display_name = display_name
self.email = email


class AzureUsers:
def __init__(self, config=MSALConfig):
self.client = msal.ConfidentialClientApplication(
Expand Down Expand Up @@ -41,7 +48,19 @@ def set_token(self):

def get_user_info_by_id(self, id):
endpoint = f"{self.config.MSAL_ENDPOINT}/users/{id}?api-version=1.6&$select=displayName,otherMails"
print(endpoint)
# print(endpoint)
http_headers = {
'Authorization': f'Bearer {self.access_token}',
'Accept': 'application/json',
'Content-Type': 'application/json',
}
data = requests.get(
endpoint, headers=http_headers, stream=False
).json()
return data

def get_users_info(self):
endpoint = f"{self.config.MSAL_ENDPOINT}/users?api-version=1.6&$select=displayName,otherMails,objectId"
http_headers = {
'Authorization': f'Bearer {self.access_token}',
'Accept': 'application/json',
Expand All @@ -51,3 +70,25 @@ def get_user_info_by_id(self, id):
endpoint, headers=http_headers, stream=False
).json()
return data

def users(self):
endpoint = f"{self.config.MSAL_ENDPOINT}/users?api-version=1.6&$select=displayName,otherMails,objectId"
http_headers = {
'Authorization': f'Bearer {self.access_token}',
#'Accept': 'application/json',
#'Content-Type': 'application/json',
}
data = requests.get(
endpoint, headers=http_headers, stream=False
).json()
# print(data)

users = []
for value in data['value']:
user = AzureUser(
id=value['objectId'],
display_name=value['displayName'],
email=value['otherMails'][0],
)
users.append(user)
return users
7 changes: 7 additions & 0 deletions utils/extend_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ def add_activity_name_to_time_entries(time_entries, activities):
setattr(time_entry, 'activity_name', activity.name)


def add_user_email_to_time_entries(time_entries, users):
for time_entry in time_entries:
for user in users:
if time_entry.owner_id == user.id:
setattr(time_entry, 'owner_email', user.email)


def create_in_condition(
data_object: list, attr_to_filter: str = "", first_attr: str = "c.id"
):
Expand Down