forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist_handler.go
105 lines (91 loc) · 2.46 KB
/
list_handler.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
package uadmin
import (
"fmt"
"net/http"
"strings"
)
// listHandler
func listHandler(w http.ResponseWriter, r *http.Request, session *Session) {
r.ParseMultipartForm(32 << 20)
type Context struct {
User string
Pagination int
Data *listData
Schema ModelSchema
IsUpdated bool
CanAdd bool
CanDelete bool
HasAccess bool
SiteName string
Language Language
RootURL string
HasCategorical bool
Searchable bool
CSRF string
Logo string
FavIcon string
Menu []DashboardMenu
}
c := Context{}
c.RootURL = RootURL
c.SiteName = SiteName
c.Language = getLanguage(r)
c.User = session.User.Username
c.CSRF = session.Key
c.Logo = Logo
c.FavIcon = FavIcon
user := session.User
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
ModelName := r.URL.Path
// Check permissions
perm := user.GetAccess(ModelName)
if !perm.Read {
pageErrorHandler(w, r, session)
return
}
c.HasAccess = perm.Read
c.CanAdd = perm.Add
c.CanDelete = perm.Delete
// Initialize the schema
m, ok := NewModel(ModelName, false)
// Return 404 if it is an unknown model
if !ok {
pageErrorHandler(w, r, session)
return
}
// Process delete
if r.Method == cPOST {
if r.FormValue("delete") == "delete" {
processDelete(ModelName, w, r, session, &user)
c.IsUpdated = true
http.Redirect(w, r, fmt.Sprint(RootURL+r.URL.Path), http.StatusSeeOther)
}
}
// Get the schema for the model
c.Schema, _ = getSchema(m.Interface())
for i := range c.Schema.Fields {
if c.Schema.Fields[i].CategoricalFilter {
c.HasCategorical = true
}
if c.Schema.Fields[i].Filter && c.Schema.Fields[i].Type == cFK {
c.Schema.Fields[i].Choices = getChoices(strings.ToLower(c.Schema.Fields[i].TypeName))
}
if c.Schema.Fields[i].Searchable {
c.Searchable = true
}
}
// func (*ModelSchema, *User) (string, []interface{})
query := ""
args := []interface{}{}
if c.Schema.ListModifier != nil {
query, args = c.Schema.ListModifier(&c.Schema, &user)
}
c.Data = getListData(m.Interface(), PageLength, r, session, query, &c.Schema, args...)
c.Pagination = paginationHandler(c.Data.Count, PageLength)
c.Menu = session.User.GetDashboardMenu()
for i := range c.Menu {
c.Menu[i].MenuName = Translate(c.Menu[i].MenuName, c.Language.Code, true)
}
RenderHTML(w, r, "./templates/uadmin/"+c.Schema.GetListTheme()+"/list.html", c)
}