feat: Portal, Email Inbound, Discuss + module improvements

- Portal: /my/* routes, signup, password reset, portal user support
- Email Inbound: IMAP polling (go-imap/v2), thread matching
- Discuss: mail.channel, long-polling bus, DM, unread count
- Cron: ir.cron runner (goroutine scheduler)
- Bank Import, CSV/Excel Import
- Automation (ir.actions.server)
- Fetchmail service
- HR Payroll model
- Various fixes across account, sale, stock, purchase, crm, hr, project

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marc
2026-04-12 18:41:57 +02:00
parent 2c7c1e6c88
commit 66383adf06
87 changed files with 14696 additions and 654 deletions

View File

@@ -1,6 +1,10 @@
package models
import "odoo-go/pkg/orm"
import (
"fmt"
"odoo-go/pkg/orm"
)
// initIrCron registers ir.cron — Scheduled actions.
// Mirrors: odoo/addons/base/models/ir_cron.py class IrCron
@@ -30,5 +34,86 @@ func initIrCron() {
orm.Integer("priority", orm.FieldOpts{String: "Priority", Default: 5}),
orm.Char("code", orm.FieldOpts{String: "Python Code"}),
orm.Many2one("model_id", "ir.model", orm.FieldOpts{String: "Model"}),
// Execution target (simplified: direct model+method instead of ir.actions.server)
orm.Char("model_name", orm.FieldOpts{String: "Model Name"}),
orm.Char("method_name", orm.FieldOpts{String: "Method Name"}),
// Failure tracking
orm.Integer("failure_count", orm.FieldOpts{String: "Failure Count", Default: 0}),
orm.Datetime("first_failure_date", orm.FieldOpts{String: "First Failure Date"}),
)
// Constraint: validate model_name and method_name against the registry.
// Prevents setting arbitrary/invalid model+method combos on cron jobs.
m.AddConstraint(func(rs *orm.Recordset) error {
records, err := rs.Read([]string{"model_name", "method_name"})
if err != nil || len(records) == 0 {
return nil
}
rec := records[0]
modelName, _ := rec["model_name"].(string)
methodName, _ := rec["method_name"].(string)
if modelName == "" && methodName == "" {
return nil // both empty is OK (legacy code-based crons)
}
if modelName != "" {
model := orm.Registry.Get(modelName)
if model == nil {
return fmt.Errorf("ir.cron: model %q not found in registry", modelName)
}
if methodName != "" && model.Methods != nil {
if _, ok := model.Methods[methodName]; !ok {
return fmt.Errorf("ir.cron: method %q not found on model %q", methodName, modelName)
}
}
}
return nil
})
// method_direct_trigger — manually trigger a cron job.
// Mirrors: odoo/addons/base/models/ir_cron.py method_direct_trigger
m.RegisterMethod("method_direct_trigger", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
// Admin-only: only uid=1 or superuser may trigger cron jobs directly
env := rs.Env()
if env.UID() != 1 && !env.IsSuperuser() {
return nil, fmt.Errorf("ir.cron: method_direct_trigger requires admin privileges")
}
records, err := rs.Read([]string{"model_name", "method_name"})
if err != nil {
return nil, fmt.Errorf("ir.cron: method_direct_trigger read failed: %w", err)
}
if len(records) == 0 {
return nil, fmt.Errorf("ir.cron: method_direct_trigger: no record found")
}
rec := records[0]
modelName, _ := rec["model_name"].(string)
methodName, _ := rec["method_name"].(string)
if modelName == "" || methodName == "" {
return nil, fmt.Errorf("ir.cron: model_name or method_name not set")
}
// Validate model_name against registry (prevents calling arbitrary models)
model := orm.Registry.Get(modelName)
if model == nil {
return nil, fmt.Errorf("ir.cron: model %q not found in registry", modelName)
}
if model.Methods == nil {
return nil, fmt.Errorf("ir.cron: model %q has no methods", modelName)
}
method, ok := model.Methods[methodName]
if !ok {
return nil, fmt.Errorf("ir.cron: method %q not found on model %q", methodName, modelName)
}
result, err := method(env.Model(modelName), args...)
if err != nil {
return nil, fmt.Errorf("ir.cron: %s.%s failed: %w", modelName, methodName, err)
}
return result, nil
})
}

View File

@@ -127,9 +127,28 @@ func initIrActions() {
{Value: "object_write", Label: "Update Record"},
{Value: "object_create", Label: "Create Record"},
{Value: "multi", Label: "Execute Several Actions"},
{Value: "email", Label: "Send Email"},
}, orm.FieldOpts{String: "Action To Do", Default: "code", Required: true}),
orm.Text("code", orm.FieldOpts{String: "Code"}),
orm.Many2many("groups_id", "res.groups", orm.FieldOpts{String: "Groups"}),
// Automated action fields
orm.Selection("trigger", []orm.SelectionItem{
{Value: "on_create", Label: "On Creation"},
{Value: "on_write", Label: "On Update"},
{Value: "on_create_or_write", Label: "On Creation & Update"},
{Value: "on_unlink", Label: "On Deletion"},
{Value: "on_time", Label: "Based on Time Condition"},
}, orm.FieldOpts{String: "Trigger"}),
orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
// object_write: fields to update
orm.Text("update_field_id", orm.FieldOpts{String: "Field to Update"}),
orm.Char("update_value", orm.FieldOpts{String: "Value"}),
// email: template fields
orm.Char("email_to", orm.FieldOpts{String: "Email To", Help: "Field name on the record (e.g. email, partner_id.email)"}),
orm.Char("email_subject", orm.FieldOpts{String: "Email Subject"}),
orm.Text("email_body", orm.FieldOpts{String: "Email Body", Help: "HTML body. Use {{field_name}} for record values."}),
// filter domain
orm.Text("filter_domain", orm.FieldOpts{String: "Filter Domain", Help: "Only trigger when record matches this domain"}),
)
}

View File

@@ -66,8 +66,39 @@ func initResUsers() {
String: "Share User", Compute: "_compute_share", Store: true,
Help: "External user with limited access (portal/public)",
}),
orm.Char("signup_token", orm.FieldOpts{String: "Signup Token"}),
orm.Datetime("signup_expiration", orm.FieldOpts{String: "Signup Token Expiration"}),
)
// _compute_share: portal/public users have share=true (not in group_user).
// Mirrors: odoo/addons/base/models/res_users.py Users._compute_share()
m.RegisterMethod("_compute_share", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
env := rs.Env()
// Look up group_user ID
var groupUserID int64
err := env.Tx().QueryRow(env.Ctx(),
`SELECT g.id FROM res_groups g
JOIN ir_model_data imd ON imd.res_id = g.id AND imd.model = 'res.groups'
WHERE imd.module = 'base' AND imd.name = 'group_user'`).Scan(&groupUserID)
if err != nil {
return nil, nil // Can't determine, skip
}
for _, id := range rs.IDs() {
var inGroup bool
err := env.Tx().QueryRow(env.Ctx(),
`SELECT EXISTS(
SELECT 1 FROM res_groups_res_users_rel
WHERE res_groups_id = $1 AND res_users_id = $2
)`, groupUserID, id).Scan(&inGroup)
if err != nil {
continue
}
env.Model("res.users").Browse(id).Write(orm.Values{"share": !inGroup})
}
return nil, nil
})
// -- Methods --
// action_get returns the "Change My Preferences" action for the current user.