lambda-erp 1.4.18__tar.gz → 1.4.19__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/PKG-INFO +1 -1
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/chat.py +5 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/main.py +4 -0
- lambda_erp-1.4.19/api/record_attachments.py +230 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/masters.py +2 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/frontend/src/api/client.ts +24 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/database.py +27 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/pyproject.toml +1 -1
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/.gitignore +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/LICENSE +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/README.md +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/attachments.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/auth.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/bootstrap.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/chat_links.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/demo_limits.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/deps.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/erp_context.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/errors.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/list_values.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/llm_provider.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/mcp_oauth.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/mentions.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/menu_layout.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/oauth.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/pdf.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/pdf_contract.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/pdf_exports.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/pdf_fonts.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/pdf_pages.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/pdf_profiles.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/pdf_templates.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/pricing_actions.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/providers.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/record_notes.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/remarks_md.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/accounting.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/actions.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/admin.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/analytics.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/availability.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/bank_reconciliation.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/bank_statements.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/chat_api.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/documents.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/mcp.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/pdf_templates.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/proposals.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/reports.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/routers/setup.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/services.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/stock_actions.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/templates/_proposal_totals.html +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/templates/_record_base.html +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/templates/delivery_note.html +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/templates/document.html +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/templates/proposal.html +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/templates/record.html +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/templates/reservation.html +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/time_filters.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/tool_permissions.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/api/write_context.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/docs/agents/README.md +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/frontend/LICENSE +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/frontend/README.md +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/bank_account.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/bank_reconciliation.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/bank_statement_import.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/bank_transaction.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/budget.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/camt.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/chart_of_accounts.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/general_ledger.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/journal_entry.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/payment_entry.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/pos_invoice.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/purchase_invoice.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/revaluation.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/sales_invoice.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/settlement.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/engine.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/pack.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/packs/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/packs/ch.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/packs/de_common.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/packs/de_skr03.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/packs/de_skr04.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/packs/generic.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/profiles.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/setup/spine.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/accounting/subscription.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/addresses.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/assets/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/assets/asset.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/assets/lifecycle.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/assets/reservation.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/backlog.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/bundles.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/buying/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/buying/purchase_order.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/controllers/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/controllers/currency.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/controllers/defaults.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/controllers/item_price.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/controllers/item_prices.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/controllers/price_resolution.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/controllers/pricing_rule.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/controllers/taxes_and_totals.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/exceptions.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/hooks.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/master_integrity.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/model.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/permissions.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/selling/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/selling/proposal.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/selling/quotation.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/selling/sales_order.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/simulation.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/stock/__init__.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/stock/consignment.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/stock/delivery_note.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/stock/purchase_receipt.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/stock/reservation.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/stock/serial.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/stock/stock_count.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/stock/stock_entry.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/stock/stock_ledger.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/timestamps.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/utils.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/validation.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/lambda_erp/workflow.py +0 -0
- {lambda_erp-1.4.18 → lambda_erp-1.4.19}/terraform/README.md +0 -0
|
@@ -3034,6 +3034,11 @@ For a complete sales return (financial + stock): create and submit both a Credit
|
|
|
3034
3034
|
- Never invent or guess a serial number. If the user has not given them, ask; to see what is in stock use search_masters master_type="serial-no" (filter item_code, warehouse) or the serial-trace report for one number.
|
|
3035
3035
|
- Units from before the item was serial-tracked have no number: give them one with a Stock Entry of type "Serial Assignment" (item_code, qty, t_warehouse, serial_no).
|
|
3036
3036
|
|
|
3037
|
+
### Attachments
|
|
3038
|
+
- Any document or master record can keep files (the customer's order e-mail or PDF, a signed slip, a certificate). They never change the record and are never printed. See what is there with the `list_record_attachments` action.
|
|
3039
|
+
- When the user uploads a file to the conversation and a record is created from it or it belongs to one, keep it with that record using `attach_chat_file` (record_type = the type slug, record_id = the record's name, attachment_id = the uploaded file's id). Do this for a customer's order that became a sales order without being asked twice; otherwise only when the user asks.
|
|
3040
|
+
- A Delivery Note has `tracking_reference` for the carrier's consignment number.
|
|
3041
|
+
|
|
3037
3042
|
### Item bundles
|
|
3038
3043
|
- An **Item Bundle** (doctype `item-bundle`: item_code = the bundle, items = rows of item_code and qty per bundle) makes one item code stand for several items. The bundle's own item is not a stock item.
|
|
3039
3044
|
- On quotations, sales orders, delivery notes and sales / POS invoices, put the bundle's item_code and qty on a line and save: the line is replaced by one line per item of the bundle, which are then ordinary lines. Do not give a bundle line a rate, discount or batch; do not expand a bundle by hand.
|
|
@@ -66,6 +66,8 @@ async def lifespan(app: FastAPI):
|
|
|
66
66
|
initialize_mentions()
|
|
67
67
|
from api.record_notes import initialize_record_notes
|
|
68
68
|
initialize_record_notes()
|
|
69
|
+
from api.record_attachments import initialize_record_attachments
|
|
70
|
+
initialize_record_attachments()
|
|
69
71
|
from api.stock_actions import initialize_stock_actions
|
|
70
72
|
initialize_stock_actions()
|
|
71
73
|
from api.pricing_actions import initialize_pricing_actions
|
|
@@ -155,6 +157,8 @@ app.include_router(auth_router, prefix="/api")
|
|
|
155
157
|
app.include_router(oauth_router, prefix="/api")
|
|
156
158
|
app.include_router(mcp_oauth_router)
|
|
157
159
|
app.include_router(attachments_router, prefix="/api")
|
|
160
|
+
from api.record_attachments import router as record_attachments_router
|
|
161
|
+
app.include_router(record_attachments_router, prefix="/api")
|
|
158
162
|
app.include_router(documents.router, prefix="/api")
|
|
159
163
|
app.include_router(proposals.router, prefix="/api")
|
|
160
164
|
app.include_router(pdf_templates_router.router, prefix="/api")
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"""Files kept with a document or master record.
|
|
2
|
+
|
|
3
|
+
The customer's order as it arrived, a signed delivery slip, a certificate:
|
|
4
|
+
kept next to the record they belong to instead of in a folder. An attachment
|
|
5
|
+
never changes the record (no `modified` bump, allowed on submitted documents
|
|
6
|
+
too) and is never printed with it.
|
|
7
|
+
|
|
8
|
+
Whoever may read the record may list and download its files; adding and
|
|
9
|
+
removing them needs the manager role. The demo user gets neither.
|
|
10
|
+
|
|
11
|
+
REST:
|
|
12
|
+
GET /api/record-attachments?record_type=sales-order&record_id=SO-0001
|
|
13
|
+
POST /api/record-attachments (multipart: record_type, record_id, file, description)
|
|
14
|
+
GET /api/record-attachments/{id} (the file)
|
|
15
|
+
DELETE /api/record-attachments/{id}
|
|
16
|
+
|
|
17
|
+
Chat / MCP: actions list_record_attachments and attach_chat_file (keeps a
|
|
18
|
+
file that was uploaded to the conversation with a record).
|
|
19
|
+
|
|
20
|
+
See docs/attachments.md.
|
|
21
|
+
"""
|
|
22
|
+
import hashlib
|
|
23
|
+
import os
|
|
24
|
+
import re
|
|
25
|
+
import uuid
|
|
26
|
+
|
|
27
|
+
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
|
28
|
+
from fastapi.responses import Response
|
|
29
|
+
from pydantic import Field
|
|
30
|
+
|
|
31
|
+
from api import mentions, services
|
|
32
|
+
from api.auth import get_current_user
|
|
33
|
+
from lambda_erp.database import get_db
|
|
34
|
+
from lambda_erp.utils import now
|
|
35
|
+
|
|
36
|
+
TABLE = "Record Attachment"
|
|
37
|
+
MAX_SIZE = 15 * 1024 * 1024
|
|
38
|
+
MAX_PER_RECORD = 50
|
|
39
|
+
# What may be kept: documents, images, mail, spreadsheets. Nothing a browser
|
|
40
|
+
# would run; files are always sent back as downloads.
|
|
41
|
+
ALLOWED = {
|
|
42
|
+
".pdf": "application/pdf",
|
|
43
|
+
".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp",
|
|
44
|
+
".tif": "image/tiff", ".tiff": "image/tiff",
|
|
45
|
+
".eml": "message/rfc822", ".msg": "application/vnd.ms-outlook",
|
|
46
|
+
".txt": "text/plain", ".csv": "text/csv",
|
|
47
|
+
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
48
|
+
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
49
|
+
".xml": "application/xml",
|
|
50
|
+
}
|
|
51
|
+
_UNSAFE = re.compile(r"[^\w.\- ()\[\]]+", re.UNICODE)
|
|
52
|
+
|
|
53
|
+
router = APIRouter(prefix="/record-attachments", tags=["attachments"])
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _target(record_type):
|
|
57
|
+
doctype = services.SLUG_TO_DOCTYPE.get(record_type)
|
|
58
|
+
if doctype:
|
|
59
|
+
return doctype
|
|
60
|
+
master = services.MASTER_TABLES.get(record_type)
|
|
61
|
+
return master[0] if master else None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _require_record(record_type, record_id, user, write=False):
|
|
65
|
+
"""The record exists and the caller may read it (and, to write, is at
|
|
66
|
+
least a manager). Refusals do not say whether the record exists."""
|
|
67
|
+
from lambda_erp import permissions
|
|
68
|
+
from lambda_erp.exceptions import PermissionDenied
|
|
69
|
+
|
|
70
|
+
table = _target(record_type)
|
|
71
|
+
role = (user or {}).get("role")
|
|
72
|
+
if not table or role not in mentions.REAL_ROLES or not get_db().exists(table, record_id):
|
|
73
|
+
raise HTTPException(404, "Record is unavailable")
|
|
74
|
+
try:
|
|
75
|
+
with permissions.acting_as(user):
|
|
76
|
+
permissions.require_readable(table, record_id)
|
|
77
|
+
except PermissionDenied:
|
|
78
|
+
raise HTTPException(404, "Record is unavailable")
|
|
79
|
+
if write and role not in ("manager", "admin"):
|
|
80
|
+
raise HTTPException(403, "Adding or removing attachments requires the manager role")
|
|
81
|
+
return table
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def safe_filename(filename):
|
|
85
|
+
name = os.path.basename(str(filename or "").replace("\\", "/")).strip()
|
|
86
|
+
name = _UNSAFE.sub("_", name).strip(". ")[:150]
|
|
87
|
+
return name or "attachment"
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _public(row):
|
|
91
|
+
return {"id": row["name"], "record_type": row["record_type"], "record_id": row["record_id"],
|
|
92
|
+
"filename": row["filename"], "content_type": row["content_type"], "size": row["size"],
|
|
93
|
+
"description": row.get("description"), "uploaded_by": row.get("uploaded_by_name") or row.get("uploaded_by"),
|
|
94
|
+
"created_at": row.get("creation"), "url": f"/api/record-attachments/{row['name']}"}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def list_attachments(record_type, record_id, user):
|
|
98
|
+
_require_record(record_type, record_id, user)
|
|
99
|
+
rows = get_db().sql(
|
|
100
|
+
f'SELECT name, record_type, record_id, filename, content_type, size, description, uploaded_by, '
|
|
101
|
+
f'uploaded_by_name, creation FROM "{TABLE}" WHERE record_type = ? AND record_id = ? ORDER BY creation, name',
|
|
102
|
+
[record_type, record_id])
|
|
103
|
+
return {"attachments": [_public(row) for row in rows]}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def add_attachment(record_type, record_id, filename, data, user, description=None):
|
|
107
|
+
"""Keep a file with a record. Returns its public description."""
|
|
108
|
+
_require_record(record_type, record_id, user, write=True)
|
|
109
|
+
filename = safe_filename(filename)
|
|
110
|
+
extension = os.path.splitext(filename)[1].lower()
|
|
111
|
+
if extension not in ALLOWED:
|
|
112
|
+
raise HTTPException(415, f"{extension or 'This file type'} cannot be attached. "
|
|
113
|
+
f"Allowed: {', '.join(sorted(ALLOWED))}")
|
|
114
|
+
if not data:
|
|
115
|
+
raise HTTPException(400, "The file is empty")
|
|
116
|
+
if len(data) > MAX_SIZE:
|
|
117
|
+
raise HTTPException(413, f"The file is too large (at most {MAX_SIZE // (1024 * 1024)} MB)")
|
|
118
|
+
db = get_db()
|
|
119
|
+
count = db.sql(f'SELECT COUNT(*) AS n FROM "{TABLE}" WHERE record_type = ? AND record_id = ?',
|
|
120
|
+
[record_type, record_id])[0]["n"]
|
|
121
|
+
if count >= MAX_PER_RECORD:
|
|
122
|
+
raise HTTPException(409, f"A record holds at most {MAX_PER_RECORD} attachments")
|
|
123
|
+
name = uuid.uuid4().hex
|
|
124
|
+
db.sql(
|
|
125
|
+
f'INSERT INTO "{TABLE}" (name, record_type, record_id, filename, content_type, size, sha256, data, '
|
|
126
|
+
f'description, uploaded_by, uploaded_by_name, creation) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
127
|
+
[name, record_type, record_id, filename, ALLOWED[extension], len(data), hashlib.sha256(data).hexdigest(),
|
|
128
|
+
data, (str(description).strip()[:500] or None) if description else None,
|
|
129
|
+
user.get("user_id") or user.get("name"), user.get("full_name") or user.get("email"), now()])
|
|
130
|
+
db.commit()
|
|
131
|
+
row = db.sql(f'SELECT name, record_type, record_id, filename, content_type, size, description, uploaded_by, '
|
|
132
|
+
f'uploaded_by_name, creation FROM "{TABLE}" WHERE name = ?', [name])[0]
|
|
133
|
+
return _public(row)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _load(attachment_id, user, write=False):
|
|
137
|
+
rows = get_db().sql(f'SELECT * FROM "{TABLE}" WHERE name = ?', [attachment_id])
|
|
138
|
+
if not rows:
|
|
139
|
+
raise HTTPException(404, "Attachment not found")
|
|
140
|
+
_require_record(rows[0]["record_type"], rows[0]["record_id"], user, write=write)
|
|
141
|
+
return rows[0]
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def delete_attachments_for(record_type, record_id):
|
|
145
|
+
"""Drop a hard-deleted record's files."""
|
|
146
|
+
get_db().sql(f'DELETE FROM "{TABLE}" WHERE record_type = ? AND record_id = ?', [record_type, record_id])
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
# -- REST ---------------------------------------------------------------------
|
|
150
|
+
|
|
151
|
+
@router.get("")
|
|
152
|
+
def list_route(record_type: str, record_id: str, user: dict = Depends(get_current_user)):
|
|
153
|
+
return list_attachments(record_type, record_id, user)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@router.post("")
|
|
157
|
+
async def upload_route(record_type: str = Form(...), record_id: str = Form(...), file: UploadFile = File(...),
|
|
158
|
+
description: str | None = Form(None), user: dict = Depends(get_current_user)):
|
|
159
|
+
# Check access before reading the body into memory.
|
|
160
|
+
_require_record(record_type, record_id, user, write=True)
|
|
161
|
+
data = await file.read(MAX_SIZE + 1)
|
|
162
|
+
return add_attachment(record_type, record_id, file.filename, data, user, description)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
@router.get("/{attachment_id}")
|
|
166
|
+
def download_route(attachment_id: str, user: dict = Depends(get_current_user)):
|
|
167
|
+
row = _load(attachment_id, user)
|
|
168
|
+
data = row["data"]
|
|
169
|
+
if isinstance(data, memoryview):
|
|
170
|
+
data = data.tobytes()
|
|
171
|
+
ascii_name = row["filename"].encode("ascii", "replace").decode().replace('"', "_")
|
|
172
|
+
return Response(content=bytes(data), media_type=row["content_type"], headers={
|
|
173
|
+
# Always a download, never rendered in the application's origin.
|
|
174
|
+
"Content-Disposition": f'attachment; filename="{ascii_name}"',
|
|
175
|
+
"X-Content-Type-Options": "nosniff",
|
|
176
|
+
"Cache-Control": "private, no-store",
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@router.delete("/{attachment_id}")
|
|
181
|
+
def delete_route(attachment_id: str, user: dict = Depends(get_current_user)):
|
|
182
|
+
row = _load(attachment_id, user, write=True)
|
|
183
|
+
db = get_db()
|
|
184
|
+
db.sql(f'DELETE FROM "{TABLE}" WHERE name = ?', [row["name"]])
|
|
185
|
+
db.commit()
|
|
186
|
+
return {"ok": True}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
# -- chat / MCP ---------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
class ListAttachments(mentions.Input):
|
|
192
|
+
record_type: str = Field(min_length=1, max_length=100)
|
|
193
|
+
record_id: str = Field(min_length=1, max_length=200)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
class AttachChatFile(mentions.Input):
|
|
197
|
+
record_type: str = Field(min_length=1, max_length=100)
|
|
198
|
+
record_id: str = Field(min_length=1, max_length=200)
|
|
199
|
+
attachment_id: str = Field(min_length=1, max_length=100)
|
|
200
|
+
description: str | None = Field(default=None, max_length=500)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _list_action(args, user):
|
|
204
|
+
data = ListAttachments.model_validate(args)
|
|
205
|
+
return list_attachments(data.record_type, data.record_id, user)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _attach_chat_file(args, user):
|
|
209
|
+
data = AttachChatFile.model_validate(args)
|
|
210
|
+
from api.attachments import get_attachments_by_ids
|
|
211
|
+
found = get_attachments_by_ids([data.attachment_id], user.get("user_id") or user.get("name"))
|
|
212
|
+
if not found:
|
|
213
|
+
raise HTTPException(404, "That file was not uploaded to one of your conversations")
|
|
214
|
+
return add_attachment(data.record_type, data.record_id, found[0]["filename"], found[0]["data"], user,
|
|
215
|
+
data.description)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def initialize_record_attachments():
|
|
219
|
+
services.register_action(
|
|
220
|
+
"list_record_attachments", _list_action, minimum_role="viewer", pass_user=True,
|
|
221
|
+
parameters=ListAttachments.model_json_schema(),
|
|
222
|
+
description="List the files kept with one document or master record (name, type, size, who added it). "
|
|
223
|
+
"record_type is the document or master type slug (e.g. sales-order, item) and record_id its "
|
|
224
|
+
"name. The files themselves are downloaded in the application.")
|
|
225
|
+
services.register_action(
|
|
226
|
+
"attach_chat_file", _attach_chat_file, minimum_role="manager", pass_user=True,
|
|
227
|
+
parameters=AttachChatFile.model_json_schema(),
|
|
228
|
+
description="Keep a file the user uploaded to this conversation with a document or master record, e.g. "
|
|
229
|
+
"the customer's order e-mail or PDF with the sales order made from it. attachment_id is the "
|
|
230
|
+
"id of the uploaded file. It does not change the record and is never printed.")
|
|
@@ -908,8 +908,10 @@ def delete_master_record(master_type: str, name: str) -> dict:
|
|
|
908
908
|
raise HTTPException(status_code=409, detail=f"Cannot delete {doctype} {name}: referenced by {reference}")
|
|
909
909
|
|
|
910
910
|
from api.record_notes import delete_record_notes_for
|
|
911
|
+
from api.record_attachments import delete_attachments_for
|
|
911
912
|
with db.atomic():
|
|
912
913
|
delete_record_notes_for(master_type, name)
|
|
914
|
+
delete_attachments_for(master_type, name)
|
|
913
915
|
db.delete(doctype, name=name)
|
|
914
916
|
return {"ok": True, "status": "deleted"}
|
|
915
917
|
|
|
@@ -260,6 +260,16 @@ export interface FleetCalendarFeed {
|
|
|
260
260
|
|
|
261
261
|
/** Narrows what a user's role allows: hidden classes of sensitive data and,
|
|
262
262
|
* per document or master type, the actions that remain. */
|
|
263
|
+
export interface RecordAttachment {
|
|
264
|
+
id: string;
|
|
265
|
+
filename: string;
|
|
266
|
+
content_type: string;
|
|
267
|
+
size: number;
|
|
268
|
+
description?: string | null;
|
|
269
|
+
uploaded_by?: string | null;
|
|
270
|
+
created_at?: string | null;
|
|
271
|
+
}
|
|
272
|
+
|
|
263
273
|
export interface AccessProfile {
|
|
264
274
|
name: string;
|
|
265
275
|
description?: string;
|
|
@@ -807,6 +817,20 @@ export const api = {
|
|
|
807
817
|
),
|
|
808
818
|
authDeleteAccessProfile: (name: string) =>
|
|
809
819
|
request<any>(`/auth/access-profiles/${encodeURIComponent(name)}`, { method: "DELETE" }),
|
|
820
|
+
// Files kept with a document or master record.
|
|
821
|
+
listRecordAttachments: (recordType: string, recordId: string) =>
|
|
822
|
+
request<{ attachments: RecordAttachment[] }>(`/record-attachments${qs({ record_type: recordType, record_id: recordId })}`),
|
|
823
|
+
uploadRecordAttachment: (recordType: string, recordId: string, file: File, description?: string) => {
|
|
824
|
+
const form = new FormData();
|
|
825
|
+
form.append("record_type", recordType);
|
|
826
|
+
form.append("record_id", recordId);
|
|
827
|
+
form.append("file", file);
|
|
828
|
+
if (description) form.append("description", description);
|
|
829
|
+
return multipartRequest<RecordAttachment>("/record-attachments", form);
|
|
830
|
+
},
|
|
831
|
+
deleteRecordAttachment: (id: string) =>
|
|
832
|
+
request<any>(`/record-attachments/${encodeURIComponent(id)}`, { method: "DELETE" }),
|
|
833
|
+
recordAttachmentUrl: (id: string) => `${BASE}/record-attachments/${encodeURIComponent(id)}`,
|
|
810
834
|
authAssignRecordScope: (userName: string, record_scope: Record<string, string[]> | null) =>
|
|
811
835
|
request<any>(`/auth/users/${encodeURIComponent(userName)}/record-scope`, {
|
|
812
836
|
method: "PUT", body: JSON.stringify({ record_scope }),
|
|
@@ -3661,6 +3661,32 @@ def _m064_item_bundles(db: "Database") -> None:
|
|
|
3661
3661
|
db.ensure_column(table, "bundle_qty", "REAL")
|
|
3662
3662
|
|
|
3663
3663
|
|
|
3664
|
+
def _m065_record_attachments(db: "Database") -> None:
|
|
3665
|
+
"""Files kept with a document or master record, and a tracking reference
|
|
3666
|
+
on delivery notes (see docs/attachments.md)."""
|
|
3667
|
+
db.conn.execute(db._ddl(
|
|
3668
|
+
"""CREATE TABLE IF NOT EXISTS "Record Attachment" (
|
|
3669
|
+
name TEXT PRIMARY KEY,
|
|
3670
|
+
record_type TEXT NOT NULL,
|
|
3671
|
+
record_id TEXT NOT NULL,
|
|
3672
|
+
filename TEXT NOT NULL,
|
|
3673
|
+
content_type TEXT NOT NULL,
|
|
3674
|
+
size INTEGER NOT NULL,
|
|
3675
|
+
sha256 TEXT,
|
|
3676
|
+
data BLOB NOT NULL,
|
|
3677
|
+
description TEXT,
|
|
3678
|
+
uploaded_by TEXT,
|
|
3679
|
+
uploaded_by_name TEXT,
|
|
3680
|
+
creation TEXT
|
|
3681
|
+
)"""))
|
|
3682
|
+
db.conn.execute(db._ddl(
|
|
3683
|
+
'CREATE INDEX IF NOT EXISTS "ix_record_attachment_record" ON "Record Attachment" (record_type, record_id)'))
|
|
3684
|
+
db.conn.commit()
|
|
3685
|
+
db._col_cache.pop("Record Attachment", None)
|
|
3686
|
+
db._text_col_cache.pop("Record Attachment", None)
|
|
3687
|
+
db.ensure_column("Delivery Note", "tracking_reference", "TEXT")
|
|
3688
|
+
|
|
3689
|
+
|
|
3664
3690
|
def _m052_access_profiles(db: "Database") -> None:
|
|
3665
3691
|
"""Access profiles narrow what a user's role allows (lambda_erp.permissions).
|
|
3666
3692
|
No profile is created and no user is assigned one: nothing changes for
|
|
@@ -3765,6 +3791,7 @@ Database.MIGRATIONS = [
|
|
|
3765
3791
|
(62, "stock_document_pricing", _m062_stock_document_pricing),
|
|
3766
3792
|
(63, "addresses", _m063_addresses),
|
|
3767
3793
|
(64, "item_bundles", _m064_item_bundles),
|
|
3794
|
+
(65, "record_attachments", _m065_record_attachments),
|
|
3768
3795
|
]
|
|
3769
3796
|
|
|
3770
3797
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|