fixed llm prompt
This commit is contained in:
@@ -51,21 +51,24 @@ ROW_MODELS = {
|
||||
}
|
||||
|
||||
EDITABLE_FIELDS = {
|
||||
"perechen": {"position", "designation", "quantity", "note", "is_header", "is_empty", "stretch", "is_underline"},
|
||||
"perechen": {
|
||||
"position", "designation", "quantity", "note",
|
||||
"is_header", "is_empty", "stretch", "is_underline", "cell_stretches",
|
||||
},
|
||||
"specification_pcb": {
|
||||
"format", "zone", "position", "designation", "name", "quantity", "note",
|
||||
"is_header", "is_empty", "stretch", "is_underline",
|
||||
"is_header", "is_empty", "stretch", "is_underline", "cell_stretches",
|
||||
},
|
||||
"specification": {
|
||||
"format", "zone", "position", "designation", "name", "quantity", "note",
|
||||
"is_header", "is_empty", "stretch", "is_underline",
|
||||
"is_header", "is_empty", "stretch", "is_underline", "cell_stretches",
|
||||
},
|
||||
"vedomost": {
|
||||
"name", "product_code", "document_code", "supplier", "where_used",
|
||||
"quantity_per_item", "quantity_in_set", "quantity_for_reg", "total_quantity", "note",
|
||||
"is_header", "is_empty", "stretch", "is_underline",
|
||||
"is_header", "is_empty", "stretch", "is_underline", "cell_stretches",
|
||||
},
|
||||
"simple_list": {"designator", "name", "quantity", "is_empty"},
|
||||
"simple_list": {"designator", "name", "quantity", "is_empty", "cell_stretches"},
|
||||
}
|
||||
|
||||
|
||||
@@ -277,6 +280,72 @@ def set_inscriptions(db: Session, project_id: int, values: dict[int, str]) -> di
|
||||
return get_inscriptions(db, project_id)
|
||||
|
||||
|
||||
def _sort_key_for_field(value: Any) -> tuple[int, float | str]:
|
||||
"""Числа (в т.ч. quantity) раньше строк; пустые в конце."""
|
||||
s = str(value or "").strip().replace(",", ".")
|
||||
if not s:
|
||||
return (2, 0.0)
|
||||
try:
|
||||
return (0, float(s))
|
||||
except ValueError:
|
||||
return (1, s.casefold())
|
||||
|
||||
|
||||
def _reorder_by_ids(rows: list[Any], row_ids: list[int]) -> list[Any]:
|
||||
by_id = {r.id: r for r in rows}
|
||||
ordered: list[Any] = []
|
||||
seen: set[int] = set()
|
||||
for rid in row_ids:
|
||||
obj = by_id.get(rid)
|
||||
if obj is not None and rid not in seen:
|
||||
ordered.append(obj)
|
||||
seen.add(rid)
|
||||
for r in rows:
|
||||
if r.id not in seen:
|
||||
ordered.append(r)
|
||||
return ordered
|
||||
|
||||
|
||||
def _sort_row_objects(
|
||||
rows: list[Any],
|
||||
field: str,
|
||||
order: str = "asc",
|
||||
within_groups: bool = True,
|
||||
) -> list[Any]:
|
||||
reverse = str(order or "asc").lower().startswith("desc")
|
||||
|
||||
def sort_block(block: list[Any]) -> list[Any]:
|
||||
return sorted(
|
||||
block,
|
||||
key=lambda r: _sort_key_for_field(getattr(r, field, "")),
|
||||
reverse=reverse,
|
||||
)
|
||||
|
||||
if not within_groups:
|
||||
headers_empty = [r for r in rows if getattr(r, "is_header", False) or getattr(r, "is_empty", False)]
|
||||
data = [r for r in rows if not getattr(r, "is_header", False) and not getattr(r, "is_empty", False)]
|
||||
# Простая глобальная сортировка данных; служебные строки — в исходном относительном
|
||||
# положении не сохраняем: ставим отсортированные data, затем «лишние» empty/header в конец.
|
||||
# Для ГОСТ обычно within_groups=True.
|
||||
return sort_block(data) + headers_empty
|
||||
|
||||
result: list[Any] = []
|
||||
i = 0
|
||||
n = len(rows)
|
||||
while i < n:
|
||||
r = rows[i]
|
||||
if getattr(r, "is_header", False) or getattr(r, "is_empty", False):
|
||||
result.append(r)
|
||||
i += 1
|
||||
continue
|
||||
block: list[Any] = []
|
||||
while i < n and not getattr(rows[i], "is_header", False) and not getattr(rows[i], "is_empty", False):
|
||||
block.append(rows[i])
|
||||
i += 1
|
||||
result.extend(sort_block(block))
|
||||
return result
|
||||
|
||||
|
||||
def apply_llm_edits(
|
||||
db: Session, project_id: int, table_type: str, edits: list[dict[str, Any]]
|
||||
) -> list[dict[str, Any]]:
|
||||
@@ -312,6 +381,32 @@ def apply_llm_edits(
|
||||
payload["row_index"] = edit.get("row_index", 9999)
|
||||
payload["is_auto_generated"] = False
|
||||
db.add(model(**payload))
|
||||
elif op == "reorder_rows":
|
||||
row_ids = edit.get("row_ids") or []
|
||||
if not isinstance(row_ids, list) or not row_ids:
|
||||
continue
|
||||
rows = db.scalars(
|
||||
select(model).where(model.project_id == project_id).order_by(model.row_index)
|
||||
).all()
|
||||
ordered = _reorder_by_ids(list(rows), [int(x) for x in row_ids])
|
||||
for i, r in enumerate(ordered):
|
||||
r.row_index = i
|
||||
db.flush()
|
||||
elif op == "sort_rows":
|
||||
field = edit.get("field") or (edit.get("fields") or {}).get("field")
|
||||
if not field or field not in allowed:
|
||||
continue
|
||||
order = edit.get("order") or (edit.get("fields") or {}).get("order") or "asc"
|
||||
within = edit.get("within_groups")
|
||||
if within is None:
|
||||
within = True
|
||||
rows = db.scalars(
|
||||
select(model).where(model.project_id == project_id).order_by(model.row_index)
|
||||
).all()
|
||||
ordered = _sort_row_objects(list(rows), str(field), str(order), bool(within))
|
||||
for i, r in enumerate(ordered):
|
||||
r.row_index = i
|
||||
db.flush()
|
||||
db.commit()
|
||||
# reindex
|
||||
rows = db.scalars(
|
||||
|
||||
Reference in New Issue
Block a user