Skip to content

Commit 47be5ca

Browse files
authoredFeb 8, 2024
Merge pull request #148 from iMMAP/feat/project-export-excel-sheet
Feat/project export excel sheet
2 parents f814c6c + b41114b commit 47be5ca

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed
 

‎src/rh/exports.py

+37-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
from .models import (
15-
Project,
15+
Project, Disaggregation
1616
)
1717

1818
#############################################
@@ -42,12 +42,14 @@ def post(self, request, project_id):
4242
"""
4343
try:
4444
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+
4547

4648
workbook = Workbook()
4749

4850
self.write_project_sheet(workbook, project)
4951
self.write_population_sheet(workbook, project)
50-
self.write_target_locations_sheet(workbook, project)
52+
self.write_target_locations_sheet(workbook, project, disaggregations)
5153
self.write_budget_progress_sheet(workbook, project)
5254

5355
excel_file = BytesIO()
@@ -202,7 +204,7 @@ def write_population_data_rows(self, sheet, project):
202204
project (Project): The project object.
203205
"""
204206
activity_plans = project.activityplan_set.all()
205-
for plan in activity_plans:
207+
for activity_idx, plan in enumerate(activity_plans, start=1):
206208
row = [
207209
plan.activity_domain.name if plan.activity_domain else None,
208210
plan.activity_type.name if plan.activity_type else None,
@@ -211,9 +213,9 @@ def write_population_data_rows(self, sheet, project):
211213
]
212214

213215
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)
215217

216-
def write_target_locations_sheet(self, workbook, project):
218+
def write_target_locations_sheet(self, workbook, project, disaggregations):
217219
"""
218220
Write the target locations sheet to the workbook.
219221
@@ -224,19 +226,26 @@ def write_target_locations_sheet(self, workbook, project):
224226

225227
sheet = workbook.create_sheet(title="Target Locations")
226228

229+
# Fetch all disaggregations to use as headers
230+
disaggregations = [disaggregation.name for disaggregation in Disaggregation.objects.all()]
231+
227232
# Define column headers and types for Sheet 3
228233
columns = [
229234
{"header": "Province", "type": "string", "width": 20},
230235
{"header": "District", "type": "string", "width": 20},
231236
{"header": "Zone/Ward", "type": "string", "width": 20},
232237
]
233238

239+
# Add disaggregation names as headers
240+
columns.extend({"header": disaggregation, "type": "string", "width": 20} for disaggregation in disaggregations)
241+
242+
234243
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)
236245

237246
sheet.freeze_panes = sheet["A2"]
238247

239-
def write_target_locations_data_rows(self, sheet, project):
248+
def write_target_locations_data_rows(self, sheet, project, disaggregations):
240249
"""
241250
Write target locations data rows to the sheet.
242251
@@ -245,15 +254,34 @@ def write_target_locations_data_rows(self, sheet, project):
245254
project (Project): The project object.
246255
"""
247256
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+
# )
249261
row = [
250262
location.province.name,
251263
location.district.name,
252264
location.zone.name if location.zone else None,
253265
]
254266

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+
255283
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)
257285

258286
def write_budget_progress_sheet(self, workbook, project):
259287
"""

0 commit comments

Comments
 (0)