Fixed Form Vlidation
This commit is contained in:
@@ -30,7 +30,7 @@ class Settings(BaseSettings):
|
||||
# SECRET_KEY MUST be overridden in production.
|
||||
SECRET_KEY: str = "change-me-in-production-this-is-not-a-secret"
|
||||
JWT_ALGORITHM: str = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 15
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 45
|
||||
REFRESH_TOKEN_EXPIRE_DAYS: int = 7
|
||||
LOGIN_MAX_FAILED_ATTEMPTS: int = 10
|
||||
|
||||
|
||||
+17
-2
@@ -59,10 +59,21 @@ async def _reject_repeat(
|
||||
raise ConflictError("An identical row already exists in this form")
|
||||
|
||||
|
||||
#: 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"}
|
||||
|
||||
|
||||
async def _renumber_rows(table_name: str, db: AsyncSession) -> None:
|
||||
columns = _table(table_name)
|
||||
# An MR's own form is numbered 1, 2, 3… whoever else has rows in the same table.
|
||||
partition = "PARTITION BY owner_user_id " if table_name in OWNED_TABLES else ""
|
||||
if table_name in OWNED_TABLES:
|
||||
partition = "PARTITION BY owner_user_id "
|
||||
elif table_name in MONTH_NUMBERED_TABLES:
|
||||
partition = "PARTITION BY lower(btrim(COALESCE(\"month\", ''))) "
|
||||
else:
|
||||
partition = ""
|
||||
serial_updates = ""
|
||||
for column in columns:
|
||||
if column in SERIAL_COLUMNS:
|
||||
@@ -398,7 +409,7 @@ 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.MR, Role.ADMIN))) -> None:
|
||||
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:
|
||||
_table(table_name)
|
||||
row_owner = await _row_owner(table_name, row_id, current_user, db)
|
||||
result = await db.execute(text(f'DELETE FROM "{table_name}" WHERE id = :id'), {"id": row_id})
|
||||
@@ -477,6 +488,9 @@ async def download_csv_table_pdfs(
|
||||
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())
|
||||
pdf = csv_table_pdf_bytes(
|
||||
table_name,
|
||||
CSV_LABELS[table_name],
|
||||
@@ -485,6 +499,7 @@ async def download_csv_table_pdfs(
|
||||
approval_status,
|
||||
am_name,
|
||||
approved_at=approved_at,
|
||||
renumber=renumber,
|
||||
)
|
||||
return Response(
|
||||
pdf,
|
||||
|
||||
+24
-4
@@ -167,7 +167,9 @@ FORMS: dict[str, FormSpec] = {
|
||||
Col("VALUE", "value", 9.6, "center"),
|
||||
Col("CLOSING", "closing", 11.6, "center"),
|
||||
Col("SEC. VALUE", "secondary_value", 10.1, "center"),
|
||||
Col("AUG. SALES", "aug_sales", 9.6, "center"),
|
||||
# Header text is filled in per report from the "month" column — see
|
||||
# _sales_projection_columns — never a hardcoded month.
|
||||
Col("SALES", "aug_sales", 9.6, "center"),
|
||||
Col("VALUE", "value_2", 9.5, "center"),
|
||||
),
|
||||
min_rows=26,
|
||||
@@ -252,6 +254,14 @@ def _has(row: Row, fields: tuple[str, ...]) -> bool:
|
||||
return any(_text(row.get(field)) for field in fields)
|
||||
|
||||
|
||||
def _sales_projection_columns(cols: tuple[Col, ...], rows: list[Row]) -> tuple[Col, ...]:
|
||||
"""The printed form's "<Month>. Sales" column: the month it prints is whichever
|
||||
month the rows were actually filled in for, never a fixed month."""
|
||||
month = _distinct(rows, "month")
|
||||
header = f"{month[:3].upper()}. SALES" if month else "SALES"
|
||||
return tuple(Col(header, c.field, c.width, c.align) if c.field == "aug_sales" else c for c in cols)
|
||||
|
||||
|
||||
def _grid(
|
||||
cols: tuple[Col, ...],
|
||||
rows: list[Row],
|
||||
@@ -315,8 +325,15 @@ def build_form_context(
|
||||
approval_status: str | None = None,
|
||||
am_name: str | None = None,
|
||||
approved_at: str | None = None,
|
||||
renumber: bool = False,
|
||||
) -> dict[str, object]:
|
||||
"""Everything a form template needs, for all stored rows of ``table_name``."""
|
||||
"""Everything a form template needs, for all stored rows of ``table_name``.
|
||||
|
||||
``renumber``: for a table whose stored Sl. No. restarts at 1 each month (see
|
||||
MONTH_NUMBERED_TABLES in app/csv_data/router.py), the caller sets this when the rows
|
||||
span more than one month — e.g. every month printed together — so the stored numbers
|
||||
(1, 2… 1, 2…) are ignored in favour of one plain running count over what is printed.
|
||||
"""
|
||||
spec = FORMS.get(table_name)
|
||||
grids: dict[str, dict[str, object]] = {}
|
||||
extra: dict[str, object] = {}
|
||||
@@ -360,11 +377,14 @@ def build_form_context(
|
||||
for r in rows or [{}]
|
||||
]
|
||||
else:
|
||||
columns = spec.columns
|
||||
if table_name == "sales_projection":
|
||||
columns = _sales_projection_columns(columns, rows)
|
||||
grids["main"] = _grid(
|
||||
spec.columns,
|
||||
columns,
|
||||
rows,
|
||||
min_rows=spec.min_rows,
|
||||
serial_field=spec.serial_field,
|
||||
serial_field=None if renumber else spec.serial_field,
|
||||
serial_dot=spec.serial_dot,
|
||||
table_width=spec.table_width,
|
||||
)
|
||||
|
||||
+2
-1
@@ -155,10 +155,11 @@ def csv_table_pdf_bytes(
|
||||
am_name: str | None = None,
|
||||
am_role: str = "Area Manager",
|
||||
approved_at: str | None = None,
|
||||
renumber: bool = False,
|
||||
) -> bytes:
|
||||
"""Render every stored row of an imported table on its own company-form layout."""
|
||||
context = build_form_context(
|
||||
table_name, table_label, columns, rows, approval_status, am_name, approved_at
|
||||
table_name, table_label, columns, rows, approval_status, am_name, approved_at, renumber
|
||||
)
|
||||
html = _env.get_template(f"forms/{context['template']}.html").render(**context)
|
||||
if WEASYPRINT_AVAILABLE:
|
||||
|
||||
Reference in New Issue
Block a user