12
12
13
13
14
14
from .models import (
15
- Project ,
15
+ Project , Disaggregation
16
16
)
17
17
18
18
#############################################
@@ -42,12 +42,14 @@ def post(self, request, project_id):
42
42
"""
43
43
try :
44
44
project = Project .objects .get (id = project_id ) # Get the project object
45
+ disaggregations = [disaggregation .name for disaggregation in Disaggregation .objects .all ()]# Get all the disaggregation object
46
+
45
47
46
48
workbook = Workbook ()
47
49
48
50
self .write_project_sheet (workbook , project )
49
51
self .write_population_sheet (workbook , project )
50
- self .write_target_locations_sheet (workbook , project )
52
+ self .write_target_locations_sheet (workbook , project , disaggregations )
51
53
self .write_budget_progress_sheet (workbook , project )
52
54
53
55
excel_file = BytesIO ()
@@ -202,7 +204,7 @@ def write_population_data_rows(self, sheet, project):
202
204
project (Project): The project object.
203
205
"""
204
206
activity_plans = project .activityplan_set .all ()
205
- for plan in activity_plans :
207
+ for activity_idx , plan in enumerate ( activity_plans , start = 1 ) :
206
208
row = [
207
209
plan .activity_domain .name if plan .activity_domain else None ,
208
210
plan .activity_type .name if plan .activity_type else None ,
@@ -211,9 +213,9 @@ def write_population_data_rows(self, sheet, project):
211
213
]
212
214
213
215
for col_idx , value in enumerate (row , start = 1 ):
214
- sheet .cell (row = 2 , column = col_idx , value = value )
216
+ sheet .cell (row = activity_idx + 1 , column = col_idx , value = value )
215
217
216
- def write_target_locations_sheet (self , workbook , project ):
218
+ def write_target_locations_sheet (self , workbook , project , disaggregations ):
217
219
"""
218
220
Write the target locations sheet to the workbook.
219
221
@@ -224,19 +226,26 @@ def write_target_locations_sheet(self, workbook, project):
224
226
225
227
sheet = workbook .create_sheet (title = "Target Locations" )
226
228
229
+ # Fetch all disaggregations to use as headers
230
+ disaggregations = [disaggregation .name for disaggregation in Disaggregation .objects .all ()]
231
+
227
232
# Define column headers and types for Sheet 3
228
233
columns = [
229
234
{"header" : "Province" , "type" : "string" , "width" : 20 },
230
235
{"header" : "District" , "type" : "string" , "width" : 20 },
231
236
{"header" : "Zone/Ward" , "type" : "string" , "width" : 20 },
232
237
]
233
238
239
+ # Add disaggregation names as headers
240
+ columns .extend ({"header" : disaggregation , "type" : "string" , "width" : 20 } for disaggregation in disaggregations )
241
+
242
+
234
243
self .write_sheet_columns (sheet , columns )
235
- self .write_target_locations_data_rows (sheet , project )
244
+ self .write_target_locations_data_rows (sheet , project , disaggregations )
236
245
237
246
sheet .freeze_panes = sheet ["A2" ]
238
247
239
- def write_target_locations_data_rows (self , sheet , project ):
248
+ def write_target_locations_data_rows (self , sheet , project , disaggregations ):
240
249
"""
241
250
Write target locations data rows to the sheet.
242
251
@@ -245,15 +254,34 @@ def write_target_locations_data_rows(self, sheet, project):
245
254
project (Project): The project object.
246
255
"""
247
256
target_locations = project .targetlocation_set .all ()
248
- for location in target_locations :
257
+ for location_idx , location in enumerate (target_locations , start = 1 ):
258
+ # disaggregation_names = ", ".join(
259
+ # disaggregation.name for disaggregation in Disaggregation.objects.all()
260
+ # )
249
261
row = [
250
262
location .province .name ,
251
263
location .district .name ,
252
264
location .zone .name if location .zone else None ,
253
265
]
254
266
267
+ # Create dictionary to map disaggregation names to target values
268
+ disaggregation_values = {disaggregation : "" for disaggregation in disaggregations }
269
+
270
+ # Fetch disaggregation information associated with current location
271
+ location_disaggregations = location .disaggregationlocation_set .all ()
272
+
273
+ # Set values for each disaggregation
274
+ for disaggregation_location in location_disaggregations :
275
+ disaggregation_name = disaggregation_location .disaggregation .name
276
+ target = disaggregation_location .target
277
+ disaggregation_values [disaggregation_name ] = target
278
+
279
+ # Append disaggregation values to row
280
+ row .extend (disaggregation_values [disaggregation ] for disaggregation in disaggregations )
281
+
282
+
255
283
for col_idx , value in enumerate (row , start = 1 ):
256
- sheet .cell (row = 2 , column = col_idx , value = value )
284
+ sheet .cell (row = location_idx + 1 , column = col_idx , value = value )
257
285
258
286
def write_budget_progress_sheet (self , workbook , project ):
259
287
"""
0 commit comments