Skip to content

Commit 91095b2

Browse files
authored
NOBUG IR docx tweaks (#672)
1 parent d6927c4 commit 91095b2

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

compliance-api/src/compliance_api/services/inspection_record/inspection_record_doc_generator.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ def _add_html_to_container(container, html_text, *, font_size=None, clear_first=
8585
else:
8686
first_para_used = True
8787

88-
for element in soup.children:
88+
# if div, get its children
89+
children = soup.children
90+
if len(list(soup.children)) == 1 and list(soup.children)[0].name == 'div':
91+
children = list(soup.children)[0].children
92+
93+
for element in children:
8994
if element.name == "p":
9095
# Use the first empty paragraph if available
9196
if not first_para_used and hasattr(container, 'paragraphs') and container.paragraphs:
@@ -410,7 +415,7 @@ def _add_requirement_details_table(doc, req):
410415
para = cell.paragraphs[0]
411416

412417
for idx, source in enumerate(source_details):
413-
# Add requirement header
418+
# Add requirement header for first source
414419
if idx == 0:
415420
run = para.add_run(f"Requirement {req.get('sort_order', '')}: ")
416421
run.bold = True
@@ -424,9 +429,10 @@ def _add_requirement_details_table(doc, req):
424429
if source.get('appendix_no'):
425430
run = para.add_run(f" (Appendix {source.get('appendix_no')})")
426431

427-
# Add title if present
432+
para.add_run('\n')
433+
434+
# Add title if present (comes right after requirement title)
428435
if source.get('title'):
429-
run = para.add_run('\n')
430436
run = para.add_run(source.get('title'))
431437
run.bold = True
432438

@@ -462,8 +468,10 @@ def _add_requirement_details_table(doc, req):
462468
error_para = cell.add_paragraph()
463469
error_para.text = f"[Failed to load image: {img.get('original_file_name', 'unknown')}]"
464470

465-
# Add document details
471+
# Add document details (come after source description and images)
466472
for doc_group in source.get('requirement_documents', []):
473+
para = cell.add_paragraph() # New paragraph for document group
474+
para.add_run('\n')
467475
run = para.add_run(doc_group.get('document_title', ''))
468476
run.bold = True
469477

@@ -482,7 +490,7 @@ def _add_requirement_details_table(doc, req):
482490
section_text = f"Section {document.get('section_number')} "
483491
if document.get('section_title'):
484492
section_text += document.get('section_title')
485-
run = para.add_run(section_text + '\n')
493+
run = para.add_run(section_text)
486494
run.bold = True
487495

488496
# Add description
@@ -492,9 +500,32 @@ def _add_requirement_details_table(doc, req):
492500
document.get('description'),
493501
clear_first=False,
494502
)
495-
503+
# Add document images
504+
for img in document.get('document_images', []):
505+
image_url = img.get('image_url')
506+
if image_url:
507+
try:
508+
response = requests.get(image_url)
509+
response.raise_for_status()
510+
image_stream = BytesIO(response.content)
511+
512+
img_para = cell.add_paragraph()
513+
run = img_para.add_run()
514+
run.add_picture(image_stream, width=Inches(4))
515+
516+
if img.get('original_file_name'):
517+
caption_para = cell.add_paragraph()
518+
caption_run = caption_para.add_run(img.get('original_file_name'))
519+
caption_run.font.size = Pt(9)
520+
except RequestException:
521+
error_para = cell.add_paragraph()
522+
error_para.text = f"[Failed to load image: {img.get('original_file_name', 'unknown')}]"
523+
524+
# Add spacing between multiple sources
496525
if idx < len(source_details) - 1:
526+
para = cell.add_paragraph()
497527
para.add_run('\n')
528+
para = cell.add_paragraph() # New paragraph for next source
498529

499530
# Inspection Details/Findings
500531
row = req_table.add_row()
@@ -661,18 +692,21 @@ def set_header_cell(cell, text):
661692
_add_html_to_container(
662693
merged_cell,
663694
preview_data["inspection_scope"],
695+
clear_first=True,
664696
)
665697

666698
if preview_data.get("preliminary_review_details"):
667699
_add_html_to_container(
668700
merged_cell,
669701
preview_data["preliminary_review_details"],
702+
clear_first=False,
670703
)
671704

672705
if preview_data.get("finding_statement"):
673706
_add_html_to_container(
674707
merged_cell,
675708
preview_data["finding_statement"],
709+
clear_first=False,
676710
)
677711

678712
# Row 9: In Attendance

0 commit comments

Comments
 (0)