This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contenttype.go
87 lines (79 loc) · 2.76 KB
/
contenttype.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
package uadmin
import (
"fmt"
"github.com/sergeyglazyrindev/uadmin/core"
"os"
)
type ContentTypeCommand struct {
}
func (c ContentTypeCommand) Proceed(subaction string, args []string) error {
var action string
var help string
var isCorrectActionPassed bool = false
commandRegistry := &core.CommandRegistry{
Actions: make(map[string]core.ICommand),
}
commandRegistry.AddAction("sync", &SyncContentTypes{})
if len(os.Args) > 2 {
action = os.Args[2]
isCorrectActionPassed = commandRegistry.IsRegisteredCommand(action)
}
if !isCorrectActionPassed {
helpText := commandRegistry.MakeHelpText()
help = fmt.Sprintf(`
Please provide what do you want to do ?
%s
`, helpText)
fmt.Print(help)
return nil
}
commandRegistry.RunAction(subaction, "", args)
return nil
}
func (c ContentTypeCommand) GetHelpText() string {
return "Content type for uadmin project"
}
type SyncContentTypes struct {
}
func (command SyncContentTypes) Proceed(subaction string, args []string) error {
appInstance.InitializeRouter()
uadminDatabase := core.NewUadminDatabase()
defer uadminDatabase.Close()
db := uadminDatabase.Db
var contentType core.ContentType
var permission core.Permission
for blueprintRootAdminPage := range core.CurrentDashboardAdminPanel.AdminPages.GetAll() {
core.Trail(core.INFO, "Sync content types for blueprint %s", blueprintRootAdminPage.BlueprintName)
for modelPage := range blueprintRootAdminPage.SubPages.GetAll() {
if modelPage.Model == nil {
continue
}
core.Trail(core.INFO, "Add content type for model %s - %s", modelPage.BlueprintName, modelPage.ModelName)
db.Model(&core.ContentType{}).Where(
&core.ContentType{BlueprintName: modelPage.BlueprintName, ModelName: modelPage.ModelName},
).First(&contentType)
if contentType.ID == 0 {
contentType = core.ContentType{BlueprintName: modelPage.BlueprintName, ModelName: modelPage.ModelName}
db.Create(&contentType)
core.Trail(core.INFO, "Created content type for blueprint %s model %s", modelPage.BlueprintName, modelPage.ModelName)
}
for permDescribed := range core.ProjectPermRegistry.GetAllPermissions() {
db.Model(&core.Permission{}).Where(
&core.Permission{ContentTypeID: contentType.ID, PermissionBits: permDescribed.Bit},
).First(&permission)
if permission.ID == 0 {
permission = core.Permission{ContentTypeID: contentType.ID, PermissionBits: permDescribed.Bit}
db.Create(&permission)
core.Trail(core.INFO, "Created permission %s for blueprint %s model %s", permDescribed.Name, modelPage.BlueprintName, modelPage.ModelName)
permission = core.Permission{}
}
permission = core.Permission{}
}
contentType = core.ContentType{}
}
}
return nil
}
func (command SyncContentTypes) GetHelpText() string {
return "Sync your content types"
}