Complete ORM gaps + server features + module depth push

ORM:
- SQL Constraints support (Model.AddSQLConstraint, applied in InitDatabase)
- Translatable field Read (ir_translation lookup for non-en_US)
- active_test filter in SearchCount + ReadGroup (consistency with Search)
- Environment.Ref() improved (format validation, parameterized query)

Server:
- /web/action/run endpoint (server action execution stub)
- /web/model/get_definitions (field metadata for multiple models)
- Binary field serving rewritten: reads from DB, falls back to SVG
  with record initial (fixes avatar/logo rendering)

Business modules deepened:
- Account: action_post validation (partner, lines), sequence numbering
  (JOURNAL/YYYY/NNNN), action_register_payment, remove_move_reconcile
- Sale: action_cancel, action_draft, action_view_invoice
- Purchase: button_draft
- Stock: action_cancel on picking
- CRM: action_set_won_rainbowman, convert_opportunity
- HR: hr.contract model (employee, wage, dates, state)
- Project: action_blocked, task stage seed data

Views:
- Cancel/Reset buttons in sale.form header
- Register Payment button in invoice.form (visible when posted+unpaid)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc
2026-04-03 01:03:47 +02:00
parent cc1f150732
commit 24dee3704a
9 changed files with 255 additions and 46 deletions

View File

@@ -3,6 +3,8 @@ package orm
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
@@ -76,13 +78,23 @@ func (env *Environment) Model(name string) *Recordset {
// Ref returns a record by its XML ID (external identifier).
// Mirrors: self.env.ref('module.xml_id')
func (env *Environment) Ref(xmlID string) (*Recordset, error) {
// Try direct integer ID (for programmatic use) — reject since model is unknown
if id, err := strconv.ParseInt(xmlID, 10, 64); err == nil {
return nil, fmt.Errorf("orm: ref requires module.name format, not bare ID %d", id)
}
// Query ir_model_data for the external ID
var resModel string
var resID int64
parts := strings.SplitN(xmlID, ".", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("orm: ref %q must be module.name format", xmlID)
}
err := env.tx.QueryRow(env.ctx,
`SELECT model, res_id FROM ir_model_data WHERE module || '.' || name = $1 LIMIT 1`,
xmlID,
`SELECT model, res_id FROM ir_model_data WHERE module = $1 AND name = $2 LIMIT 1`,
parts[0], parts[1],
).Scan(&resModel, &resID)
if err != nil {
return nil, fmt.Errorf("orm: ref %q not found: %w", xmlID, err)

View File

@@ -50,6 +50,9 @@ type Model struct {
Constraints []ConstraintFunc // Validation constraints
Methods map[string]MethodFunc // Named business methods
// SQL-level constraints
SQLConstraints []SQLConstraint
// Computed fields
computes map[string]ComputeFunc // field_name → compute function
dependencyMap map[string][]string // trigger_field → []computed_field_names
@@ -216,6 +219,19 @@ func (m *Model) IsAbstract() bool { return m.modelType == ModelAbstract }
// IsTransient returns true if this is a wizard/temporary model.
func (m *Model) IsTransient() bool { return m.modelType == ModelTransient }
// SQLConstraint represents a SQL-level constraint on a model's table.
// Mirrors: _sql_constraints in Odoo.
type SQLConstraint struct {
Name string // Constraint name
Definition string // SQL definition (e.g., "UNIQUE(name, company_id)")
Message string // Error message
}
// AddSQLConstraint registers a SQL-level constraint on this model.
func (m *Model) AddSQLConstraint(name, definition, message string) {
m.SQLConstraints = append(m.SQLConstraints, SQLConstraint{Name: name, Definition: definition, Message: message})
}
// ConstraintFunc validates a recordset. Returns error if constraint violated.
// Mirrors: @api.constrains in Odoo.
type ConstraintFunc func(rs *Recordset) error

View File

@@ -46,6 +46,25 @@ func (rs *Recordset) ReadGroup(domain Domain, groupby []string, aggregates []str
opt = opts[0]
}
// Auto-filter archived records unless active_test=false in context
// Mirrors: odoo/orm/models.py BaseModel._where_calc()
if activeField := m.GetField("active"); activeField != nil {
activeTest := true
if v, ok := rs.env.context["active_test"]; ok {
if b, ok := v.(bool); ok {
activeTest = b
}
}
if activeTest {
activeLeaf := Leaf("active", "=", true)
if len(domain) == 0 {
domain = Domain{activeLeaf}
} else {
domain = append(Domain{OpAnd}, append(domain, activeLeaf)...)
}
}
}
// Apply record rules
domain = ApplyRecordRules(rs.env, m, domain)

View File

@@ -591,6 +591,33 @@ func (rs *Recordset) Read(fields []string) ([]Values, error) {
}
}
// Post-fetch: translations for non-English languages
// Mirrors: odoo/orm/models.py BaseModel._read() translation lookup
if rs.env.Lang() != "en_US" && rs.env.Lang() != "" {
for _, fname := range storedFields {
f := m.GetField(fname)
if f == nil || !f.Translate {
continue
}
for _, rec := range results {
srcVal, _ := rec[fname].(string)
if srcVal == "" {
continue
}
var translated string
err := rs.env.tx.QueryRow(rs.env.ctx,
`SELECT value FROM ir_translation
WHERE lang = $1 AND src = $2 AND value != ''
LIMIT 1`,
rs.env.Lang(), srcVal,
).Scan(&translated)
if err == nil && translated != "" {
rec[fname] = translated
}
}
}
}
// Post-fetch: M2M fields (from junction tables)
if len(m2mFields) > 0 && len(rs.ids) > 0 {
for _, fname := range m2mFields {
@@ -779,6 +806,25 @@ func (rs *Recordset) Search(domain Domain, opts ...SearchOpts) (*Recordset, erro
func (rs *Recordset) SearchCount(domain Domain) (int64, error) {
m := rs.model
// Auto-filter archived records unless active_test=false in context
// Mirrors: odoo/orm/models.py BaseModel._where_calc()
if activeField := m.GetField("active"); activeField != nil {
activeTest := true
if v, ok := rs.env.context["active_test"]; ok {
if b, ok := v.(bool); ok {
activeTest = b
}
}
if activeTest {
activeLeaf := Leaf("active", "=", true)
if len(domain) == 0 {
domain = Domain{activeLeaf}
} else {
domain = append(Domain{OpAnd}, append(domain, activeLeaf)...)
}
}
}
compiler := &DomainCompiler{model: m, env: rs.env}
where, params, err := compiler.Compile(domain)
if err != nil {