forked from npiganeau/yep
/
actions.go
183 lines (162 loc) · 5.33 KB
/
actions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2016 NDP Systèmes. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ir
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/beevik/etree"
"github.com/npiganeau/yep/yep/tools"
)
type ActionType string
const (
ACTION_ACT_WINDOW ActionType = "ir.actions.act_window"
ACTION_SERVER ActionType = "ir.actions.server"
)
type ActionViewType string
const (
ACTION_VIEW_TYPE_FORM ActionViewType = "form"
ACTION_VIEW_TYPE_TREE ActionViewType = "tree"
)
var ActionsRegistry *ActionsCollection
func MakeActionRef(id string) ActionRef {
action := ActionsRegistry.GetActionById(id)
if action == nil {
return ActionRef{}
}
return ActionRef{id, action.Name}
}
// ActionRef is an array of two strings representing an action:
// - The first one is the ID of the action
// - The second one is the name of the action
type ActionRef [2]string
func (ar ActionRef) String() string {
sl := []string{ar[0], ar[1]}
return fmt.Sprintf(`[%s]`, strings.Join(sl, ","))
}
// Value extracts ID of our ActionRef for storing in the database.
func (ar ActionRef) Value() (driver.Value, error) {
return driver.Value(ar[0]), nil
}
// Scan fetches the name of our action from the ID
// stored in the database to fill the ActionRef.
func (ar *ActionRef) Scan(src interface{}) error {
var source string
switch s := src.(type) {
case string:
source = s
case []byte:
source = string(s)
default:
return fmt.Errorf("Invalid type for ActionRef: %T", src)
}
*ar = MakeActionRef(source)
return nil
}
var _ driver.Valuer = ActionRef{}
var _ sql.Scanner = &ActionRef{}
type ActionsCollection struct {
sync.RWMutex
actions map[string]*BaseAction
}
// NewActionCollection returns a pointer to a new
// ActionsCollection instance
func NewActionsCollection() *ActionsCollection {
res := ActionsCollection{
actions: make(map[string]*BaseAction),
}
return &res
}
// AddAction adds the given action to our ActionsCollection
func (ar *ActionsCollection) AddAction(a *BaseAction) {
ar.Lock()
defer ar.Unlock()
ar.actions[a.ID] = a
}
// GetActionById returns the Action with the given id
func (ar *ActionsCollection) GetActionById(id string) *BaseAction {
return ar.actions[id]
}
type BaseAction struct {
ID string `json:"id"`
Type ActionType `json:"type"`
Name string `json:"name"`
Model string `json:"res_model"`
ResID int64 `json:"res_id"`
Groups []string `json:"groups_id"`
Domain string `json:"domain"`
Help string `json:"help"`
SearchView ViewRef `json:"search_view_id"`
SrcModel string `json:"src_model"`
Usage string `json:"usage"`
//Flags interface{}`json:"flags"`
Views []ViewRef `json:"views"`
View ViewRef `json:"view_id"`
AutoRefresh bool `json:"auto_refresh"`
ManualSearch bool `json:"-"`
ActViewType ActionViewType `json:"-"`
ViewMode string `json:"view_mode"`
ViewIds []string `json:"view_ids"`
Multi bool `json:"multi"`
Target string `json:"target"`
AutoSearch bool `json:"auto_search"`
//SearchView string `json:"search_view"`
Filter bool `json:"filter"`
Limit int64 `json:"limit"`
Context tools.Context `json:"context"`
}
type Toolbar struct {
Print []*BaseAction `json:"print"`
Action []*BaseAction `json:"action"`
Relate []*BaseAction `json:"relate"`
}
/*
LoadActionFromEtree reads the action given etree.Element, creates or updates the action
and adds it to the action registry if it not already.
*/
func LoadActionFromEtree(element *etree.Element) {
// We populate an actionHash from XML data fields
actionHash := make(map[string]interface{})
actionHash["id"] = element.SelectAttrValue("id", "NO_ID")
for _, fieldNode := range element.FindElements("field") {
name := fieldNode.SelectAttrValue("name", "NO_NAME")
ref := fieldNode.SelectAttrValue("ref", "")
if ref != "" && (name == "view_id" || name == "search_view_id") {
actionHash[name] = MakeViewRef(ref)
} else if len(fieldNode.ChildElements()) > 0 {
fieldType := fieldNode.SelectAttrValue("type", "text")
switch fieldType {
case "html":
nodeDoc := etree.NewDocument()
nodeDoc.SetRoot(fieldNode.ChildElements()[0].Copy())
value, _ := nodeDoc.WriteToString()
actionHash[name] = value
default:
tools.LogAndPanic(log, "Unknown field type", "type", fieldType, "action", actionHash["id"])
}
} else {
actionHash[name] = fieldNode.Text()
}
}
// We marshal viewHash in JSON and then unmarshal into a View struct
bytes, _ := json.Marshal(actionHash)
var act BaseAction
if err := json.Unmarshal(bytes, &act); err != nil {
tools.LogAndPanic(log, "Unable to unmarshal action", "actionHash", actionHash, "error", err)
}
ActionsRegistry.AddAction(&act)
}