Spaces:
Sleeping
Sleeping
Commit ·
e722874
1
Parent(s): cf17e86
Make category/priority badges adaptive
Browse filesReplace qualitative category labels with percentage-based labels (e.g. 100% Original Strength, 95-100% Original Strength, 75-95%, 50-75%, <50%). Replace the fixed badge width with dynamic sizing: measure text widths via pdf.stringWidth (Helvetica-Bold, size 10), add padding, clamp to min/max widths, and compute badge_gap so both badges stay centered. If badges overflow the content area, shrink the gap or scale badge widths to fit. Update label positions and roundedRect draws to use the computed cat_w/pr_w values and adjust minor layout constants for improved responsiveness and readability.
578 unmodified lines5795805815825835845855865875885895905 unmodified lines596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625578 unmodified linescategory = _safe_text(template.get("category"))priority = _safe_text(template.get("priority"))cat_scale = {"0": {"label": "Excellent", "bg": green_100, "text": colors.HexColor("#166534")},"1": {"label": "Good", "bg": green_200, "text": colors.HexColor("#166534")},"2": {"label": "Fair", "bg": yellow_100, "text": colors.HexColor("#854d0e")},"3": {"label": "Poor", "bg": yellow_200, "text": colors.HexColor("#854d0e")},"4": {"label": "Worse", "bg": orange_200, "text": colors.HexColor("#9a3412")},"5": {"label": "Severe", "bg": red_200, "text": colors.HexColor("#991b1b")},}pr_scale = {"1": {"label": "Immediate", "bg": red_200, "text": colors.HexColor("#991b1b")},5 unmodified linescat_text, cat_bg, cat_text_color = _badge_style(category, cat_scale)pr_text, pr_bg, pr_text_color = _badge_style(priority, pr_scale)badge_w = 40 * mmbadge_h = 10 * mmy -= 1 * mmpdf.setFillColor(gray_500)pdf.setFont("Helvetica", 9)badge_gap = 20 * mmtotal_badge_w = badge_w * 2 + badge_gapstart_x = (width - total_badge_w) / 2cat_x = start_xpr_x = start_x + badge_w + badge_gapcat_label_x = cat_x + badge_w / 2pr_label_x = pr_x + badge_w / 2label_y = ypdf.drawCentredString(cat_label_x, label_y, "Category")pdf.drawCentredString(pr_label_x, label_y, "Priority")y -= 10 * mmpdf.setFillColor(cat_bg)pdf.setStrokeColor(gray_200)pdf.roundRect(cat_x, y - 2, badge_w, badge_h, 2 * mm, stroke=1, fill=1)pdf.setFillColor(cat_text_color)pdf.setFont("Helvetica-Bold", 10)pdf.drawCentredString(cat_label_x, y - 2 + badge_h / 2 - 3, cat_text)pdf.setFillColor(pr_bg)pdf.roundRect(pr_x, y - 2, badge_w, badge_h, 2 * mm, stroke=1, fill=1)pdf.setFillColor(pr_text_color)pdf.drawCentredString(pr_label_x, y - 2 + badge_h / 2 - 3, pr_text)y -= 8 * mm578 unmodified lines5795805815825835845855865875885895905 unmodified lines596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650578 unmodified linescategory = _safe_text(template.get("category"))priority = _safe_text(template.get("priority"))cat_scale = {"0": {"label": "100% Original Strength", "bg": green_100, "text": colors.HexColor("#166534")},"1": {"label": "100% Original Strength", "bg": green_200, "text": colors.HexColor("#166534")},"2": {"label": "95-100% Original Strength", "bg": yellow_100, "text": colors.HexColor("#854d0e")},"3": {"label": "75-95% Original Strength", "bg": yellow_200, "text": colors.HexColor("#854d0e")},"4": {"label": "50-75% Original Strength", "bg": orange_200, "text": colors.HexColor("#9a3412")},"5": {"label": "<50% Original Strength", "bg": red_200, "text": colors.HexColor("#991b1b")},}pr_scale = {"1": {"label": "Immediate", "bg": red_200, "text": colors.HexColor("#991b1b")},5 unmodified linescat_text, cat_bg, cat_text_color = _badge_style(category, cat_scale)pr_text, pr_bg, pr_text_color = _badge_style(priority, pr_scale)badge_h = 10 * mmy -= 1 * mmpdf.setFillColor(gray_500)pdf.setFont("Helvetica", 9)# Make badge width adapt to text length while keeping both badges centered.badge_font = "Helvetica-Bold"badge_size = 10badge_padding = 6 * mmmin_badge_w = 28 * mmmax_badge_w = 85 * mmcontent_w = width - 2 * margincat_text_w = pdf.stringWidth(cat_text, badge_font, badge_size) + badge_padding * 2pr_text_w = pdf.stringWidth(pr_text, badge_font, badge_size) + badge_padding * 2cat_w = max(min_badge_w, min(max_badge_w, cat_text_w))pr_w = max(min_badge_w, min(max_badge_w, pr_text_w))badge_gap = 12 * mmtotal_badge_w = cat_w + pr_w + badge_gapif total_badge_w > content_w:badge_gap = max(4 * mm, content_w - (cat_w + pr_w))total_badge_w = cat_w + pr_w + badge_gapif total_badge_w > content_w:available = max(20 * mm, content_w - badge_gap)scale = available / max(cat_w + pr_w, 1)cat_w *= scalepr_w *= scaletotal_badge_w = cat_w + pr_w + badge_gapstart_x = (width - total_badge_w) / 2cat_x = start_xpr_x = start_x + cat_w + badge_gapcat_label_x = cat_x + cat_w / 2pr_label_x = pr_x + pr_w / 2label_y = ypdf.drawCentredString(cat_label_x, label_y, "Category")pdf.drawCentredString(pr_label_x, label_y, "Priority")y -= 10 * mmpdf.setFillColor(cat_bg)pdf.setStrokeColor(gray_200)pdf.roundRect(cat_x, y - 2, cat_w, badge_h, 2 * mm, stroke=1, fill=1)pdf.setFillColor(cat_text_color)pdf.setFont(badge_font, badge_size)pdf.drawCentredString(cat_label_x, y - 2 + badge_h / 2 - 3, cat_text)pdf.setFillColor(pr_bg)pdf.roundRect(pr_x, y - 2, pr_w, badge_h, 2 * mm, stroke=1, fill=1)pdf.setFillColor(pr_text_color)pdf.drawCentredString(pr_label_x, y - 2 + badge_h / 2 - 3, pr_text)y -= 8 * mm
server/app/services/pdf_reportlab.py+40-15