Skip to content

Commit e9c9a2e

Browse files
authored
feat: TT-384-re add activity name to time entries
1 parent df3fe5c commit e9c9a2e

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

commons/data_access_layer/file_stream.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import os
2-
from azure.storage.blob.blockblobservice import BlockBlobService
1+
import tempfile
2+
3+
from azure.storage.blob import BlockBlobService
4+
from utils.azure_users import AzureConnection
35

4-
ACCOUNT_KEY = os.environ.get('AZURE_STORAGE_ACCOUNT_KEY')
56

67
class FileStream:
8+
ACCOUNT_KEY = AzureConnection().get_blob_storage_key()
9+
account_name:str
10+
container_name:str
11+
blob_service:BlockBlobService
12+
713
def __init__(self, account_name:str, container_name:str):
814
"""
915
Initialize the FileStream object. which is used to get the file stream from Azure Blob Storage.
@@ -12,13 +18,17 @@ def __init__(self, account_name:str, container_name:str):
1218
"""
1319
self.account_name = account_name
1420
self.container_name = container_name
15-
self.blob_service = BlockBlobService(account_name=self.account_name, account_key=ACCOUNT_KEY)
1621

1722
def get_file_stream(self, filename:str):
18-
import tempfile
1923
try:
2024
local_file = tempfile.NamedTemporaryFile()
21-
self.blob_service.get_blob_to_stream(self.container_name, filename, stream=local_file)
25+
self.blob_service = BlockBlobService(account_name=self.account_name, account_key=self.ACCOUNT_KEY)
26+
self.blob_service.get_blob_to_stream(
27+
container_name=self.container_name,
28+
blob_name= filename,
29+
stream=local_file,
30+
max_connections=300
31+
)
2232

2333
local_file.seek(0)
2434
return local_file

time_tracker_api/activities/activities_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def find_all_from_blob_storage(
123123
tenant_id_value = self.find_partition_key_value(event_context)
124124
function_mapper = self.get_mapper_or_dict(mapper)
125125
if tenant_id_value is None:
126-
return []
126+
return [{"result": "error", "message": "tenant_id is None"}]
127127

128128
fs = FileStream("storageaccounteystr82c5","tt-common-files")
129129
result = fs.get_file_stream(file_name)
@@ -143,7 +143,7 @@ def get_all_with_id_in_list(
143143
activity_ids,
144144
)
145145

146-
def get_all(
146+
def get_all_v1(
147147
self,
148148
conditions: dict = None,
149149
activities_id: List = None,
@@ -162,7 +162,7 @@ def get_all(
162162
)
163163
return activities
164164

165-
def get_all_test(self, conditions: dict = None) -> list:
165+
def get_all(self,**kwargs) -> list:
166166
event_ctx = self.create_event_context("read-many")
167167
activities = self.repository.find_all_from_blob_storage(event_context=event_ctx)
168168
return activities

utils/azure_users.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class MSConfig:
1313
'MS_SECRET',
1414
'MS_SCOPE',
1515
'MS_ENDPOINT',
16-
'USERID'
16+
'USERID',
17+
'AZURE_STORAGE_ACCOUNT_KEY'
1718
]
1819

1920
check_variables_are_defined(ms_variables)
@@ -24,6 +25,7 @@ class MSConfig:
2425
SCOPE = os.environ.get('MS_SCOPE')
2526
ENDPOINT = os.environ.get('MS_ENDPOINT')
2627
USERID = os.environ.get('USERID')
28+
AZURE_STORAGE_ACCOUNT_KEY = os.environ.get('AZURE_STORAGE_ACCOUNT_KEY')
2729

2830

2931
class BearerAuth(requests.auth.AuthBase):
@@ -68,6 +70,9 @@ def __init__(self, config=MSConfig):
6870
self.access_token = self.get_token()
6971
self.groups_and_users = None
7072

73+
def get_blob_storage_key(self) -> str:
74+
return self.config.AZURE_STORAGE_ACCOUNT_KEY
75+
7176
def get_msal_client(self):
7277
client = msal.ConfidentialClientApplication(
7378
self.config.CLIENT_ID,

utils/extend_model.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def add_project_info_to_time_entries(time_entries, projects):
9696
setattr(time_entry, 'customer_name', project.customer_name)
9797

9898

99-
def add_activity_name_to_time_entries(time_entries, activities):
99+
def add_activity_name_to_time_entries_v1(time_entries, activities):
100100
for time_entry in time_entries:
101101
for activity in activities:
102102
if time_entry.activity_id == activity.id:
@@ -107,6 +107,19 @@ def add_activity_name_to_time_entries(time_entries, activities):
107107
)
108108
setattr(time_entry, 'activity_name', name)
109109

110+
def add_activity_name_to_time_entries(time_entries, activities):
111+
for time_entry in time_entries:
112+
result = [x for x in activities if time_entry.activity_id == x.id]
113+
if result:
114+
name = (
115+
result[0].name + " (archived)"
116+
if result[0].is_deleted()
117+
else result[0].name
118+
)
119+
setattr(time_entry, 'activity_name', name)
120+
else:
121+
setattr(time_entry, 'activity_name', "activity")
122+
110123

111124
def add_user_email_to_time_entries(time_entries, users):
112125
for time_entry in time_entries:

0 commit comments

Comments
 (0)