Marthee commited on
Commit
a49bc50
·
verified ·
1 Parent(s): 9a1b9a4

Update API.py

Browse files
Files changed (1) hide show
  1. API.py +27 -12
API.py CHANGED
@@ -127,19 +127,13 @@ def AppendtablestoSheets():
127
  # projectSectionsSheet.set_dataframe(start='A1',df=table3)
128
 
129
  # return table1,table2,table3
130
-
131
  def GenerateTables():
132
  """
133
- Optimized function to generate tables for projects, parts, and sections.
134
  """
135
  dict1 = APIValues()
136
 
137
- # Initialize lists to collect data for each table
138
- table1_data = []
139
- table2_data = []
140
- table3_data = []
141
-
142
- # Initialize lists for bulk processing
143
  table1_data = [[item.get('id'), item.get('projectname')] for item in dict1]
144
 
145
  table2_data = [
@@ -155,11 +149,32 @@ def GenerateTables():
155
  for section in part.get('projectsections', [])
156
  ]
157
 
158
- # Convert lists to DataFrames
159
- global table1
160
- global table2
161
- global table3
 
 
 
 
 
 
 
 
 
 
 
162
  table1 = pd.DataFrame(table1_data, columns=['ProjectId', 'ProjectName'])
 
 
 
 
 
 
 
 
 
 
163
  table2 = pd.DataFrame(table2_data, columns=['ProjectId', 'ProjectPartId', 'ProjectPart'])
164
  table3 = pd.DataFrame(table3_data, columns=['ProjectId', 'ProjectPartId', 'ProjectSection'])
165
 
 
127
  # projectSectionsSheet.set_dataframe(start='A1',df=table3)
128
 
129
  # return table1,table2,table3
 
130
  def GenerateTables():
131
  """
132
+ Generate tables and sort projects (table1) by the newest section creation date.
133
  """
134
  dict1 = APIValues()
135
 
136
+ # Collect data for each table
 
 
 
 
 
137
  table1_data = [[item.get('id'), item.get('projectname')] for item in dict1]
138
 
139
  table2_data = [
 
149
  for section in part.get('projectsections', [])
150
  ]
151
 
152
+ # Collect latest 'createdon' timestamps per project
153
+ project_createdon = {}
154
+ for item in dict1:
155
+ project_id = item.get('id')
156
+ created_dates = [
157
+ section.get('createdon')
158
+ for part in item.get('projectparts', [])
159
+ for section in part.get('projectsections', [])
160
+ if section.get('createdon')
161
+ ]
162
+ if created_dates:
163
+ created_dates = pd.to_datetime(created_dates)
164
+ project_createdon[project_id] = max(created_dates)
165
+
166
+ # Create table1 and add 'CreatedOn' for sorting
167
  table1 = pd.DataFrame(table1_data, columns=['ProjectId', 'ProjectName'])
168
+ table1['CreatedOn'] = table1['ProjectId'].map(project_createdon)
169
+
170
+ # Sort by newest created date
171
+ table1.sort_values(by='CreatedOn', ascending=False, inplace=True)
172
+
173
+ # Drop 'CreatedOn' column if not needed
174
+ table1.drop(columns='CreatedOn', inplace=True)
175
+
176
+ # Create other tables
177
+ global table2, table3
178
  table2 = pd.DataFrame(table2_data, columns=['ProjectId', 'ProjectPartId', 'ProjectPart'])
179
  table3 = pd.DataFrame(table3_data, columns=['ProjectId', 'ProjectPartId', 'ProjectSection'])
180