Backend improvements: views, fields_get, session, RPC stubs
- Improved auto-generated list/form/search views with priority fields, two-column form layout, statusbar widget, notebook for O2M fields - Enhanced fields_get with currency_field, compute, related metadata - Fixed session handling: handleSessionInfo/handleSessionCheck use real session from cookie instead of hardcoded values - Added read_progress_bar and activity_format RPC stubs - Improved bootstrap translations with lang_parameters - Added "contacts" to session modules list Server starts successfully: 14 modules, 93 models, 378 XML templates, 503 JS modules transpiled — all from local frontend/ directory. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -262,19 +262,19 @@ func (dc *DomainCompiler) compileSimpleCondition(column, operator string, value
|
||||
return fmt.Sprintf("%q NOT IN (%s)", column, strings.Join(placeholders, ", ")), nil
|
||||
|
||||
case "like":
|
||||
dc.params = append(dc.params, value)
|
||||
dc.params = append(dc.params, wrapLikeValue(value))
|
||||
return fmt.Sprintf("%q LIKE $%d", column, paramIdx), nil
|
||||
|
||||
case "not like":
|
||||
dc.params = append(dc.params, value)
|
||||
dc.params = append(dc.params, wrapLikeValue(value))
|
||||
return fmt.Sprintf("%q NOT LIKE $%d", column, paramIdx), nil
|
||||
|
||||
case "ilike":
|
||||
dc.params = append(dc.params, value)
|
||||
dc.params = append(dc.params, wrapLikeValue(value))
|
||||
return fmt.Sprintf("%q ILIKE $%d", column, paramIdx), nil
|
||||
|
||||
case "not ilike":
|
||||
dc.params = append(dc.params, value)
|
||||
dc.params = append(dc.params, wrapLikeValue(value))
|
||||
return fmt.Sprintf("%q NOT ILIKE $%d", column, paramIdx), nil
|
||||
|
||||
case "=like":
|
||||
@@ -369,26 +369,27 @@ func (dc *DomainCompiler) compileQualifiedCondition(qualifiedColumn, operator st
|
||||
}
|
||||
return fmt.Sprintf("%s %s (%s)", qualifiedColumn, op, strings.Join(placeholders, ", ")), nil
|
||||
|
||||
case "like", "not like", "ilike", "not ilike", "=like", "=ilike":
|
||||
dc.params = append(dc.params, value)
|
||||
sqlOp := strings.ToUpper(strings.TrimPrefix(operator, "="))
|
||||
if strings.HasPrefix(operator, "=") {
|
||||
sqlOp = strings.ToUpper(operator[1:])
|
||||
}
|
||||
case "like", "not like", "ilike", "not ilike":
|
||||
dc.params = append(dc.params, wrapLikeValue(value))
|
||||
sqlOp := "LIKE"
|
||||
switch operator {
|
||||
case "like":
|
||||
sqlOp = "LIKE"
|
||||
case "not like":
|
||||
sqlOp = "NOT LIKE"
|
||||
case "ilike", "=ilike":
|
||||
case "ilike":
|
||||
sqlOp = "ILIKE"
|
||||
case "not ilike":
|
||||
sqlOp = "NOT ILIKE"
|
||||
case "=like":
|
||||
sqlOp = "LIKE"
|
||||
}
|
||||
return fmt.Sprintf("%s %s $%d", qualifiedColumn, sqlOp, paramIdx), nil
|
||||
|
||||
case "=like":
|
||||
dc.params = append(dc.params, value)
|
||||
return fmt.Sprintf("%s LIKE $%d", qualifiedColumn, paramIdx), nil
|
||||
|
||||
case "=ilike":
|
||||
dc.params = append(dc.params, value)
|
||||
return fmt.Sprintf("%s ILIKE $%d", qualifiedColumn, paramIdx), nil
|
||||
|
||||
default:
|
||||
dc.params = append(dc.params, value)
|
||||
return fmt.Sprintf("%s %s $%d", qualifiedColumn, operator, paramIdx), nil
|
||||
@@ -427,3 +428,18 @@ func normalizeSlice(value Value) []interface{} {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// wrapLikeValue wraps a string value with % wildcards for LIKE/ILIKE operators,
|
||||
// matching Odoo's behavior where ilike/like auto-wrap the search term.
|
||||
// If the value already contains %, it is left as-is.
|
||||
// Mirrors: odoo/orm/domains.py _expression._unaccent_wrap (value wrapping)
|
||||
func wrapLikeValue(value Value) Value {
|
||||
s, ok := value.(string)
|
||||
if !ok {
|
||||
return value
|
||||
}
|
||||
if strings.Contains(s, "%") || strings.Contains(s, "_") {
|
||||
return value // Already has wildcards, leave as-is
|
||||
}
|
||||
return "%" + s + "%"
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ type Model struct {
|
||||
|
||||
// Hooks
|
||||
BeforeCreate func(env *Environment, vals Values) error // Called before INSERT
|
||||
DefaultGet func(env *Environment, fields []string) Values // Dynamic defaults (e.g., from DB)
|
||||
Constraints []ConstraintFunc // Validation constraints
|
||||
Methods map[string]MethodFunc // Named business methods
|
||||
|
||||
@@ -53,6 +54,11 @@ type Model struct {
|
||||
computes map[string]ComputeFunc // field_name → compute function
|
||||
dependencyMap map[string][]string // trigger_field → []computed_field_names
|
||||
|
||||
// Onchange handlers
|
||||
// Maps field_name → handler that receives current vals and returns computed updates.
|
||||
// Mirrors: @api.onchange in Odoo.
|
||||
OnchangeHandlers map[string]func(env *Environment, vals Values) Values
|
||||
|
||||
// Resolved
|
||||
parents []*Model // Resolved parent models from _inherit
|
||||
allFields map[string]*Field // Including fields from parents
|
||||
@@ -227,6 +233,17 @@ func (m *Model) RegisterMethod(name string, fn MethodFunc) *Model {
|
||||
return m
|
||||
}
|
||||
|
||||
// RegisterOnchange registers an onchange handler for a field.
|
||||
// When the field changes on the client, the handler is called with the current
|
||||
// record values and returns computed field updates.
|
||||
// Mirrors: @api.onchange('field_name') in Odoo.
|
||||
func (m *Model) RegisterOnchange(fieldName string, handler func(env *Environment, vals Values) Values) {
|
||||
if m.OnchangeHandlers == nil {
|
||||
m.OnchangeHandlers = make(map[string]func(env *Environment, vals Values) Values)
|
||||
}
|
||||
m.OnchangeHandlers[fieldName] = handler
|
||||
}
|
||||
|
||||
// Extend extends this model with additional fields (like _inherit in Odoo).
|
||||
// Mirrors: class MyModelExt(models.Model): _inherit = 'res.partner'
|
||||
func (m *Model) Extend(fields ...*Field) *Model {
|
||||
|
||||
@@ -97,6 +97,16 @@ func (rs *Recordset) Create(vals Values) (*Recordset, error) {
|
||||
// Phase 1: Apply defaults for missing fields
|
||||
ApplyDefaults(m, vals)
|
||||
|
||||
// Apply dynamic defaults from model's DefaultGet hook (e.g., DB lookups)
|
||||
if m.DefaultGet != nil {
|
||||
dynDefaults := m.DefaultGet(rs.env, nil)
|
||||
for k, v := range dynDefaults {
|
||||
if _, exists := vals[k]; !exists {
|
||||
vals[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add magic fields
|
||||
if rs.env.uid > 0 {
|
||||
vals["create_uid"] = rs.env.uid
|
||||
@@ -363,12 +373,13 @@ func (rs *Recordset) Read(fields []string) ([]Values, error) {
|
||||
idPlaceholders[i] = fmt.Sprintf("$%d", i+1)
|
||||
}
|
||||
|
||||
// Fetch without ORDER BY — we'll reorder to match rs.ids below.
|
||||
// This preserves the caller's intended order (e.g., from Search with a custom ORDER).
|
||||
query := fmt.Sprintf(
|
||||
`SELECT %s FROM %q WHERE "id" IN (%s) ORDER BY %s`,
|
||||
`SELECT %s FROM %q WHERE "id" IN (%s)`,
|
||||
strings.Join(columns, ", "),
|
||||
m.table,
|
||||
strings.Join(idPlaceholders, ", "),
|
||||
m.order,
|
||||
)
|
||||
|
||||
rows, err := rs.env.tx.Query(rs.env.ctx, query, args...)
|
||||
@@ -377,7 +388,8 @@ func (rs *Recordset) Read(fields []string) ([]Values, error) {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []Values
|
||||
// Collect results keyed by ID so we can reorder them.
|
||||
resultsByID := make(map[int64]Values, len(rs.ids))
|
||||
for rows.Next() {
|
||||
scanDest := make([]interface{}, len(columns))
|
||||
for i := range scanDest {
|
||||
@@ -398,12 +410,22 @@ func (rs *Recordset) Read(fields []string) ([]Values, error) {
|
||||
rs.env.cache.Set(m.name, id, name, val)
|
||||
}
|
||||
}
|
||||
results = append(results, record)
|
||||
if id, ok := toRecordID(record["id"]); ok {
|
||||
resultsByID[id] = record
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Reorder results to match the original rs.ids order.
|
||||
results := make([]Values, 0, len(rs.ids))
|
||||
for _, id := range rs.ids {
|
||||
if rec, ok := resultsByID[id]; ok {
|
||||
results = append(results, rec)
|
||||
}
|
||||
}
|
||||
|
||||
// Post-fetch: M2M fields (from junction tables)
|
||||
if len(m2mFields) > 0 && len(rs.ids) > 0 {
|
||||
for _, fname := range m2mFields {
|
||||
@@ -619,7 +641,7 @@ func (rs *Recordset) NameGet() (map[int64]string, error) {
|
||||
|
||||
result := make(map[int64]string, len(records))
|
||||
for _, rec := range records {
|
||||
id, _ := rec["id"].(int64)
|
||||
id, _ := toRecordID(rec["id"])
|
||||
name, _ := rec[recName].(string)
|
||||
result[id] = name
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package orm
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
// ApplyRecordRules adds ir.rule domain filters to a search.
|
||||
// Mirrors: odoo/addons/base/models/ir_rule.py IrRule._compute_domain()
|
||||
@@ -10,32 +13,80 @@ import "fmt"
|
||||
// - Group rules are OR-ed within the group set
|
||||
// - The final domain is: global_rules AND (group_rule_1 OR group_rule_2 OR ...)
|
||||
//
|
||||
// For the initial implementation, we support company-based record rules:
|
||||
// Records with a company_id field are filtered to the user's company.
|
||||
// Implementation:
|
||||
// 1. Built-in company filter (for models with company_id)
|
||||
// 2. Custom ir.rule records loaded from the database
|
||||
func ApplyRecordRules(env *Environment, m *Model, domain Domain) Domain {
|
||||
if env.su {
|
||||
return domain // Superuser bypasses record rules
|
||||
}
|
||||
|
||||
// Auto-apply company filter if model has company_id
|
||||
// 1. Auto-apply company filter if model has company_id
|
||||
// Records where company_id = user's company OR company_id IS NULL (shared records)
|
||||
if f := m.GetField("company_id"); f != nil && f.Type == TypeMany2one {
|
||||
myCompany := Leaf("company_id", "=", env.CompanyID())
|
||||
noCompany := Leaf("company_id", "=", nil)
|
||||
companyFilter := Or(myCompany, noCompany)
|
||||
if len(domain) == 0 {
|
||||
return companyFilter
|
||||
domain = companyFilter
|
||||
} else {
|
||||
// AND the company filter with existing domain
|
||||
result := Domain{OpAnd}
|
||||
result = append(result, domain...)
|
||||
result = append(result, companyFilter...)
|
||||
domain = result
|
||||
}
|
||||
// AND the company filter with existing domain
|
||||
result := Domain{OpAnd}
|
||||
result = append(result, domain...)
|
||||
// Wrap company filter in the domain
|
||||
result = append(result, companyFilter...)
|
||||
return result
|
||||
}
|
||||
|
||||
// TODO: Load custom ir.rule records from DB and compile their domains
|
||||
// For now, only the built-in company filter is applied
|
||||
// 2. Load custom ir.rule records from DB
|
||||
// Mirrors: odoo/addons/base/models/ir_rule.py IrRule._compute_domain()
|
||||
//
|
||||
// Query rules that apply to this model for the current user:
|
||||
// - Rule must be active and have perm_read = true
|
||||
// - Either the rule has no group restriction (global rule),
|
||||
// or the user belongs to one of the rule's groups.
|
||||
// Use a savepoint so that a failed query (e.g., missing junction table)
|
||||
// doesn't abort the parent transaction.
|
||||
sp, spErr := env.tx.Begin(env.ctx)
|
||||
if spErr != nil {
|
||||
return domain
|
||||
}
|
||||
rows, err := sp.Query(env.ctx,
|
||||
`SELECT r.id, r.domain_force, COALESCE(r.global, false)
|
||||
FROM ir_rule r
|
||||
JOIN ir_model m ON m.id = r.model_id
|
||||
WHERE m.model = $1 AND r.active = true
|
||||
AND r.perm_read = true`,
|
||||
m.Name())
|
||||
if err != nil {
|
||||
sp.Rollback(env.ctx)
|
||||
return domain
|
||||
}
|
||||
defer func() {
|
||||
rows.Close()
|
||||
sp.Commit(env.ctx)
|
||||
}()
|
||||
|
||||
// Collect domain_force strings from matching rules
|
||||
// TODO: parse domain_force strings into Domain objects and merge them
|
||||
ruleCount := 0
|
||||
for rows.Next() {
|
||||
var ruleID int64
|
||||
var domainForce *string
|
||||
var global bool
|
||||
if err := rows.Scan(&ruleID, &domainForce, &global); err != nil {
|
||||
continue
|
||||
}
|
||||
ruleCount++
|
||||
// TODO: parse domainForce (Python-style domain string) into Domain
|
||||
// and AND global rules / OR group rules into the result domain.
|
||||
// For now, rules are loaded but domain parsing is deferred.
|
||||
_ = domainForce
|
||||
_ = global
|
||||
}
|
||||
if ruleCount > 0 {
|
||||
log.Printf("orm: loaded %d ir.rule record(s) for %s (domain parsing pending)", ruleCount, m.Name())
|
||||
}
|
||||
|
||||
return domain
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user