forked from hexya-erp/hexya-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_parameter.go
86 lines (76 loc) · 2.79 KB
/
config_parameter.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
// Copyright 2017 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package base
import (
"fmt"
"github.com/hexya-erp/hexya/hexya/models"
"github.com/hexya-erp/hexya/pool"
"github.com/spf13/viper"
)
var defaultParameters map[string](func(env models.Environment) (string, pool.GroupSet)) = map[string](func(env models.Environment) (string, pool.GroupSet)){
"web.base.url": func(env models.Environment) (string, pool.GroupSet) {
return fmt.Sprintf("http://localhost:%s", viper.GetString("Server.Port")), pool.Group().NewSet(env)
},
}
func init() {
pool.ConfigParameter().DeclareModel()
pool.ConfigParameter().AddFields(map[string]models.FieldDefinition{
"Key": models.CharField{Index: true, Required: true, Unique: true},
"Value": models.TextField{Required: true},
"Groups": models.Many2ManyField{RelationModel: pool.Group()},
})
pool.ConfigParameter().Methods().Init().DeclareMethod(
`Init Initializes the parameters listed in defaultParameters.
It overrides existing parameters if force is 'true'.`,
func(rs pool.ConfigParameterSet, force ...bool) {
var forceInit bool
if len(force) > 0 && force[0] {
forceInit = true
}
for key, fnct := range defaultParameters {
params := rs.Model().NewSet(rs.Env()).Sudo().Search(pool.ConfigParameter().Key().Equals(key))
if forceInit || params.IsEmpty() {
value, groups := fnct(rs.Env())
pool.ConfigParameter().NewSet(rs.Env()).SetParam(key, value).LimitToGroups(groups)
}
}
})
pool.ConfigParameter().Methods().GetParam().DeclareMethod(
`GetParam retrieves the value for a given key. It returns defaultValue if the parameter is missing.`,
func(rs pool.ConfigParameterSet, key string, defaultValue string) string {
param := rs.Model().Search(rs.Env(), pool.ConfigParameter().Key().Equals(key)).Limit(1).Load("Value")
if param.Value() == "" {
return defaultValue
}
return param.Value()
})
pool.ConfigParameter().Methods().SetParam().DeclareMethod(
`SetParam sets the value of a parameter. It returns the parameter`,
func(rs pool.ConfigParameterSet, key, value string) pool.ConfigParameterSet {
var res pool.ConfigParameterSet
param := rs.Model().Search(rs.Env(), pool.ConfigParameter().Key().Equals(key))
if param.IsEmpty() {
if value != "" {
res = rs.Create(&pool.ConfigParameterData{
Key: key,
Value: value,
})
}
return res
}
if value == "" {
param.Unlink()
return rs.Model().NewSet(rs.Env())
}
param.SetValue(value)
return param
})
pool.ConfigParameter().Methods().LimitToGroups().DeclareMethod(
`LimitToGroups limits the access to this key to the given list of groups`,
func(rs pool.ConfigParameterSet, groups pool.GroupSet) {
if rs.IsEmpty() {
return
}
rs.SetGroups(groups)
})
}