| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | from __future__ import annotations |
| 16 | |
| 17 | import csv |
| 18 | import io |
| 19 | import json |
| 20 | from html import escape |
| 21 | from typing import Any |
| 22 | |
| 23 | from .document import Cards, Data, Note, Report, Table |
| 24 | |
| 25 | CSV_DELIMITER = ";" |
| 26 | CSV_ENCODING = "utf-8-sig" |
| 27 | |
| 28 | |
| 29 | def display_value(value: Any) -> str: |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | if value is None: |
| 36 | return "" |
| 37 | if isinstance(value, dict): |
| 38 | if "display" in value: |
| 39 | return str(value["display"]) |
| 40 | return json.dumps(value, ensure_ascii=False) |
| 41 | if isinstance(value, bool): |
| 42 | return "evet" if value else "hayır" |
| 43 | if isinstance(value, list): |
| 44 | return ", ".join(display_value(v) for v in value) |
| 45 | return str(value) |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | def to_json(report: Report) -> str: |
| 54 | |
| 55 | return json.dumps(report.to_dict(), ensure_ascii=False, indent=2, default=str) |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | def table_to_csv(table: Table) -> str: |
| 64 | |
| 65 | buffer = io.StringIO(newline="") |
| 66 | writer = csv.writer(buffer, delimiter=CSV_DELIMITER, lineterminator="\r\n") |
| 67 | writer.writerow([c.label for c in table.columns]) |
| 68 | for row in table.rows: |
| 69 | writer.writerow([display_value(row.get(c.key)) for c in table.columns]) |
| 70 | return buffer.getvalue() |
| 71 | |
| 72 | |
| 73 | def csv_tables(report: Report) -> list[tuple[str, str]]: |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | return [(t.id, table_to_csv(t)) for t in report.tables()] |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | _STYLE = """ |
| 87 | :root { color-scheme: light; } |
| 88 | * { box-sizing: border-box; } |
| 89 | body { |
| 90 | margin: 0; padding: 24px; |
| 91 | font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; |
| 92 | font-size: 14px; line-height: 1.5; color: #1a1a1a; background: #f7f7f5; |
| 93 | } |
| 94 | main { max-width: 1000px; margin: 0 auto; background: #fff; padding: 28px; |
| 95 | border: 1px solid #e2e2dd; } |
| 96 | h1 { font-size: 22px; margin: 0 0 4px; } |
| 97 | h2 { font-size: 17px; margin: 28px 0 8px; border-bottom: 2px solid #1a1a1a; |
| 98 | padding-bottom: 4px; } |
| 99 | .meta { color: #555; font-size: 12px; margin-bottom: 16px; } |
| 100 | .note { background: #f1f4f8; border-left: 3px solid #4a6fa5; padding: 8px 12px; |
| 101 | margin: 8px 0 14px; font-size: 12px; color: #333; } |
| 102 | .empty { background: #fff6e5; border-left: 3px solid #b8860b; padding: 10px 12px; |
| 103 | margin: 12px 0; } |
| 104 | table { border-collapse: collapse; width: 100%; margin: 6px 0 4px; font-size: 13px; } |
| 105 | th, td { border: 1px solid #d8d8d2; padding: 5px 8px; text-align: left; |
| 106 | vertical-align: top; } |
| 107 | th { background: #eeeee9; font-weight: 600; } |
| 108 | tbody tr:nth-child(even) { background: #fafaf7; } |
| 109 | td.num { text-align: right; } |
| 110 | .card { border: 1px solid #d8d8d2; padding: 10px 12px; margin: 8px 0; } |
| 111 | .card h3 { font-size: 14px; margin: 0 0 6px; } |
| 112 | .card dl { margin: 0; display: grid; grid-template-columns: 110px 1fr; gap: 2px 8px; } |
| 113 | .card dt { font-weight: 600; color: #444; } |
| 114 | .card dd { margin: 0; } |
| 115 | .tag { display: inline-block; font-size: 11px; border: 1px solid #b9b9b2; |
| 116 | padding: 0 6px; margin-left: 6px; color: #444; } |
| 117 | .low { color: #8a5a00; } |
| 118 | footer { margin-top: 28px; font-size: 11px; color: #666; |
| 119 | border-top: 1px solid #e2e2dd; padding-top: 10px; } |
| 120 | @media print { |
| 121 | body { background: #fff; padding: 0; font-size: 11pt; } |
| 122 | main { border: 0; padding: 0; max-width: none; } |
| 123 | h2 { page-break-after: avoid; } |
| 124 | table, .card { page-break-inside: avoid; } |
| 125 | thead { display: table-header-group; } |
| 126 | .note { background: none; border-left: 2px solid #666; } |
| 127 | @page { margin: 16mm; } |
| 128 | } |
| 129 | """ |
| 130 | |
| 131 | |
| 132 | def _esc(value: Any) -> str: |
| 133 | return escape(display_value(value), quote=True) |
| 134 | |
| 135 | |
| 136 | def _table_html(table: Table) -> str: |
| 137 | parts = [f"<h2>{escape(table.title)}</h2>"] |
| 138 | if table.note: |
| 139 | parts.append(f'<p class="note">{escape(table.note)}</p>') |
| 140 | if not table.rows: |
| 141 | parts.append('<p class="empty">Bu bölümde gösterilecek satır yok.</p>') |
| 142 | return "\n".join(parts) |
| 143 | head = "".join(f"<th>{escape(c.label)}</th>" for c in table.columns) |
| 144 | body = [] |
| 145 | for row in table.rows: |
| 146 | cells = [] |
| 147 | for column in table.columns: |
| 148 | value = row.get(column.key) |
| 149 | css = "" |
| 150 | if isinstance(value, dict) and value.get("value") is None: |
| 151 | css = ' class="low"' |
| 152 | elif isinstance(value, (int, float)) and not isinstance(value, bool): |
| 153 | css = ' class="num"' |
| 154 | cells.append(f"<td{css}>{_esc(value)}</td>") |
| 155 | body.append("<tr>" + "".join(cells) + "</tr>") |
| 156 | parts.append( |
| 157 | "<table><thead><tr>" |
| 158 | + head |
| 159 | + "</tr></thead><tbody>" |
| 160 | + "".join(body) |
| 161 | + "</tbody></table>" |
| 162 | ) |
| 163 | return "\n".join(parts) |
| 164 | |
| 165 | |
| 166 | def _cards_html(cards: Cards) -> str: |
| 167 | parts = [f"<h2>{escape(cards.title)}</h2>"] |
| 168 | if cards.note: |
| 169 | parts.append(f'<p class="note">{escape(cards.note)}</p>') |
| 170 | if not cards.items: |
| 171 | parts.append('<p class="empty">Gösterilecek tavsiye yok.</p>') |
| 172 | return "\n".join(parts) |
| 173 | for item in cards.items: |
| 174 | head = escape(str(item.get("title") or "")) |
| 175 | tags = [str(item.get("rule_id") or ""), str(item.get("confidence_label") or "")] |
| 176 | if item.get("course"): |
| 177 | tags.append(str(item["course"])) |
| 178 | if item.get("about"): |
| 179 | tags.append(str(item["about"])) |
| 180 | tag_html = "".join(f'<span class="tag">{escape(t)}</span>' for t in tags if t) |
| 181 | rows = "".join( |
| 182 | f"<dt>{escape(str(part.get('label')))}</dt>" |
| 183 | f"<dd>{escape(str(part.get('text')))}</dd>" |
| 184 | for part in item.get("why") or [] |
| 185 | ) |
| 186 | parts.append( |
| 187 | f'<div class="card"><h3>{head}{tag_html}</h3><dl>{rows}</dl></div>' |
| 188 | ) |
| 189 | return "\n".join(parts) |
| 190 | |
| 191 | |
| 192 | def _note_html(note: Note) -> str: |
| 193 | body = "<br>".join(escape(line) for line in note.text.splitlines()) |
| 194 | return f"<h2>{escape(note.title)}</h2>\n<p class=\"note\">{body}</p>" |
| 195 | |
| 196 | |
| 197 | def _data_html(data: Data) -> str: |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | rows = "".join( |
| 204 | f"<tr><td>{escape(str(name))}</td>" |
| 205 | f"<td class=\"num\">{len(value) if isinstance(value, (list, dict)) else 1}</td></tr>" |
| 206 | for name, value in sorted(data.data.items()) |
| 207 | ) |
| 208 | note = f'<p class="note">{escape(data.note)}</p>' if data.note else "" |
| 209 | return ( |
| 210 | f"<h2>{escape(data.title)}</h2>{note}" |
| 211 | "<table><thead><tr><th>Tablo</th><th>Satır</th></tr></thead>" |
| 212 | f"<tbody>{rows}</tbody></table>" |
| 213 | '<p class="note">Ham gövde JSON biçimindedir; bu sayfada yalnız ' |
| 214 | "muhasebesi gösterilir.</p>" |
| 215 | ) |
| 216 | |
| 217 | |
| 218 | def to_html(report: Report) -> str: |
| 219 | |
| 220 | from . import text as text_mod |
| 221 | |
| 222 | body: list[str] = [] |
| 223 | for section in report.sections: |
| 224 | if isinstance(section, Table): |
| 225 | body.append(_table_html(section)) |
| 226 | elif isinstance(section, Cards): |
| 227 | body.append(_cards_html(section)) |
| 228 | elif isinstance(section, Note): |
| 229 | body.append(_note_html(section)) |
| 230 | elif isinstance(section, Data): |
| 231 | body.append(_data_html(section)) |
| 232 | |
| 233 | subject = f" · {escape(report.subject)}" if report.subject else "" |
| 234 | empty_html = ( |
| 235 | '<p class="empty">Bu rapor <strong>boş</strong> üretildi: ' |
| 236 | "gösterilecek satır bulunamadı.</p>" |
| 237 | if report.empty |
| 238 | else "" |
| 239 | ) |
| 240 | notes_html = "".join(f"<li>{escape(n)}</li>" for n in report.notes) |
| 241 | return f"""<!DOCTYPE html> |
| 242 | <html lang="tr"> |
| 243 | <head> |
| 244 | <meta charset="utf-8"> |
| 245 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 246 | <title>{escape(report.title)}</title> |
| 247 | <style>{_STYLE}</style> |
| 248 | </head> |
| 249 | <body> |
| 250 | <main> |
| 251 | <h1>{escape(report.title)}</h1> |
| 252 | <p class="meta">Okul: {escape(report.school)}{subject} · Üretim: |
| 253 | {escape(text_mod.date_tr(report.generated_at))} · Rapor tipi: |
| 254 | {escape(report.kind)}</p> |
| 255 | {empty_html} |
| 256 | {"".join(body)} |
| 257 | <footer> |
| 258 | <strong>Bu rapor nasıl okunur</strong> |
| 259 | <ul>{notes_html}</ul> |
| 260 | Kaynak: ZEKA'nın kendi veritabanı (beş tablo). Okul veritabanına yazılmaz, |
| 261 | hiçbir kayıt değiştirilmez. |
| 262 | </footer> |
| 263 | </main> |
| 264 | </body> |
| 265 | </html> |
| 266 | """ |