--
This commit is contained in:
+263
-23
@@ -3,17 +3,18 @@ import uuid
|
||||
from collections.abc import Iterable
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Query, status
|
||||
from fastapi import APIRouter, Body, Depends, Query, Request, status
|
||||
from fastapi.responses import Response
|
||||
from sqlalchemy import select, text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.auth.dependencies import require_role
|
||||
from app.common.enums import Role
|
||||
from app.audit.service import AuditService
|
||||
from app.auth.dependencies import client_ip, require_role
|
||||
from app.common.enums import AuditAction, EntityType, Role, FIELD_ROLES
|
||||
from app.common.exceptions import ConflictError, ForbiddenError, NotFoundError, ValidationError
|
||||
from app.config.settings import settings
|
||||
from app.csv_data import approval as approvals
|
||||
from app.csv_data.config import CSV_LABELS, TABLES
|
||||
from app.csv_data.config import CSV_LABELS, FORM_ORDER, TABLES
|
||||
from app.database.session import get_db
|
||||
from app.users.models import User
|
||||
from app.organization.models import Headquarters
|
||||
@@ -62,7 +63,11 @@ async def _reject_repeat(
|
||||
#: Tables whose printed Sl. No. starts over at 1 for each month rather than running on
|
||||
#: forever — a shared master list (e.g. Sales Projection's product list) refilled every
|
||||
#: month, not one MR's own running form.
|
||||
MONTH_NUMBERED_TABLES = {"sales_projection"}
|
||||
MONTH_NUMBERED_TABLES = {"sales_projection", "sales_and_stock_statement"}
|
||||
#: Forms whose rows are the company's product list, shared by every MR. An MR may delete an extra
|
||||
#: copy of a product (a row added by mistake), but never a product's only row — that would take
|
||||
#: the product off everyone's list; they clear its figures instead (a PATCH). The admin may delete any.
|
||||
PRODUCT_LIST_TABLES = {"sales_projection", "sales_and_stock_statement"}
|
||||
|
||||
|
||||
async def _renumber_rows(table_name: str, db: AsyncSession) -> None:
|
||||
@@ -98,7 +103,7 @@ async def _apply_mr_identity(
|
||||
values: dict[str, str | None], subject: User | None, db: AsyncSession
|
||||
) -> dict[str, str | None]:
|
||||
"""Fill NAME / HEADQUARTER from the MR the row is for (the signed-in MR, or the one an admin picked)."""
|
||||
if subject is None or subject.role != Role.MR:
|
||||
if subject is None or subject.role not in FIELD_ROLES:
|
||||
return values
|
||||
headquarters_name = None
|
||||
if subject.headquarters_id:
|
||||
@@ -120,7 +125,7 @@ async def _present_rows_for_user(
|
||||
Admins must see the source values and every row. MRs use the imported rows as
|
||||
their working data, but identity columns must always reflect the logged-in MR.
|
||||
"""
|
||||
if current_user.role != Role.MR:
|
||||
if current_user.role not in FIELD_ROLES:
|
||||
return rows
|
||||
|
||||
headquarters_name = None
|
||||
@@ -168,6 +173,25 @@ async def _apply_sales_projection_product(
|
||||
return values
|
||||
|
||||
|
||||
#: Expense Statement: the TOTAL column is always fare + daily allowance + postage + other expenses.
|
||||
EXPENSE_AMOUNTS = ("fare", "da_hq", "da_ex", "da_out", "postage", "other_expense")
|
||||
|
||||
|
||||
def _apply_expense_total(values: dict[str, str | None]) -> dict[str, str | None]:
|
||||
total = 0.0
|
||||
for column in EXPENSE_AMOUNTS:
|
||||
raw = (values.get(column) or "").replace(",", "").strip()
|
||||
if not raw:
|
||||
continue
|
||||
try:
|
||||
total += float(raw)
|
||||
except ValueError:
|
||||
raise ValidationError("Expense amounts must be numbers", {"column": column}) from None
|
||||
has_amount = any((values.get(column) or "").strip() for column in EXPENSE_AMOUNTS)
|
||||
values["total"] = f"{total:.2f}" if has_amount else None
|
||||
return values
|
||||
|
||||
|
||||
def _select(table_name: str, columns: Iterable[str]) -> str:
|
||||
selected = ", ".join(f'"{column}"' for column in columns)
|
||||
return f'SELECT id, source_row_number, {selected} FROM "{table_name}"'
|
||||
@@ -180,7 +204,7 @@ async def _owner_condition(
|
||||
an Area Manager only the MRs of their headquarters, an admin anyone (or one chosen MR)."""
|
||||
if table_name not in OWNED_TABLES:
|
||||
return [], {}
|
||||
if current_user.role == Role.MR:
|
||||
if current_user.role in FIELD_ROLES:
|
||||
if owner_id not in (None, current_user.id):
|
||||
raise ForbiddenError("You can only see your own forms")
|
||||
return ["owner_user_id = :owner"], {"owner": current_user.id}
|
||||
@@ -194,13 +218,18 @@ async def _owner_condition(
|
||||
|
||||
async def _row_owner(table_name: str, row_id: uuid.UUID, current_user: User, db: AsyncSession) -> uuid.UUID | None:
|
||||
"""Who owns this row, after checking `current_user` is allowed to touch it."""
|
||||
if table_name not in OWNED_TABLES:
|
||||
if table_name not in OWNED_TABLES and table_name not in PER_MR_PRODUCT_TABLES:
|
||||
return None
|
||||
found = (await db.execute(text(f'SELECT owner_user_id FROM "{table_name}" WHERE id = :id'), {"id": row_id})).first()
|
||||
if found is None:
|
||||
raise NotFoundError("CSV row not found", {"id": str(row_id)})
|
||||
owner_id = found[0]
|
||||
if current_user.role == Role.MR and owner_id != current_user.id:
|
||||
if table_name in PER_MR_PRODUCT_TABLES:
|
||||
# A shared product line has no owner; only an MR's own row is theirs alone.
|
||||
if current_user.role in FIELD_ROLES and owner_id not in (None, current_user.id):
|
||||
raise ForbiddenError("This row belongs to another medical representative")
|
||||
return owner_id
|
||||
if current_user.role in FIELD_ROLES and owner_id != current_user.id:
|
||||
raise ForbiddenError("This row belongs to another medical representative")
|
||||
if current_user.role == Role.AM:
|
||||
if owner_id is None:
|
||||
@@ -221,6 +250,8 @@ async def _load_rows(
|
||||
table_name: str, db: AsyncSession, current_user: User, owner_id: uuid.UUID | None = None, month: str | None = None
|
||||
) -> list[dict[str, object]]:
|
||||
columns = _table(table_name)
|
||||
if table_name in PER_MR_PRODUCT_TABLES:
|
||||
return await _load_per_mr_rows(table_name, db, current_user, owner_id, month)
|
||||
conditions, params = await _owner_condition(table_name, current_user, db, owner_id)
|
||||
more, extra = _month_filter(table_name, month)
|
||||
where = f" WHERE {' AND '.join(conditions + more)}" if conditions or more else ""
|
||||
@@ -228,8 +259,116 @@ async def _load_rows(
|
||||
return await _present_rows_for_user([dict(row) for row in result.mappings().all()], current_user, db)
|
||||
|
||||
|
||||
#: Product-list forms where every MR has their own figures. Rows with no owner are the shared
|
||||
#: product list: an MR sees each product they have not started yet as a blank line, and their
|
||||
#: first entry on it becomes their own row. A manager sees the MRs' rows, with the MR's name
|
||||
#: and headquarters taken from the owner.
|
||||
PER_MR_PRODUCT_TABLES = {"sales_projection", "sales_and_stock_statement"}
|
||||
#: What an MR types in (and the month it belongs to): a product line with none of these is blank.
|
||||
_FIGURES = {
|
||||
"sales_projection": ("report_date", "month", "closing_qty", "closing_value", "projection_qty", "projection_value"),
|
||||
"sales_and_stock_statement": ("report_date", "month", "opening", "receipt", "sale", "closing", "value_in_pts"),
|
||||
}
|
||||
|
||||
|
||||
def _product_key(row: dict[str, object]) -> str:
|
||||
return str(row.get("product_name") or "").strip().upper()
|
||||
|
||||
|
||||
def _has_figures(table_name: str, row: dict[str, object]) -> bool:
|
||||
return any(str(row.get(c) or "").strip() for c in _FIGURES[table_name])
|
||||
|
||||
|
||||
async def _view_owner(
|
||||
table_name: str, db: AsyncSession, current_user: User, owner_id: uuid.UUID | None
|
||||
) -> uuid.UUID | None:
|
||||
"""The one MR whose copy is shown: the signed-in MR, or the one a manager picked."""
|
||||
if current_user.role in FIELD_ROLES:
|
||||
return current_user.id
|
||||
if owner_id is None:
|
||||
return None
|
||||
await approvals.assert_owner_in_scope(db, current_user, owner_id)
|
||||
return owner_id
|
||||
|
||||
|
||||
async def _load_per_mr_rows(
|
||||
table_name: str, db: AsyncSession, current_user: User, owner_id: uuid.UUID | None, month: str | None
|
||||
) -> list[dict[str, object]]:
|
||||
columns = _table(table_name)
|
||||
selected = ", ".join(f'"{c}"' for c in columns)
|
||||
result = await db.execute(
|
||||
text(f'SELECT id, source_row_number, owner_user_id, {selected} FROM "{table_name}" ORDER BY source_row_number')
|
||||
)
|
||||
everything = [dict(r) for r in result.mappings().all()]
|
||||
mine = await _view_owner(table_name, db, current_user, owner_id)
|
||||
owner_ids = {r["owner_user_id"] for r in everything if r["owner_user_id"]} | ({mine} if mine else set())
|
||||
people: dict[uuid.UUID, tuple[str, str | None]] = {}
|
||||
if owner_ids:
|
||||
found = await db.execute(
|
||||
select(User.id, User.full_name, Headquarters.name)
|
||||
.outerjoin(Headquarters, Headquarters.id == User.headquarters_id)
|
||||
.where(User.id.in_(owner_ids))
|
||||
)
|
||||
people = {uid: (name, hq) for uid, name, hq in found.all()}
|
||||
|
||||
shown: list[dict[str, object]] = []
|
||||
if mine is not None:
|
||||
own = [r for r in everything if r["owner_user_id"] == mine]
|
||||
started = {_product_key(r) for r in own}
|
||||
for r in everything:
|
||||
if r["owner_user_id"] is None and _product_key(r) not in started:
|
||||
shown.append({**r, **{c: None for c in _FIGURES[table_name]}}) # a blank line of the product list
|
||||
shown += own
|
||||
else:
|
||||
# A manager gets every MR's rows plus the shared product list (the page lays the list out).
|
||||
shown = everything
|
||||
# A row an MR starts on a product keeps that product's place in the list, whenever it was
|
||||
# created; a manager reading several MRs sees them one MR after another.
|
||||
place: dict[str, int] = {}
|
||||
for r in everything:
|
||||
if r["owner_user_id"] is None:
|
||||
place[_product_key(r)] = min(place.get(_product_key(r), r["source_row_number"]), r["source_row_number"])
|
||||
def order(r: dict[str, object]) -> tuple:
|
||||
who = people.get(r["owner_user_id"], ("", None))[0] if mine is None else ""
|
||||
return (who, place.get(_product_key(r), r["source_row_number"]), r["source_row_number"])
|
||||
shown.sort(key=order)
|
||||
|
||||
wanted = (month or "").strip().lower()
|
||||
out: list[dict[str, object]] = []
|
||||
for r in shown:
|
||||
if wanted and str(r.get("month") or "").strip().lower() != wanted:
|
||||
continue
|
||||
item = {k: v for k, v in r.items() if k != "owner_user_id"}
|
||||
who = r["owner_user_id"] or mine # a blank product line is shown as that MR's own
|
||||
if who in people:
|
||||
item["name"], item["headquarter"] = people[who][0], people[who][1] or ""
|
||||
out.append(item)
|
||||
return await _present_rows_for_user(out, current_user, db)
|
||||
|
||||
|
||||
def _month_of_every_product(rows: list[dict[str, object]], month: str, columns: Iterable[str]) -> list[dict[str, object]]:
|
||||
"""A product-list form for one month: every product once — its row for that month, else its
|
||||
still-unused blank row, else an empty line carrying only the product — so the printed form
|
||||
always lists the whole range, filled in where this month has figures."""
|
||||
wanted = month.strip().lower()
|
||||
key = lambda r: str(r.get("product_name") or "").strip().upper() # noqa: E731
|
||||
copies: dict[str, list[dict[str, object]]] = {}
|
||||
for row in rows:
|
||||
if key(row):
|
||||
copies.setdefault(key(row), []).append(row)
|
||||
out: list[dict[str, object]] = []
|
||||
for product, group in copies.items():
|
||||
mine = next((r for r in group if str(r.get("month") or "").strip().lower() == wanted), None) or next(
|
||||
(r for r in group if not str(r.get("month") or "").strip()), None
|
||||
)
|
||||
if mine is None:
|
||||
mine = {c: group[0].get(c) for c in ("product_name", "pts", "value_in_pts", "name", "headquarter") if c in set(columns)}
|
||||
out.append({**mine, "month": month.strip()})
|
||||
return out
|
||||
|
||||
|
||||
def _available_csv_tables(current_user: User) -> list[str]:
|
||||
visible = [name for name in TABLES if name not in HIDDEN_TABLES]
|
||||
visible = [name for name in FORM_ORDER if name not in HIDDEN_TABLES]
|
||||
if current_user.role == Role.AM:
|
||||
return [name for name in visible if name in APPROVAL_TABLES]
|
||||
return visible
|
||||
@@ -243,7 +382,7 @@ def _ensure_table_access(table_name: str, current_user: User) -> None:
|
||||
|
||||
|
||||
@router.get("/tables")
|
||||
async def list_csv_tables(current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM))) -> list[dict[str, object]]:
|
||||
async def list_csv_tables(current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM))) -> list[dict[str, object]]:
|
||||
return [{"name": name, "label": CSV_LABELS[name], "columns": list(TABLES[name])} for name in _available_csv_tables(current_user)]
|
||||
|
||||
|
||||
@@ -288,7 +427,7 @@ def _approval_table(table_name: str) -> str:
|
||||
@router.get("/approvals")
|
||||
async def list_form_approvals(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM)),
|
||||
) -> list[dict[str, object]]:
|
||||
"""Approval state of the forms that carry an Area Manager signature, limited to the MRs this user may see."""
|
||||
return [_approval_out(r, owner, hq) for r, owner, hq in await approvals.list_approvals(db, current_user)]
|
||||
@@ -297,7 +436,7 @@ async def list_form_approvals(
|
||||
@router.get("/owners")
|
||||
async def list_form_owners(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM)),
|
||||
) -> list[dict[str, object]]:
|
||||
"""The medical representatives whose forms this user may open."""
|
||||
hq = {h.id: h.name for h in (await db.execute(select(Headquarters))).scalars()}
|
||||
@@ -336,7 +475,7 @@ async def list_csv_rows(
|
||||
table_name: str,
|
||||
owner_id: uuid.UUID | None = None,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM)),
|
||||
) -> list[dict[str, object]]:
|
||||
_ensure_table_access(table_name, current_user)
|
||||
return await _load_rows(table_name, db, current_user, owner_id)
|
||||
@@ -354,15 +493,19 @@ async def create_csv_row(
|
||||
values = _clean_payload(table_name, payload)
|
||||
owner: User | None = None
|
||||
if table_name in OWNED_TABLES:
|
||||
if current_user.role == Role.MR:
|
||||
if current_user.role in FIELD_ROLES:
|
||||
owner = current_user
|
||||
elif owner_id is None:
|
||||
raise ValidationError("Choose which medical representative this row is for")
|
||||
else:
|
||||
owner = await approvals.assert_owner_in_scope(db, current_user, owner_id)
|
||||
if owner is None and table_name in PER_MR_PRODUCT_TABLES and current_user.role in FIELD_ROLES:
|
||||
owner = current_user
|
||||
values = await _apply_mr_identity(values, owner or current_user, db)
|
||||
if table_name == "sales_projection":
|
||||
values = await _apply_sales_projection_product(values, db, current_user)
|
||||
if table_name == "expense_statement":
|
||||
values = _apply_expense_total(values)
|
||||
if not _has_user_data(table_name, values):
|
||||
raise ValidationError("Add at least one value before creating a row")
|
||||
await _reject_repeat(table_name, values, db, owner.id if owner else None)
|
||||
@@ -384,17 +527,65 @@ async def create_csv_row(
|
||||
return await _get_row(table_name, row_id, db)
|
||||
|
||||
|
||||
async def _stored_owner(table_name: str, row_id: uuid.UUID, db: AsyncSession) -> uuid.UUID | None:
|
||||
found = (await db.execute(text(f'SELECT owner_user_id FROM "{table_name}" WHERE id = :id'), {"id": row_id})).first()
|
||||
if found is None:
|
||||
raise NotFoundError("CSV row not found", {"id": str(row_id)})
|
||||
return found[0]
|
||||
|
||||
|
||||
async def _start_own_row(
|
||||
table_name: str, shared: dict[str, object], values: dict[str, str | None],
|
||||
payload: dict[str, object], db: AsyncSession, current_user: User,
|
||||
) -> dict[str, object]:
|
||||
"""An MR's first entry on a product of the shared list: it becomes their own row, so the
|
||||
shared line stays blank for everyone else."""
|
||||
columns = _table(table_name)
|
||||
new = {c: None for c in columns}
|
||||
for c in ("product_name", "pts"):
|
||||
if c in new:
|
||||
new[c] = shared.get(c)
|
||||
new.update({c: v for c, v in values.items() if c in payload and c not in SERIAL_COLUMNS})
|
||||
new = await _apply_mr_identity(new, current_user, db)
|
||||
if not _has_user_data(table_name, new):
|
||||
raise ValidationError("A row must contain at least one value")
|
||||
next_row = (await db.execute(text(f'SELECT COALESCE(MAX(source_row_number), 0) + 1 FROM "{table_name}"'))).scalar_one()
|
||||
for c in columns:
|
||||
if c in SERIAL_COLUMNS:
|
||||
new[c] = str(next_row)
|
||||
names = ", ".join(["id", "source_row_number", "owner_user_id", *[f'"{c}"' for c in columns]])
|
||||
params = ", ".join([":id", ":source_row_number", ":owner_user_id", *[f":{c}" for c in columns]])
|
||||
row_id = uuid.uuid4()
|
||||
await db.execute(
|
||||
text(f'INSERT INTO "{table_name}" ({names}) VALUES ({params})'),
|
||||
{**new, "id": row_id, "source_row_number": next_row, "owner_user_id": current_user.id},
|
||||
)
|
||||
await _renumber_rows(table_name, db)
|
||||
await db.commit()
|
||||
return await _get_row(table_name, row_id, db)
|
||||
|
||||
|
||||
@router.patch("/{table_name}/rows/{row_id}")
|
||||
async def update_csv_row(table_name: str, row_id: uuid.UUID, payload: dict[str, object] = Body(...), db: AsyncSession = Depends(get_db), current_user: User = Depends(require_role(Role.MR, Role.ADMIN))) -> dict[str, object]:
|
||||
values = _clean_payload(table_name, payload)
|
||||
if table_name in PER_MR_PRODUCT_TABLES and current_user.role in FIELD_ROLES:
|
||||
shared = await _get_row(table_name, row_id, db)
|
||||
if await _stored_owner(table_name, row_id, db) is None:
|
||||
return await _start_own_row(table_name, shared, values, payload, db, current_user)
|
||||
row_owner = await _row_owner(table_name, row_id, current_user, db)
|
||||
values = await _apply_mr_identity(values, current_user, db)
|
||||
if table_name == "sales_projection":
|
||||
values = await _apply_sales_projection_product(values, db, current_user)
|
||||
changes = {column: value for column, value in values.items() if column in payload}
|
||||
stored = await _get_row(table_name, row_id, db)
|
||||
if table_name == "expense_statement" and any(column in payload for column in (*EXPENSE_AMOUNTS, "total")):
|
||||
merged = {c: (changes[c] if c in changes else stored.get(c)) for c in EXPENSE_AMOUNTS}
|
||||
changes["total"] = _apply_expense_total(merged)["total"]
|
||||
if not changes:
|
||||
raise ValidationError("At least one CSV column is required")
|
||||
if not _has_user_data(table_name, {**values, **changes}):
|
||||
# Judged on the whole row as it will be stored, not only the columns sent: clearing
|
||||
# one cell of a row that still has other values is a normal edit.
|
||||
if not _has_user_data(table_name, {**{c: stored.get(c) for c in _table(table_name)}, **changes}):
|
||||
raise ValidationError("A row must contain at least one value")
|
||||
changes = {column: value for column, value in changes.items() if column not in SERIAL_COLUMNS}
|
||||
if not changes:
|
||||
@@ -409,12 +600,56 @@ async def update_csv_row(table_name: str, row_id: uuid.UUID, payload: dict[str,
|
||||
|
||||
|
||||
@router.delete("/{table_name}/rows/{row_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_csv_row(table_name: str, row_id: uuid.UUID, db: AsyncSession = Depends(get_db), current_user: User = Depends(require_role(Role.ADMIN))) -> None:
|
||||
async def delete_csv_row(
|
||||
table_name: str,
|
||||
row_id: uuid.UUID,
|
||||
request: Request,
|
||||
confirm: str | None = Query(default=None, description="The product's name, to remove a product's last row"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN)),
|
||||
) -> None:
|
||||
"""Remove a row entered by mistake. An MR may remove only their own rows (checked by _row_owner).
|
||||
|
||||
On a shared product list, a product's last row takes the product off everyone's list: an MR
|
||||
may not remove it at all, and the administrator must name the product in `confirm`. Every
|
||||
removal is written to the audit log with the whole row, so it can be put back by hand."""
|
||||
_table(table_name)
|
||||
row_owner = await _row_owner(table_name, row_id, current_user, db)
|
||||
row = await _get_row(table_name, row_id, db)
|
||||
if table_name in PRODUCT_LIST_TABLES:
|
||||
product = str(row.get("product_name") or "").strip()
|
||||
copies = (
|
||||
await db.execute(
|
||||
text(
|
||||
f'SELECT COUNT(*) FROM "{table_name}" '
|
||||
"WHERE upper(btrim(COALESCE(product_name, ''))) = upper(:product)"
|
||||
+ (" AND owner_user_id IS NOT DISTINCT FROM :owner" if table_name in PER_MR_PRODUCT_TABLES else "")
|
||||
),
|
||||
{"product": product, **({"owner": row_owner} if table_name in PER_MR_PRODUCT_TABLES else {})},
|
||||
)
|
||||
).scalar_one()
|
||||
if copies < 2:
|
||||
if current_user.role in FIELD_ROLES:
|
||||
raise ForbiddenError(
|
||||
"This is the product's only row, so it stays in the list. Clear its figures instead.",
|
||||
{"table": table_name},
|
||||
)
|
||||
if (confirm or "").strip().upper() != product.upper():
|
||||
raise ValidationError(
|
||||
"Type the product's name to confirm removing it from everyone's list",
|
||||
{"product_name": product},
|
||||
)
|
||||
result = await db.execute(text(f'DELETE FROM "{table_name}" WHERE id = :id'), {"id": row_id})
|
||||
if result.rowcount == 0:
|
||||
raise NotFoundError("CSV row not found", {"id": str(row_id)})
|
||||
await AuditService(db).log(
|
||||
actor_user_id=current_user.id,
|
||||
entity_type=EntityType.FORM_ROW,
|
||||
entity_id=row_id,
|
||||
action=AuditAction.DELETE,
|
||||
before={"table": table_name, **{k: (str(v) if isinstance(v, uuid.UUID) else v) for k, v in row.items()}},
|
||||
ip_address=client_ip(request),
|
||||
)
|
||||
await _renumber_rows(table_name, db)
|
||||
await approvals.invalidate(db, table_name, row_owner)
|
||||
await db.commit()
|
||||
@@ -454,7 +689,7 @@ async def download_csv_row_pdf(
|
||||
table_name: str,
|
||||
row_id: uuid.UUID,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM)),
|
||||
) -> Response:
|
||||
_ensure_table_access(table_name, current_user)
|
||||
row_owner = await _row_owner(table_name, row_id, current_user, db)
|
||||
@@ -474,23 +709,28 @@ async def download_csv_table_pdfs(
|
||||
owner_id: uuid.UUID | None = None,
|
||||
month: str | None = None,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM)),
|
||||
) -> Response:
|
||||
"""The whole form, or only one month of it (for the forms that have a month)."""
|
||||
_ensure_table_access(table_name, current_user)
|
||||
columns = _table(table_name)
|
||||
rows = await _load_rows(table_name, db, current_user, owner_id, month)
|
||||
whole_range = table_name in PRODUCT_LIST_TABLES and bool(month and month.strip())
|
||||
if table_name in PER_MR_PRODUCT_TABLES and await _view_owner(table_name, db, current_user, owner_id) is None:
|
||||
whole_range = False # several MRs together: print the rows that exist, not one line per product
|
||||
rows = await _load_rows(table_name, db, current_user, owner_id, None if whole_range else month)
|
||||
if whole_range and month:
|
||||
rows = _month_of_every_product(rows, month, columns)
|
||||
if month and month.strip() and not rows:
|
||||
raise NotFoundError(f"There are no rows for {month.strip()} in this form", {"month": month})
|
||||
# The signature is that of the single MR whose rows these are; a PDF of several MRs is unsigned.
|
||||
subject = current_user.id if current_user.role == Role.MR else owner_id
|
||||
subject = current_user.id if current_user.role in FIELD_ROLES else owner_id
|
||||
approval_status, am_name, approved_at = await _signature(table_name, subject, db)
|
||||
from app.pdf.service import csv_table_pdf_bytes
|
||||
suffix = re.sub(r"[^a-z]", "", month.lower()) if month else ""
|
||||
filename = f"{table_name}-{suffix}.pdf" if suffix else f"{table_name}.pdf"
|
||||
# Printing every month together: a month-numbered table's stored Sl. No. (1, 2… 1, 2…)
|
||||
# would repeat, so it is recomputed as one running count over what is actually printed.
|
||||
renumber = table_name in MONTH_NUMBERED_TABLES and not (month and month.strip())
|
||||
renumber = table_name in MONTH_NUMBERED_TABLES and (not (month and month.strip()) or whole_range)
|
||||
pdf = csv_table_pdf_bytes(
|
||||
table_name,
|
||||
CSV_LABELS[table_name],
|
||||
|
||||
Reference in New Issue
Block a user