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

@@ -1,62 +1,94 @@
package server
import (
"fmt"
"net/http"
"strconv"
"strings"
"odoo-go/pkg/orm"
)
// handleImage serves images for model records.
// Mirrors: odoo/addons/web/controllers/binary.py content_image()
// For now, return SVG placeholders that look like real Odoo avatars.
//
// The client requests images like:
// /web/image/res.partner/5/avatar_128
// /web/image/product.product/1/image_1920
//
// We first try to read the binary field from the database.
// If no data is found, we fall back to an SVG placeholder.
func (s *Server) handleImage(w http.ResponseWriter, r *http.Request) {
field := r.URL.Query().Get("field")
model := r.URL.Query().Get("model")
// Parse: /web/image/<model>/<id>/<field>
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/web/image/"), "/")
// Also parse path-style URLs: /web/image/res.partner/2/avatar_128
path := strings.TrimPrefix(r.URL.Path, "/web/image/")
if model == "" && path != "" && path != r.URL.Path {
pathParts := strings.Split(path, "/")
if len(pathParts) >= 3 {
model = pathParts[0]
field = pathParts[2]
} else if len(pathParts) >= 1 {
model = pathParts[0]
model := ""
id := int64(0)
field := "image_1920"
// Also accept query parameters (legacy format)
if qm := r.URL.Query().Get("model"); qm != "" {
model = qm
}
if qf := r.URL.Query().Get("field"); qf != "" {
field = qf
}
if len(parts) >= 1 && parts[0] != "" {
model = parts[0]
}
if len(parts) >= 2 {
if n, err := strconv.ParseInt(parts[1], 10, 64); err == nil {
id = n
}
}
if len(parts) >= 3 && parts[2] != "" {
field = parts[2]
}
// Try to read binary data from DB
if model != "" && id > 0 {
m := orm.Registry.Get(model)
if m != nil {
f := m.GetField(field)
if f != nil && f.Type == orm.TypeBinary {
table := m.Table()
var data []byte
ctx := r.Context()
_ = s.pool.QueryRow(ctx,
fmt.Sprintf(`SELECT "%s" FROM "%s" WHERE id = $1`, f.Column(), table), id,
).Scan(&data)
if len(data) > 0 {
// Detect content type
contentType := http.DetectContentType(data)
w.Header().Set("Content-Type", contentType)
w.Header().Set("Cache-Control", "public, max-age=604800")
w.Write(data)
return
}
}
}
}
// Company logo: return a styled SVG with company initial
if model == "res.company" && (field == "logo" || field == "logo_web") {
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Cache-Control", "public, max-age=3600")
w.Write([]byte(`<svg xmlns="http://www.w3.org/2000/svg" width="100" height="30" viewBox="0 0 100 30">
<rect width="100" height="30" rx="4" fill="#71639e"/>
<text x="50" y="20" font-family="sans-serif" font-size="14" fill="white" text-anchor="middle">Odoo</text>
</svg>`))
return
// Fallback: SVG placeholder with the record's initial letter
initial := "?"
if model != "" && id > 0 {
m := orm.Registry.Get(model)
if m != nil {
var name string
_ = s.pool.QueryRow(r.Context(),
fmt.Sprintf(`SELECT COALESCE(name, '') FROM "%s" WHERE id = $1`, m.Table()), id,
).Scan(&name)
if len(name) > 0 {
initial = strings.ToUpper(name[:1])
}
}
}
// User avatar: return a colored circle with initial
if (model == "res.partner" || model == "res.users") &&
(field == "avatar_128" || field == "avatar_256" || field == "image_128" || field == "image_256" || strings.HasPrefix(field, "avatar") || strings.HasPrefix(field, "image")) {
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Cache-Control", "public, max-age=3600")
w.Write([]byte(`<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Cache-Control", "public, max-age=604800")
fmt.Fprintf(w, `<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">
<rect width="64" height="64" rx="32" fill="#71639e"/>
<text x="32" y="40" font-family="sans-serif" font-size="28" fill="white" text-anchor="middle">U</text>
</svg>`))
return
}
// Default: 1x1 transparent PNG
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Cache-Control", "public, max-age=3600")
png := []byte{
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00,
0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x62, 0x00, 0x00, 0x00, 0x02,
0x00, 0x01, 0xe5, 0x27, 0xde, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45,
0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
}
w.Write(png)
<text x="32" y="40" font-family="sans-serif" font-size="28" fill="white" text-anchor="middle">%s</text>
</svg>`, initial)
}